[英]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.