(DataGridView + 绑定)如何根据绑定的对象对线条进行着色?

发布于 2024-07-09 09:59:05 字数 545 浏览 8 评论 0原文

我想根据绑定对象的属性为特定行添加背景色。

我拥有的解决方案(并且有效)是使用事件 DataBindingComplete ,但我认为这不是最好的解决方案。

事件如下:

    private void myGrid_DataBindingComplete(object sender, DataGridViewBindingCompleteEventArgs e)
    {

        for (int i = 0; i < this.myGrid.Rows.Count; i++)
        {
            if((this.myGrid.Rows[i].DataBoundItem as MyObject).Special)
            {
                this.myGrid.Rows[i].DefaultCellStyle.BackColor = Color.FromArgb(240, 128, 128);
            }
        }
    }

还有其他更好的选择吗?

I would like to add a backcolor for specific line depending of a Property of the object binded.

The solution I have (and it works) is to use the Event DataBindingComplete but I do not think it's the best solution.

Here is the event:

    private void myGrid_DataBindingComplete(object sender, DataGridViewBindingCompleteEventArgs e)
    {

        for (int i = 0; i < this.myGrid.Rows.Count; i++)
        {
            if((this.myGrid.Rows[i].DataBoundItem as MyObject).Special)
            {
                this.myGrid.Rows[i].DefaultCellStyle.BackColor = Color.FromArgb(240, 128, 128);
            }
        }
    }

Any other option that would be better?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

孤蝉 2024-07-16 09:59:05

您还可以将事件处理程序附加到 RowPostPaint:

dataGridView1.RowPostPaint += OnRowPostPaint;

void OnRowPostPaint(object sender, DataGridViewRowPostPaintEventArgs e)
{
    MyObject value = (MyObject) dataGridView1.Rows[e.RowIndex].DataBoundItem;
    DataGridViewCellStyle style = dataGridView1.Rows[e.RowIndex].DefaultCellStyle;

    // Do whatever you want with style and value
    ....
}

You can also attach an event handler to RowPostPaint:

dataGridView1.RowPostPaint += OnRowPostPaint;

void OnRowPostPaint(object sender, DataGridViewRowPostPaintEventArgs e)
{
    MyObject value = (MyObject) dataGridView1.Rows[e.RowIndex].DataBoundItem;
    DataGridViewCellStyle style = dataGridView1.Rows[e.RowIndex].DefaultCellStyle;

    // Do whatever you want with style and value
    ....
}
冷清清 2024-07-16 09:59:05

我实际上不太使用 WinForms,但在 ASP 中,您会使用“ItemDataBound”方法。 Windows 窗体中是否有类似的 DataGrid?

如果是这样,在该方法中,事件参数将包含数据绑定的项目以及 DataGrid 行。 所以一般代码看起来像这样(语法可能是关闭的):

if(((MyObject)e.Item.DataItem).Special)
   e.Item.DefaultCellStyle.BackColor = Color.FromArgb(240, 128, 128);

I don't really work with WinForms that much, but in ASP you would use the 'ItemDataBound' method. Is there something similar in windows forms for a DataGrid?

If so, in that method, the event arguments would contain the item that was databound, along with the DataGrid row. So the general code would look something like this (syntax is probably off):

if(((MyObject)e.Item.DataItem).Special)
   e.Item.DefaultCellStyle.BackColor = Color.FromArgb(240, 128, 128);
又爬满兰若 2024-07-16 09:59:05

我建议一些事情:

  • 看看在 _OnRowDatabound 修改你的行
  • 不要在你的代码中设置颜色! 这将是一个很大的错误。 使用 attribute 属性并设置 cssclass。 对仍在这样做的人摇动手指。

如果您在实施过程中遇到困难,请告诉我,我将发布一个片段。

I would suggest a few things:

  • look at modifying your rows at _OnRowDatabound
  • Do not set color in your code!!! This would be a big mistake. Use the attributes property and set the cssclass. Wag of the finger to people still doing this.

Let me know if you struggle with the implementation and i'll post a snippet.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文