基于单元格值的DataGrid行背景
我目前正在开发 C# WPF 数据网格。我有一个 DataGrid,它具有自动生成的列,代码连接到 SQLite 数据库并创建一个数据集,然后该数据集被设置为 DataGrid ItemsSource。
下面是带有 DataGrid 的 XAML 的代码
<DataGrid AutoGenerateColumns="True"
Margin="12,71,12,32"
Name="tblLog"
ColumnWidth="*"
CanUserResizeRows="False"
AreRowDetailsFrozen="False"
CanUserAddRows="True"
CanUserDeleteRows="True"
IsReadOnly="True"
MouseDoubleClick="tblLog_MouseDoubleClick">
</DataGrid>
下面是为 DataGrid 设置 ItemsSource 的代码
try
{
DataSet ds = new DataSet();
SQLiteDataAdapter da = new SQLiteDataAdapter(query, db.conn);
da.Fill(ds);
//tblGrid.AutoGenerateColumns = true;
tblGrid.ItemsSource = ds.Tables[0].DefaultView;
}
catch (SQLiteException ex)
{
MessageBox.Show("Unable to retrieve logins from database.\n\n" + ex.Message + "\n\nError Code: " + ex.ErrorCode);
}
数据库中显示的列(自动生成)是 ID、日期、时间、状态。 我需要做的是,如果状态列的一行中的值等于错误,则更改该行的背景颜色。
我假设我需要在 DataGrid 标记中添加某种样式标记和 DataTriggers,但不确定我需要什么。我尝试设置 ItemsSource 的代码会显示一条错误,指出在添加 ItemsSource 之前源需要为空。
感谢您提供的任何帮助。
I am currently working on a C# WPF datagrid. I have a DataGrid which has auto generated columns and the code connects to an SQLite Database and creates a dataset and then this dataset is set as the DataGrid ItemsSource.
Below is the code with the XAML of the DataGrid
<DataGrid AutoGenerateColumns="True"
Margin="12,71,12,32"
Name="tblLog"
ColumnWidth="*"
CanUserResizeRows="False"
AreRowDetailsFrozen="False"
CanUserAddRows="True"
CanUserDeleteRows="True"
IsReadOnly="True"
MouseDoubleClick="tblLog_MouseDoubleClick">
</DataGrid>
And below is the code to set the ItemsSource for the DataGrid
try
{
DataSet ds = new DataSet();
SQLiteDataAdapter da = new SQLiteDataAdapter(query, db.conn);
da.Fill(ds);
//tblGrid.AutoGenerateColumns = true;
tblGrid.ItemsSource = ds.Tables[0].DefaultView;
}
catch (SQLiteException ex)
{
MessageBox.Show("Unable to retrieve logins from database.\n\n" + ex.Message + "\n\nError Code: " + ex.ErrorCode);
}
The columns that are shown in the database (auto generated) are ID, date, time, status.
What I need to be able to do is if the value in a row of the status column equals Error change the background colour of that row.
I assume I need to add some sort of styling tags and DataTriggers within the DataGrid tags but not sure what I need. Anything I have tried to the code that sets the ItemsSource displays an error saying that the Source needs to be empty before adding the ItemsSource.
Thanks for any help you can provide.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您可以使用 DataTrigger 来执行此操作。
这是一个快速示例。我创建了一个名为 Person 的类,其属性为 Name、Age 和 Active。
在主窗口的构造函数中,我将 3 个
Person
对象添加到列表中,然后将该列表绑定到DataGrid
。然后,在 MainWindow 的 XAML 中,我创建一个 DataTrigger 样式作为资源。
此触发器的作用是从 DataGridRow 中的
Person
对象的Active
字段中获取值,如果该值为 false,则它会变为背景颜色该行的颜色变为红色。You can use a DataTrigger to do this.
Here is a quick sample. I created a class called Person with the properties Name, Age, and Active.
In the constructor of the main window, I add 3
Person
objects to a list, then bind that list to theDataGrid
.Then in the XAML for the MainWindow, I create a DataTrigger style as a resource.
What this trigger does is it takes the value from the
Active
field from thePerson
object that is in the DataGridRow, and if that value is false, then it turns to background color of the row to red.