20

我有以下代码:

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);
    }
}

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

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

4

10 回答 10

32

只需创建一个新的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;
于 2013-04-19T13:36:50.783 回答
17

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

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()));
}

如果有人知道更好的方法,请不要犹豫发布它。我愿意接受建议

于 2013-04-19T14:08:06.853 回答
11

如果您仍然对为什么这最初对您不起作用感兴趣:

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

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

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

于 2013-10-02T11:54:24.603 回答
8
dataGridView1.Rows[i].Cells[7].Style.BackColor = Color.LightGreen;
于 2016-02-17T15:26:42.803 回答
4
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;
    }
}
于 2016-03-08T20:08:30.117 回答
1

尝试以下方法(在 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
    }
}
于 2013-08-22T18:45:44.633 回答
1

类似于所示和提到的:

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

VisibleChanged 事件

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

行添加事件


示例如下:

///<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() (方法),
即:表单加载事件。

于 2020-01-23T13:38:58.623 回答
0
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;
            }
        }
    }
}
于 2013-09-29T13:11:16.470 回答
0

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

private void DataGridView1_VisibleChanged(object sender, System.EventArgs e)
{
    var grid = sender as DataGridView;
    grid.Rows[0].Cells[0].Style.BackColor = Color.Yellow;
}
于 2019-10-05T15:25:48.240 回答
-1
dataGridView1[row, col].Style.BackColor = System.Drawing.Color.Red;
于 2018-08-20T05:21:09.130 回答