繁体   English   中英

DataGridView 更改单元格背景颜色

[英]DataGridView changing cell background color

我有以下代码:

private void dgvStatus_DataBindingComplete(object sender, DataGridViewBindingCompleteEventArgs e)
{
    foreach (DataGridViewRow row in dgvStatus.Rows)
    {
        row.Cells[color.Index].Style.BackColor = Color.FromArgb(((GesTest.dsEssais.FMstatusAnomalieRow)row.DataBoundItem).iColor);
    }
}

我正在尝试从背景颜色列中设置每个单元格的背景颜色。 这不起作用颜色永远不会改变。 知道为什么吗?

我一直在环顾四周,但没有发现任何有用的东西

只需创建一个新的DataGridViewCellStyle对象,设置其背景颜色,然后将单元格的样式分配给它:

    DataGridViewCellStyle style = new DataGridViewCellStyle();
    style.BackColor = Color.FromArgb(((GesTest.dsEssais.FMstatusAnomalieRow)row.DataBoundItem).iColor);
    style.ForeColor = Color.Black;
    row.Cells[color.Index].Style = style;

我终于设法让它工作。 这里的代码:

private void dgvStatus_CellFormatting(object sender, DataGridViewCellFormattingEventArgs e)
{
    if (e.ColumnIndex != color.Index)
        return;

    e.CellStyle.BackColor = Color.FromArgb(int.Parse(((DataRowView)dgvStatus.Rows[e.RowIndex].DataBoundItem).Row[4].ToString()));
}

如果有人知道更好地做到这一点,请不要犹豫张贴。 我愿意接受建议

如果您仍然对为什么这对您不起作用一开始感兴趣:

您看不到对单元格样式所做的更改的原因是因为您显示表单之前进行了这些更改,因此它们被忽略。

在此处建议的事件中更改单元格样式可以完成这项工作,但它们会被多次调用,导致您的样式更改发生的次数比您希望的多,因此效率不高。

要解决此问题,请在代码中显示表单的点之后更改样式,或订阅 Shown 事件,并将更改放在那里(此事件的调用次数明显少于建议的其他事件)。

dataGridView1.Rows[i].Cells[7].Style.BackColor = Color.LightGreen;
int rowscount = dataGridView1.Rows.Count;         

for (int i = 0; i < rowscount; i++)
{            
    if (!(dataGridView1.Rows[i].Cells[8].Value == null))
    {
        dataGridView1.Rows[i].Cells[8].Style.BackColor = Color.LightGoldenrodYellow;
    }
}

与所示和提到的类似:

注意:考虑到单元格将在 DataGridView 控件可见后(仅)更改其颜色。 因此,一种实用的解决方案是使用:

VisibleChanged 事件

如果您希望在创建新行时保持您的风格; 还订阅:

RowsAdded 事件


示例如下:

///<summary> Instantiate the DataGridView Control. </summary>
private DataGridView dgView = new DataGridView;

///<summary> Method to configure DataGridView Control. </summary>
private void DataGridView_Configuration()
{
    // In this case the method just contains the VisibleChanged event subscription.

    dgView.VisibleChanged += DgView_VisibleChanged;

    // Uncomment line bellow in case you want to keep the style when creating new rows.
    // dgView.RowsAdded += DgView_RowsAdded;
}

///<summary> The actual Method that will re-design (Paint) DataGridView Cells. </summary>
 private void DataGridView_PaintCells()
 {
     int nrRows = dgView.Rows.Count;
     int nrColumns = dgView.Columns.Count;
     Color green = Color.LimeGreen;

     // Iterate over the total number of Rows
     for (int row = 0; row < nrRows; row++)
     {
         // Iterate over the total number of Columns
         for (int col = 0; col < nrColumns; col++) 
         {
             // Paint cell location (column, row)
             dgView[col, row].Style.BackColor = green;
         }
     }
 }

///<summary> The DataGridView VisibleChanged Event. </summary>
private void DataGridView_VisibleChanged(object sender, EventArgs e)
{
    DataGridView_PaintCells();
}

/// <summary> Occurrs when a new Row is Created. </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void DataGridView_RowsAdded(object sender, DataGridViewRowsAddedEventArgs e)
{ 
    DataGridView_PaintCells(); 
}


最后:只需调用 DataGridView_Configuration()(方法)
即:表单加载事件。

尝试以下操作(在 GridView 的 RowDataBound 方法中):

protected void GridViewUsers_RowDataBound(object sender, GridViewRowEventArgs e)
{
    // this will only change the rows backgound not the column header 

    if (e.Row.RowType == DataControlRowType.DataRow)
    {
        e.Row.Cells[0].BackColor = System.Drawing.Color.LightCyan; //first col
        e.Row.Cells[1].BackColor = System.Drawing.Color.Black; // second col
    }
}
protected void grdDataListeDetay_RowDataBound(object sender, GridViewRowEventArgs e)
{
    if (e.Row.RowType == DataControlRowType.DataRow)
    {
        if (e.Row.Cells[3].Text != "0")
        {
            for (int i = 0; i <= e.Row.Cells.Count - 1; i++)
            {
                e.Row.Cells[i].BackColor = System.Drawing.Color.Beige;
            }
        }
    }
}

您可以使用VisibleChanged事件处理程序。

private void DataGridView1_VisibleChanged(object sender, System.EventArgs e)
{
    var grid = sender as DataGridView;
    grid.Rows[0].Cells[0].Style.BackColor = Color.Yellow;
}
dataGridView1[row, col].Style.BackColor = System.Drawing.Color.Red;

暂无
暂无

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

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