简体   繁体   English

格式化DataGridView中的特定行

[英]Formatting specific rows in a DataGridView

I have a DGV named dataGridView1 that has two columns, an image column and a string column. 我有一个名为dataGridView1的DGV,它具有两列,即图像列和字符串列。 I also have a custom Collection of data that I use to populate the DGV. 我还具有用于填充DGV的自定义数据集合。 In my particular application, each row will have a specified string in the string column and one of two images in the image column. 在我的特定应用程序中,每一行在字符串列中都有一个指定的字符串,在图像列中有两个图像之一。 I am having trouble with displaying the correct image in the image column when the DGV populates. DGV填充时,我无法在图像列中显示正确的图像。

This is how I am filtering the data to what I want to put in the DGV: 这就是我将数据过滤到要放入DGV中的方式:

var match = Core.Set.Servers.Where(ServerItem => ServerItem.GameTag == text);

Currently, I am doing this to populate the DGV: 目前,我正在这样做以填充DGV:

dataGridView1.AutoGenerateColumns = false;
source = new BindingSource(match,null);
dataGridView1.DataSource = source;

However, the image cells just show the default broken image icon. 但是,图像单元格仅显示默认的残破图像图标。 My icon is located in 我的图标位于

Directory.GetCurrentDirectory() + "//Images/favorite.png";

Is there a good way using a DataTable or even a BindingSource? 有没有使用DataTable甚至BindingSource的好方法? Each item in the collection has two useful features: ServerItem.ServerName and ServerItem.IsFavorite. 集合中的每个项目都有两个有用的功能:ServerItem.ServerName和ServerItem.IsFavorite。 The first is a string, the second is a boolean. 第一个是字符串,第二个是布尔值。 I want the favorite icon to be displayed in the icon column of each row that has IsFavorite==true. 我希望收藏夹图标显示在具有IsFavorite == true的每一行的图标列中。

To show an image in a bound DataGridView based on a data value you should handle CellFormatting event of the DataGridView. 要基于数据值在绑定的DataGridView中显示图像,您应该处理DataGridView的CellFormatting事件。 I would recommend store an image in some memory structure like ImageList to avoid roundtrips to storage. 我建议将图像存储在诸如ImageList某些内存结构中,以避免往返存储。 Here is a snippet: 这是一个片段:

List<Row> data = new List<Row>
{
    new Row { IsFavorite = true },
    new Row { IsFavorite = false },
};

dataGridView1.Columns.Add(new DataGridViewImageColumn(false));
dataGridView1.Columns[0].DataPropertyName = "IsFavorite";
dataGridView1.Columns[0].DefaultCellStyle.NullValue = null;
dataGridView1.DataSource = data;

private void dataGridView1_CellFormatting(object sender, DataGridViewCellFormattingEventArgs e)
{
    if (e.ColumnIndex == 0)
    {
        if (e.Value != null && e.Value is bool)
        {
            if ((bool)e.Value == true)
            {
                e.Value = imageList1.Images[0];
            }
            else
            {
                e.Value = null;
            }
        }
    }
}

public class Row
{
    public bool IsFavorite { get; set; }
}

And there is another advice: to combine a path from parts you can use Path.Combine(string[]) 还有另一条建议:要组合零件的路径,可以使用Path.Combine(string [])

Hope this helps. 希望这可以帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM