[英]Alter Displayed Information in data-bound DataGridView
I have a datagridview that is databound. 我有一个数据绑定的datagridview。 One of the columns, which is shown below, is linked to a boolean value from an object. 如下所示,其中一列链接到对象的布尔值。 The method to retrieve the boolean value would be "curResultSet.Pass". 检索布尔值的方法是“ curResultSet.Pass”。 Instead of saying "True" or "False" in a regular cell, I want that column to have a "Pass" icon or "Fail" icon depending on the value of "curResult.Pass". 我不希望在常规单元格中说“ True”或“ False”,而是希望该列具有“ Pass”图标或“ Fail”图标,具体取决于“ curResult.Pass”的值。
I am fairly certain that my method of setting a .png to an Icon is wrong, but I can worry about that later. 我可以肯定的是,将.png设置为Icon的方法是错误的,但我稍后会担心。 Right now, my biggest concern is I am not sure how to set a certain Icon in the DataGridViewImageViewColumn based on the value of a Boolean as shown in my code below. 现在,我最大的担心是我不确定如何根据布尔值在DataGridViewImageViewColumn中设置某个图标,如下面的代码所示。
DataGridViewImageColumn imgCol = new DataGridViewImageColumn(true);
foreach(RunResult curResult in curResultSet)
{
bindingSource1.Add(curResult);
}
resultDataGridView.AutoGenerateColumns = false;
resultDataGridView.DataSource = bindingSource1;
imgCol.Image = Image.FromFile(imageFilePath + "NG icon.png");
imgCol.Icon = Icon.ExtractAssociatedIcon(imageFilePath + "OK icon.png");
imgCol.ValuesAreIcons = true;
resultDataGridView.Columns.Add(imgCol);
resultDataGridView.Columns[resultDataGridView.ColumnCount - 1].DataPropertyName = "Pass";
resultDataGridView.Columns[resultDataGridView.ColumnCount - 1].Name = "status";
Anyone have any idea how I can have my "OK icon.png" show when "curResultSet[].Pass == True" and have "NG icon.png" show when "curResultSet[].Pass == False"? 有人知道如何在“ curResultSet []。Pass == True”时显示我的“ OK icon.png”,而在“ curResultSet []。Pass == False”时显示“ NG icon.png”吗? Thanks! 谢谢!
You should use a converter and bind the image source to the boolean value: public class BoolToUriConverter : IValueConverter { private const string filePath = "your path"; 您应该使用转换器并将图像源绑定到布尔值:公共类BoolToUriConverter:IValueConverter {private const string filePath =“ your path”;
public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
{
bool val = (bool)value ;
string path = (val == true) ? $"{filePath}OK icon.png" : $"{filePath}NG icon.png";
return new Uri(path);
}
public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
{
throw new NotImplementedException();
}
}
And use it to bind your boolean column to the image column 并使用它将您的布尔列绑定到图像列
<DataGrid ItemsSource="{Binding Collection1}" AutoGenerateColumns="False">
<DataGrid.Columns>
<DataGridTextColumn Header="Field1" Binding="{Binding Field1}"></DataGridTextColumn>
<DataGridTextColumn Header="Field2" Binding="{Binding Field2}"></DataGridTextColumn>
<DataGridCheckBoxColumn Header="Field3" Binding="{Binding Field3}"></DataGridCheckBoxColumn>
<DataGridTemplateColumn>
<DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<Image Source="{Binding Field3, Converter={StaticResource boolToUriConverter}}"/>
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>
</DataGrid.Columns>
</DataGrid>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.