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

改变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);
                    }
                }
        }