简体   繁体   English

如何重置或刷新DataGridView单元的背景色?

[英]how can I reset or refresh DataGridView Cell back color?

I am facing problem regarding DataGridView cell back color. 我面临有关DataGridView单元背景色的问题。

Firstly I m changing datagridview2's specific cell's back color. 首先,我要更改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;
            }
        }    

I need to reset the back color according to new value entered in datagridview1. 我需要根据在datagridview1中输入的新值重置背景色。 here is my code. 这是我的代码。

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;
                }
            }

        }
    }    

But There is no viewable changes happen when I entered value in datagridview1. 但是,当我在datagridview1中输入值时,没有可见的变化发生。 It works only for new cell but does not change for existing colored cell to default. 它仅适用于新单元格,不适用于现有的彩色单元格更改为默认值。 any idea why? 知道为什么吗?

I got the same Problem about one week ago. 大约一周前,我遇到了同样的问题。 I solved it with just 2 rows of programmcode. 我仅用两行programmcode解决了它。 Hope this will help you to! 希望这对您有所帮助! :

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何将DataGridView单元格的字体设为特定颜色? - How can I make a DataGridView cell's font a particular color? 如何按单元格颜色对datagridview进行排序? - how do i sort datagridview by cell color? 如何以编程方式更改空的DataGridView单元的背景色? - How to programmatically change empty DataGridView Cell back color? 对于使用“ DataGridView”的Windows窗体应用程序,如何检查数据源中的值并更改单独单元格的颜色? - For a Windows Forms Application using `DataGridView`, how can I check a value in my DataSource and change the color of a separate cell? 如何更改 datagridview 特定的复选框单元格颜色并根据数据库值进行检查? - How can i change datagridview specific checkbox cell color and check based on database value? 我如何更改单元格datagridview的值 - how can i change the value of a cell datagridview 如何在datagridview中为单元格创建页脚 - How can I create a footer for cell in datagridview 如何刷新我的 DataGridView? 我正在尝试刷新 datagridview 中的列表,但我不能 - How to refresh my DataGridView? I am trying to refresh the list in datagridview but i can't 如何更改DataGridView背景颜色/单元格颜色 - How to change DataGridView Background color/ Cell Color 当我单击具有不同值的另一个单元格时,背面颜色在DataGridView文本框中变为黑色 - When i click another cell with different value, the back color become black in DataGridView textbox
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM