DataGridView changing cell background color

后端 未结 10 1631
没有蜡笔的小新
没有蜡笔的小新 2020-11-30 10:20

I have the following code :

private void dgvStatus_DataBindingComplete(object sender, DataGridViewBindingCompleteEventArgs e)
{
    foreach (DataGridViewRow          


        
相关标签:
10条回答
  • 2020-11-30 10:32

    You can use the VisibleChanged event handler.

    private void DataGridView1_VisibleChanged(object sender, System.EventArgs e)
    {
        var grid = sender as DataGridView;
        grid.Rows[0].Cells[0].Style.BackColor = Color.Yellow;
    }
    
    0 讨论(0)
  • 2020-11-30 10:36

    try the following (in RowDataBound method of GridView):

    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
        }
    }
    
    0 讨论(0)
  • 2020-11-30 10:43

    Simply create a new DataGridViewCellStyle object, set its back color and then assign the cell's style to it:

        DataGridViewCellStyle style = new DataGridViewCellStyle();
        style.BackColor = Color.FromArgb(((GesTest.dsEssais.FMstatusAnomalieRow)row.DataBoundItem).iColor);
        style.ForeColor = Color.Black;
        row.Cells[color.Index].Style = style;
    
    0 讨论(0)
  • 2020-11-30 10:50
    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;
                }
            }
        }
    }
    
    0 讨论(0)
  • 2020-11-30 10:52

    Similar as shown and mentioned:

    Notes: Take into consideration that Cells will change their color (only) after the DataGridView Control is Visible. Therefore one practical solution would be using the:

    VisibleChanged Event

    In case you wish to keep your style when creating new Rows; also subscribe the:

    RowsAdded Event


    Example bellow:

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


    Finally: Just call the DataGridView_Configuration() (method)
    i.e: Form Load Event.

    0 讨论(0)
  • 2020-11-30 10:54
    dataGridView1[row, col].Style.BackColor = System.Drawing.Color.Red;
    
    0 讨论(0)
提交回复
热议问题