【问题标题】:How to render a data-bound WinForms DataGridView Column with an Icon?如何使用图标呈现数据绑定的 WinForms DataGridView 列?
【发布时间】:2019-02-04 14:26:20
【问题描述】:

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

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

// bind controller to model
Items = new BindingList<Item>(model.Items);

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

myDataGridView.CellFormatting += new DataGridViewCellFormattingEventHandler(myontroller.HandleCellFormatting);

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

【问题讨论】:

  • 如果不能直接编辑 Item 类,能否为它创建一个扩展方法来决定显示哪个图标?
  • 可能值得一提的是,在 MVVM 开发中,ViewModel 的存在是为了在您的模型(Item)和 View 的绑定之间进行这种转换。
  • @RobinBennett:我既可以添加扩展方法,也可以让 ViewModel(控制器)进行转换,但是仍然存在将图标放在 dataGridView 中的位置的问题,因为它已经生成拥有与模型拥有的属性一样多的列。
  • 抱歉,我不是很清楚(如果您需要属性,扩展方法也无济于事)。在这种情况下,ViewModel 将是一个看起来像 Item 的对象,除了具有额外的属性。它可能会从 Item 继承,并有一个构造函数来获取 Item 并将其存储在私有属性中。
  • 听起来不错。我试试看……

标签: c# .net winforms datagridview


【解决方案1】:

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

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

示例

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

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

Image zero, negative, positive;

处理来自文件、资源或您存储图像的任何位置的表单和图像的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);
}

处理DataGridViewCellPainting 事件并为您想要的列呈现单元格内容和图像:

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;
}

处理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();
}

【讨论】:

  • CellPainting 事件中添加了 cmets,因此您可以根据自己的要求进行更改。
猜你喜欢
  • 2019-07-21
  • 1970-01-01
  • 2021-04-15
  • 2012-11-02
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-02-12
  • 1970-01-01
相关资源
最近更新 更多