首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > .NET > C# >

改变DataGridView某个单元格的色彩

2012-06-30 
改变DataGridView某个单元格的颜色C# codeprivate void dataGridView1_CellParsing(object sender, DataGr

改变DataGridView某个单元格的颜色

C# code
     private void dataGridView1_CellParsing(object sender, DataGridViewCellParsingEventArgs e)        {                          if (e.ColumnIndex == 1)                {                    if (float.Parse(e.Value.ToString()) > 200)                    {                        dataGridView1.Rows[e.RowIndex].Cells[e.ColumnIndex].Style.ForeColor = Color.Red;                        MessageBox.Show("请注意,您输入的值超超过标准值!200 ", "警告", MessageBoxButtons.OK, MessageBoxIcon.Warning);                    }                }        }

这个地方为什么先弹出对话框,后变颜色,
如何让它先变颜色,后弹出对话框呢

[解决办法]
因为这个时候你看到的单元格其实是被内部编辑控件覆盖的,要连这个控件的颜色也改掉

C# code
private void dataGridView1_CellParsing(object sender, DataGridViewCellParsingEventArgs e)        {                          if (e.ColumnIndex == 1)                {                    if (float.Parse(e.Value.ToString()) > 200)                    {                        dataGridView1.Rows[e.RowIndex].Cells[e.ColumnIndex].Style.ForeColor = Color.Red;                        dataGridView1.EditingControl.ForeColor = Color.Red;                        MessageBox.Show("请注意,您输入的值超超过标准值!200 ", "警告", MessageBoxButtons.OK, MessageBoxIcon.Warning);                    }                }        } 

热点排行