I have the following code :
private void dgvStatus_DataBindingComplete(object sender, DataGridViewBindingCompleteEventArgs e)
{
foreach (DataGridViewRow
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;
}
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
}
}
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;
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;
}
}
}
}
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.
dataGridView1[row, col].Style.BackColor = System.Drawing.Color.Red;