簡體   English   中英

如何根據 Combobox 的值更改 DataGridView 單元格顏色?

[英]How to change DataGridView cell color based on value of Combobox?

我有一個數據網格視圖,如下所示:

在此處輸入圖片說明

我想:

  • 表單加載時,如果Gender列的值為Male,則Name列對應的顏色單元格將為White

  • 如果更改Gender : Male → Female 列的值,則Name列的顏色單元格為深灰色,否則,如果更改Gender : Female → Male 列的值,則Name列的顏色單元格為白色

我試過了,但我無法做到:

    private void dataGridView_CurrentCellDirtyStateChanged(object sender, EventArgs e)
    {
        DataGridView dgv = sender as DataGridView;
        DataGridViewCell cell = dgv.CurrentCell;

        if (dgv.Rows[cell.RowIndex].Cells["Gender"].Value.ToString().Trim() == "Male")
        {
            // Male
            dgv.Rows[cell.RowIndex].DefaultCellStyle.BackColor = Color.White;
        }
        else
        {
            // Female
            dgv.Rows[cell.RowIndex].DefaultCellStyle.BackColor = Color.DarkGray;
        }
    }

或者:

    private void dataGridView_CellFormatting(object sender, DataGridViewCellFormattingEventArgs e)
    {
        DataGridView dgv = sender as DataGridView;

        if (dgv.Columns[e.ColumnIndex].Name.Equals("Gender"))
        {
            if (e.Value != null && e.Value.ToString().Trim() == "Male")
            {
                e.CellStyle.BackColor = Color.White;
            }
            else
            {
                e.CellStyle.BackColor = Color.DarkGray;
            }
        }

        //if (dgv.Rows[e.RowIndex].Cells["Gender"].Value.ToString().Trim() == "Male")
        //{
        //    e.CellStyle.BackColor = Color.White;
        //}
        //else
        //{
        //    e.CellStyle.BackColor = Color.DarkGray;
        //}
    }

關於這些的任何提示都會有很大幫助。 提前致謝。

要更改背景顏色,您必須訂閱CellFormatting事件。 然后將此代碼添加到事件處理程序:

private void dataGridView_CellFormatting(object sender, DataGridViewCellFormattingEventArgs e)
{
    DataGridView dgv = sender as DataGridView;

    if (dgv.Columns[e.ColumnIndex].Name.Equals("Gender"))
    {
        if (e.Value != null && e.Value.ToString().Trim() == "Male")
        {
            dgv.Rows[e.RowIndex].Cells["name"].Style.BackColor = Color.White;
        }
        else
        {
            dgv.Rows[e.RowIndex].Cells["name"].Style.BackColor = Color.DarkGray;
        }
    }

}

要在DataGridViewComboBoxCell選擇新值時進行驗證,請訂閱CurrentCellDirtyStateChanged事件並在其處理程序中嘗試以下代碼:

private void dataGridView_CurrentCellDirtyStateChanged(object sender, EventArgs e)
{
    DataGridView dgv = sender as DataGridView;
    DataGridViewCell cell = dgv.CurrentCell;
    if (cell is DataGridViewComboBoxCell)
    {
        dgv.CommitEdit(DataGridViewDataErrorContexts.Commit);
        dgv.EndEdit();
    }
}

只是為了向您展示一個工作示例,我正在更改 ForeColor 而不是 Back,但概念是相同的。 您需要應用默認值:

this.dgvUsers.DefaultCellStyle.ForeColor = Color.Black;

然后根據您的觸發器是什么,您需要分配一個處理程序:

dgvUsers.CellFormatting += new System.Windows.Forms.DataGridViewCellFormattingEventHandler(this.dgvUsers_CellFormatting);
this.Controls.Add(dgvUsers);

然后為了處理事件,它看起來像這樣:

// Handle user going inactive or being reactivated
private void dgvUsers_CellFormatting(object sender, DataGridViewCellFormattingEventArgs e)
{
    DataRowView row = dgvUsers.Rows[e.RowIndex].DataBoundItem as DataRowView;
    if (row != null && row.Row.ItemArray[7].Equals("N"))
    {
        e.CellStyle.ForeColor = Color.Gray;
    }
    else
    {
        e.CellStyle.ForeColor = Color.Black;
    }
}

與接受的答案不同,這使用樣式必須在一個地方更改定義。

暫無
暫無

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

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