简体   繁体   English

更改数据绑定DataGridView中的显示信息

[英]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.

相关问题 DataGridView.CellValueChanged无法为数据绑定的DataGridView触发 - DataGridView.CellValueChanged not firing for data-bound DataGridView 防止DataGridView自动将编辑提交到数据绑定的对象 - Preventing DataGridView from automatically committing edits to data-bound object 如何用图标呈现数据绑定的WinForms DataGridView列? - How to render a data-bound WinForms DataGridView Column with an Icon? 在DataGridView中将静态“标签”值与数据绑定值混合在一起是否可行? - Is it feasible to mix static “label” values with data-bound values in a DataGridView? 控件数据绑定时无法在DataGridView中以编程方式添加行 - Rows cannot be added programmatically in the DataGridView when the control data-bound 尝试使用数据绑定的DataGridView设置最大行数 - Trying to set a maximum row count with a data-bound DataGridView 无法将行动态添加到我的数据绑定dataGridview中 - Cannot add rows to my data-bound dataGridview dynamically 如何在数据绑定时以编程方式向 datagridview 添加一行? - How to programmatically add a row to a datagridview when it is data-bound? 无法在数据绑定的 DataGridView 控件上设置 ColumnCount 属性。 c# - ColumnCount property cannot be set on a data-bound DataGridView control. c# DataGridView 新项目的编程选择不会更新其他数据绑定控件 - DataGridView programatic selection of new item does not update other data-bound controls
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM