(DataGridView中+绑定)如何彩线根据对象的绑定? [英] (DataGridView + Binding)How to color line depending of the object binded?

查看:96
本文介绍了(DataGridView中+绑定)如何彩线根据对象的绑定?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想补充一个背景色为根据绑定的对象的属性的特定行。

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

该解决方案我已经(和它的作品)是使用事件 DataBindingComplete ,但我不认为这是最好的解决办法。

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

下面是事件:

    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?

推荐答案

您还可以附加一个事件处理程序RowPostPaint:

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

这篇关于(DataGridView中+绑定)如何彩线根据对象的绑定?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆