如何重置或刷新DataGridView Cell背面颜色?

时间:2014-10-10 12:06:48

标签: c# datagridview

我遇到有关DataGridView单元格背面颜色的问题。

首先,我改变了datagridview2特定单元格的背景颜色。

int cnt = dataGridView1.Rows.Count;
        for (int i = 0; i < cnt; i++)
        {
            string starting = dataGridView1.Rows[i].Cells[9].Value.ToString();
            string startings = Convert.ToDateTime(starting.ToString()).ToString("dd MMM yyyy");
            string ending = dataGridView1.Rows[i].Cells[10].Value.ToString();
            string endings = Convert.ToDateTime(ending.ToString()).ToString("dd MMM yyyy");

            TimeSpan ts = Convert.ToDateTime(ending.ToString()) - Convert.ToDateTime(starting.ToString());
            double diff = ts.TotalDays + 1;
            int dif = Convert.ToInt32(diff);
            for (int d = 0; d < dif; d++)
            {
                string str = Convert.ToDateTime(starting.ToString()).AddDays(d).ToString("dd MMM yyyy");
                dataGridView2.Rows[i].Cells[str].Style.BackColor = Color.Olive;
            }
        }    

我需要根据datagridview1中输入的新值重置背景颜色。这是我的代码。

Private void dataGridView1_CellEndEdit(object sender, DataGridViewCellEventArgs e)
    {
        if (e.ColumnIndex == 7 || e.ColumnIndex == 8 || e.ColumnIndex == 9 || e.ColumnIndex == 10)
        {
            int prd_per_d = Convert.ToInt32(dataGridView1.CurrentRow.Cells[7].Value);
            int qty=Convert.ToInt32(dataGridView1.CurrentRow.Cells[6].Value);
            dataGridView1.CurrentRow.Cells[8].Value = qty / prd_per_d + 5;

            DateTime sdt = Convert.ToDateTime(dataGridView1.CurrentRow.Cells[9].Value);
            int req_day = Convert.ToInt32(dataGridView1.CurrentRow.Cells[8].Value);
            dataGridView1.CurrentRow.Cells[10].Value = sdt.AddDays(req_day);

            int cnt = dataGridView1.Rows.Count;
            for (int i = 0; i < cnt; i++)
            {
                string starting = dataGridView1.Rows[i].Cells[9].Value.ToString();
                string startings = Convert.ToDateTime(starting.ToString()).ToString("dd MMM yyyy");
                string ending = dataGridView1.Rows[i].Cells[10].Value.ToString();
                string endings = Convert.ToDateTime(ending.ToString()).ToString("dd MMM yyyy");

                TimeSpan ts = Convert.ToDateTime(ending.ToString()) - Convert.ToDateTime(starting.ToString());
                double diff = ts.TotalDays + 1;
                int dif = Convert.ToInt32(diff);

                for (int d = 0; d < dif; d++)
                {
                    string str = Convert.ToDateTime(starting.ToString()).AddDays(d).ToString("dd MMM yyyy");

                    dataGridView2.Rows[i].Cells[str].Style.BackColor = Color.Olive;
                }
            }

        }
    }    

但是当我在datagridview1中输入值时,没有发生可见的变化。它仅适用于新单元格,但不会将现有彩色单元格更改为默认值。 知道为什么吗?

1 个答案:

答案 0 :(得分:0)

一周前我遇到了同样的问题。我用2行programmcode解决了这个问题。希望这会对你有所帮助! :

        this.dataGridView1.RowsDefaultCellStyle.BackColor = Color.Bisque;
        this.dataGridView1.AlternatingRowsDefaultCellStyle.BackColor = Color.Beige;