简体   繁体   English

DataGridView行背景颜色重置

[英]DataGridView Row Background Color resetting

I want to color certain rows in my dataGridview and do so succesfully with the below code, however once I add more rows to my datagridview, they all reset to white. 我想为dataGridview中的某些行着色,并使用下面的代码成功完成此操作,但是,一旦我向datagridview中添加了更多行,它们都将重置为白色。

This code checks all the rows from a previous collection that made it in the datagridview and colors them lightblue. 此代码检查在datagridview中使其成为先前集合的所有行,并将它们的颜色显示为浅蓝色。 These rows are matched by sets, and the background color indicates what rows are together and every set it alternates between white and blue. 这些行按组进行匹配,背景颜色指示哪些行在一起以及每组在白色和蓝色之间交替显示。

Adding another set of rows to the datagridview resets the background color to white for all previous rows in the datagridview and I have no clue why, should I overload a certain event? 向datagridview中添加另一组行会将datagridview中所有先前行的背景颜色重置为白色,我不知道为什么,是否应该重载某个事件? I'm at a loss. 我很茫然。

This is the only instance of me changing the color of anything and I have no events of any kind overwritten. 这是我更改任何颜色的唯一实例,并且没有任何类型的事件被覆盖。

List<DataRow> dgvr = new List<DataRow>();

foreach(DataGridViewRow row in dataTaken1.SelectedRows)
{
    // Adding selected rows from the first datagridview to the set
}

foreach (DataGridViewRow row in dataTaken2.SelectedRows)
{
    // Adding selected rows from the second datagridview to the set
}

foreach(DataRow row in dgvr)
{
    dataTable1.Rows.Add(row); // This is the dataTable that is bound to the datagridview
    dataTable1.AcceptChanges();
    dataGridView1.Update();
}

if (bordercolor % 2 == 1)
{
    foreach (DataRow row in dgvr)
    {
        dataGridView1.Rows[dataTable1.Rows.IndexOf(row)].DefaultCellStyle.BackColor = Color.LightBlue;
    }
}

bordercolor++; // ensures that the next set has a different background color

EDIT: Here is a picture showing how i want my rows to show, alternating colors between sets, for example, grey being odd numbered sets color and light blue being even numbered sets color. 编辑:这是一张图片,显示我希望我的行显示的方式,各组之间交替显示颜色,例如,灰色为奇数组颜色,浅蓝色为偶数组颜色。

在此处输入图片说明

You may want to check this property instead of doing it manually: 您可能需要检查此属性,而不是手动进行操作:

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM