日期:2014-05-18  浏览次数:21254 次

关于改变DataGridView的行背景色(WinForm)
关于改变DataGridView的行背景色(WinForm)

如何根据数据源的某一列改变DataGridView的行背景色。
(这一列并不在界面上显示。)

------解决方案--------------------
private void dataGridView1_CellValueChanged(object sender, DataGridViewCellEventArgs e)
{
//若本行不为空行并且无物料名称则显示灰色
if (dataGridView1.Rows[e.RowIndex].Cell["abcd"].Value=="Text1")
dataGridView1.Rows[e.RowIndex].DefaultCellStyle.BackColor = Color.Gray;
else
dataGridView1.Rows[e.RowIndex].DefaultCellStyle.BackColor = Color.White;
}
------解决方案--------------------
不显示,没什么关系
C# code

private   void   dataGridView1_RowPrePaint(object   sender,   DataGridViewRowPrePaintEventArgs   e)
                {
                        if   (e.RowIndex   > =   dataGridView1.Rows.Count   -   1)
                                return;
                        DataGridViewRow   dgr   =   dataGridView1.Rows[e.RowIndex];
                        try
                        {
                                if   (dgr.Cells[ "列名 "].Value.ToString()   ==   "比较值 ")
                                {
                                        dgr.DefaultCellStyle.ForeColor   =   设置的颜色;
                                }
                        }
                        catch   (Exception   ex)
                        {
                                MessageBox.Show(ex.Message);
                        }
                }

------解决方案--------------------
C# code
你把那列掩藏不就可以了,,显不显示并没关系   

     private void dataGridView1_CellFormatting(object sender, DataGridViewCellFormattingEventArgs e)
        {
            try
            {
                if (dataGridView1.Rows[e.RowIndex].Cells[0].Value.ToString()=="2")
                {
                        e.CellStyle.BackColor = Color.Red;
                }
            }

------解决方案--------------------
探讨
引用:

private void dataGridView1_CellValueChanged(object sender, DataGridViewCellEventArgs e)
{
//若本行不为空行并且无物料名称则显示灰色
if (dataGridView1.Rows[e.RowIndex].Cell["abcd"].Value=="Text1")
dataG……

------解决方案--------------------
探讨
引用:

引用:

我明白了,就是说没什么办法从数据源中取数据了,只能绑定到Grid上,然后设置其Visible=false这一招了。

如果是 DataSource 赋值的,在 RowPrePaint 事件里转换 DataSource 取出不显示列的值


不好意思,不太明白。
“在 RowPrePaint 事件里转换 DataSource 取……