簡體   English   中英

DataGridView行背景顏色重置

[英]DataGridView Row Background Color resetting

我想為dataGridview中的某些行着色,並使用下面的代碼成功完成此操作,但是,一旦我向datagridview中添加了更多行,它們都將重置為白色。

此代碼檢查在datagridview中使其成為先前集合的所有行,並將它們的顏色顯示為淺藍色。 這些行按組進行匹配,背景顏色指示哪些行在一起以及每組在白色和藍色之間交替顯示。

向datagridview中添加另一組行會將datagridview中所有先前行的背景顏色重置為白色,我不知道為什么,是否應該重載某個事件? 我很茫然。

這是我更改任何顏色的唯一實例,並且沒有任何類型的事件被覆蓋。

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

編輯:這是一張圖片,顯示我希望我的行顯示的方式,各組之間交替顯示顏色,例如,灰色為奇數組顏色,淺藍色為偶數組顏色。

在此處輸入圖片說明

您可能需要檢查此屬性,而不是手動進行操作:

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM