DataGridView的行的背景色是不会改变背景色、DataGridView

2023-09-03 13:14:53 作者:素颜白裙也倾城

我想改变DGV的行的基础上,在负荷,即使在Windows窗体特定条件的背景色。但我看不出颜色的任何DGV的行任何改变。谁能告诉我我该怎么解决这个问题?

 私人无效frmSecondaryPumps_Load(对象发件人,EventArgs的)
{
            尝试
            {
                数据表DT = DeviceData.BindData(SECONDARY_PUMPS.ToUpper());
                dataGridView1.DataSource = DT;

                的foreach(的DataGridViewRow行dataGridView1.Rows)
                {
                    的foreach(在dataGridView1.Columns的DataGridViewColumn列)
                    {
                        如果(row.Cells [column.Name]!= NULL)
                        {
                            如果(row.Cells [column.Name] .Value.ToString()==开)
                                row.DefaultCellStyle.BackColor = System.Drawing.Color.Green;

                            如果(row.Cells [column.Name] .Value.ToString()==关)
                                row.DefaultCellStyle.BackColor = System.Drawing.Color.Red;
                        }
                    }
                }

                dataGridView1.Refresh();
            }
            赶上(例外错误)
            {
                的MessageBox.show(err.Message);
            }
        }
 

解决方案

我觉得最好的地方是设置背景色中的的DataGridView ,东西就这些行。

 私人无效grid1_CellFormatting(对象发件人,DataGridViewCellFormattingEventArgs E)
{
    的DataGridViewRow行= grid1.Rows [e.RowIndex]; //得到你需要的指数
    //检查下你的具体列单元格的值,然后你可以切换你的颜色
    row.DefaultCellStyle.BackColor = Color.Green;
}
 

急求对dataGridView数据进行修改和增加

I want to change the background color of the DGV's row based on particular condition at load even in Windows Form. But I can't see any change of color to any DGV's row. Could anyone tell me how can I solve this problem?

private void frmSecondaryPumps_Load(object sender, EventArgs e)
{
            try
            {
                DataTable dt = DeviceData.BindData("SECONDARY_PUMPS".ToUpper());
                dataGridView1.DataSource = dt;

                foreach (DataGridViewRow row in dataGridView1.Rows)
                {
                    foreach (DataGridViewColumn column in dataGridView1.Columns)
                    {
                        if (row.Cells[column.Name] != null)
                        {
                            if (row.Cells[column.Name].Value.ToString() == "ON")
                                row.DefaultCellStyle.BackColor = System.Drawing.Color.Green;

                            if (row.Cells[column.Name].Value.ToString() == "OFF")
                                row.DefaultCellStyle.BackColor = System.Drawing.Color.Red;
                        }
                    }
                }

                dataGridView1.Refresh();
            }
            catch (Exception err)
            {
                MessageBox.Show(err.Message);
            }
        }

解决方案

I think the best place would be to set the BackColor in the CellFormatting event of the DataGridView, something on these lines.

private void grid1_CellFormatting(object sender, DataGridViewCellFormattingEventArgs e)
{
    DataGridViewRow row = grid1.Rows[e.RowIndex];// get you required index
    // check the cell value under your specific column and then you can toggle your colors
    row.DefaultCellStyle.BackColor = Color.Green;
}