如何使用图标呈现数据绑定的 WinForms DataGridView 列?

Ang*_*ket 4 .net c# datagridview winforms

在我的 C# Windows 窗体应用程序中,我有一个DataGridView绑定到BindingList<Item>列表的列表,该列表又使用List<Item>.

// bind view to controller
myDataGridView.DataBindings.Add("DataSource", myController, "Items");

// bind controller to model
Items = new BindingList<Item>(model.Items);
Run Code Online (Sandbox Code Playgroud)

因此,数据网格的列是根据 class 的属性生成的Item。我为DataGridViewsCellFormatting事件提供了一个处理程序方法,以根据类型的某些属性值显示某些单元格值Item

myDataGridView.CellFormatting += new DataGridViewCellFormattingEventHandler(myontroller.HandleCellFormatting);
Run Code Online (Sandbox Code Playgroud)

我现在还想将两个可能的图标之一添加到网格中的每一行,这也取决于 的某些属性的值Item。请注意,现在与任何项目的属性都有直接对应关系,因此我的网格中不能有额外的列来保存图标。所以我想我必须向已经存在的单元格添加一个图标,或者动态生成一个适当的列。有任何想法吗 ?

Rez*_*aei 6

在 DataGridView 单元格中的文本旁边显示图像

您需要自己处理CellPainting事件DataGridView并绘制单元格。

例子

此示例演示如何在 的绑定列中绘制图像DataGridView,以便该列显示绑定数据以及图像。例如,在这里我决定为负数绘制一个红色图标,为零数绘制一个银色图标,为正数绘制一个绿色图标:

在此输入图像描述

为此,请定义一些变量以保留对图像的引用。我们将使用此变量来渲染图像,并在不再需要图像时处理图像:

Image zero, negative, positive;
Run Code Online (Sandbox Code Playgroud)

处理Load来自文件、资源或存储图像的任何位置的表单和图像事件,并分配给这些变量。设置数据绑定。为要在其中绘制图标的单元格设置合适的左内边距:

private void Form1_Load(object sender, EventArgs e)
{
    var list = new[] {
        new { C1 = "A", C2 = -2 },
        new { C1 = "B", C2 = -1 },
        new { C1 = "C", C2 = 0 },
        new { C1 = "D", C2 = 1 },
        new { C1 = "E", C2 = 2 },
    }.ToList();
    dataGridView1.DataSource = list;

    zero = new Bitmap(16, 16);
    using (var g = Graphics.FromImage(zero))
        g.Clear(Color.Silver);
    negative = new Bitmap(16, 16);
    using (var g = Graphics.FromImage(negative))
        g.Clear(Color.Red);
    positive = new Bitmap(16, 16);
    using (var g = Graphics.FromImage(positive))
        g.Clear(Color.Green);

    //Set padding to have enough room to draw image
    dataGridView1.Columns[1].DefaultCellStyle.Padding = new Padding(18, 0, 0, 0);
}
Run Code Online (Sandbox Code Playgroud)

处理CellPainting的事件DataGridView并渲染所需列的单元格内容和图像:

private void DataGridView1_CellPainting(object sender, DataGridViewCellPaintingEventArgs e)
{
    //We don't need custom paint for row header or column header
    if (e.RowIndex < 0 || e.ColumnIndex != 1) return;

    //We don't need custom paint for null value
    if (e.Value == null || e.Value == DBNull.Value) return;

    //Choose image based on value
    Image img = zero;
    if ((int)e.Value < 0) img = negative;
    else if ((int)e.Value > 0) img = positive;

    //Paint cell
    e.Paint(e.ClipBounds, DataGridViewPaintParts.All);
    e.Graphics.DrawImage(img, e.CellBounds.Left + 1, e.CellBounds.Top + 1,
        16, e.CellBounds.Height - 3);

    //Prevent default paint
    e.Handled = true;
}
Run Code Online (Sandbox Code Playgroud)

处理FormClosing事件以处理图像:

private void Form1_FormClosing(object sender, FormClosingEventArgs e)
{
    //Dispose images
    if (zero != null) zero.Dispose();
    if (negative != null) negative.Dispose();
    if (positive != null) positive.Dispose();
}
Run Code Online (Sandbox Code Playgroud)