C# .NET - Change color of particular row in DataGridView + Winform

Asked By Abhi Rana
03-Sep-09 01:59 AM

private void dataGridView1_DataBindingComplete(object sender, DataGridViewBindingCompleteEventArgs e)

{

try

{

for (int count = 0; count < dataGridView1.Rows.Count; count++)

{

if (Convert.ToInt32(dataGridView1.Rows[count].Cells[5].Value) !=0)

{

dataGridView1.Rows[count].Cells[0].ReadOnly = true;

dataGridView1.Rows[count].Cells[0].Selected = false;

// Change color by row

// how to change color of particular row = System.Drawing.SystemColors.InactiveBorder;

}

else

{

// Change color by row

// how to change color of particular row = System.Drawing.Color.WhiteSmoke;

}

}

}catch(Exception ex)

{

MessageBox.Show(ex.Message);

}

}

}

Change color of particular row in DataGridView  Change color of particular row in DataGridView

03-Sep-09 02:17 AM

Do it in Paint like; 

Protected Overloads Overrides Sub Paint(ByVal graph As Graphics, _
         ByVal rectbounds As Rectangle, ByVal curmngrSrc As _
         CurrencyManager, ByVal RowNumber As Integer, ByVal _
         ForeColorBrush As Brush, ByVal BackColorBrush As Brush, _
         ByVal AlignmentRight As Boolean)

       ' Set Default fore/back colors
        ForeColorBrush = Brushes.Black
        BackColorBrush = Brushes.White

        Dim ObjVal As Object
        Dim dr As DataRow = CType(curmngrSrc.List().Item(RowNumber), DataRowView).Row
        ObjVal = dr(1).ToString.Trim()
       
        If Not (IsNothing(ObjVal) Or IsDBNull(ObjVal)) Then
            Dim cellValue As String
            cellValue = CType(ObjVal, String)
           
            If (cellValue.Contains("ON_HOLD")) Then
                ForeColorBrush = Brushes.PaleTurquoise
               
            ElseIf (cellValue.Contains("FAILED")) Then
                  ForeColorBrush = Brushes.Red
            End If

        End If

        Dim ri As Integer
        ri = frmMAIN.frmchldStatus.dgJobStatus.CurrentRowIndex

        If ((ri >= 0) And (ri = RowNumber)) Then
            'If datagrid current row index matches the selected row then set the selection fore/back colors  
            If (frmMAIN.frmchldStatus.dgJobStatus.IsSelected(ri)) Then
                    BackColorBrush = Brushes.DarkBlue
                    ForeColorBrush = Brushes.White
                End If
        Else
        End If

        ' Call Paint from the base class to
        ' accomplish the actual drawing.
        MyBase.Paint(graph, rectbounds, curmngrSrc, RowNumber, _
            BackColorBrush, ForeColorBrush, AlignmentRight)
    End Sub

try this  try this

03-Sep-09 02:18 AM
You can try using th eCellFormatting event.

void dataGridView1_CellFormatting(object sender,
DataGridViewCellFormattingEventArgs e)
{
object val = dataGridView1["Col1", e.RowIndex].Value;
if (e.RowIndex > -1 && e.ColumnIndex >= -1
&& val != null && val.Equals(2))
{
e.CellStyle.BackColor = Color.Red;
}
}

RE  RE

03-Sep-09 02:28 AM
Hi

You can use this way


protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
// searching through the rows
if (e.Row.RowType == DataControlRowType.DataRow)
{
bool isnew = (bool)DataBinder.Eval(e.Row.DataItem, "IsNew");
if ( isnew ) e.Row.BackColor = Color.FromName("#FAF7DA"); // is a "new" row
}
}

thank you
http://www.codegain.com
  Vasanthakumar D replied to Abhi Rana
03-Sep-09 02:34 AM

Hi,

try the below one...

private void dataGridView1_DataBindingComplete(object sender, DataGridViewBindingCompleteEventArgs e)

{

try

{

for (int count = 0; count < dataGridView1.Rows.Count; count++)

{

if (Convert.ToInt32(dataGridView1.Rows[count].Cells[5].Value) !=0)

{

dataGridView1.Rows[count].Cells[0].ReadOnly = true;

dataGridView1.Rows[count].Cells[0].Selected = false;

// Change color by row

// how to change color of particular row = System.Drawing.SystemColors.InactiveBorder;

dataGridView1.Rows[count].DefaultCellStyle.BackColor = System.Drawing.Color.FromName("InactiveBorder");

}

else

{

// Change color by row

// how to change color of particular row = System.Drawing.Color.WhiteSmoke;

dataGridView1.Rows[count].DefaultCellStyle.BackColor = System.Drawing.Color.WhiteSmoke;

}

}

}catch(Exception ex)

{

MessageBox.Show(ex.Message);

}

}

}

Thanks alot  Thanks alot
03-Sep-09 02:48 AM
end of post
Create New Account
help
String.Empty Then MsgBox("färben") For Each oCell In oRow.Cells oCell.Style.BackColor = SystemColors.Control oCell.Style.ForeColor = SystemColors.ControlDarkDark Next oCell End If End If Next oRow - - Die Msgbox erscheint immer, aber die String.Empty Then MsgBox("färben") For Each oCell In oRow.Cells oRow.InheritedStyle.BackColor = SystemColors.Control oRow.InhertitedStyle.ForeColor = SystemColors.ControlDarkDark End If Das gleiche Problem. Wie wird also die Farbe eine Zelle geändert hier nicht die CurrentCell ist? Grüße Manfred VB.NET - German Discussions System.Web.UI.WebControls.GridViewRowEventArgs (1) DGVR.DefaultCellStyle.BackColor (1) Page.ClientScript.GetPostBackEventReference (1) DataControlRowType.DataRow (1) GridViewRowEventArgs (1) ORow.DefaultCellStyle.SelectionForeColor (1) ORow.DefaultCellStyle.SelectionBackColor (1) Windows XP (1) Und geht es If oRow.Cells(0).Value.ToString = String.Empty Then MsgBox("färben") oRow.DefaultCellStyle.SelectionBackColor = SystemColors.Control oRow.DefaultCellStyle.SelectionForeColor = SystemColors.ControlDarkDark End If Die Msgbox erscheint, aber keine Farbänderungen
AnchorStyles.Left) | System.Windows.Forms.AnchorStyles.Right))); this.dg.DataMember = ""; this.dg.HeaderForeColor = System.Drawing.SystemColors.ControlText; this.dg.Location = new System.Drawing.Point(8, 16); this.dg.Name = "dg"; this ViewState["_Direction_"] = m_SortDirection; ViewState["_SortExp_"] = m_strSortExp = e.SortExpression; this.bindGridView(); } protected void OnRowCreated(object sender, GridViewRowEventArgs e) { if (e.Row.RowType = = DataControlRowType.Header) { if (String.Empty ! = m_strSortExp) { AddSortImage(e.Row); } } } void AddSortImage(GridViewRow headerRow) { Int32 iCol = GetSortColumnIndex
Framework I have a GetData methd which is returning a table using ASP.NET Discussions DataControlRowType.DataRow (1) GridViewRowEventArgs (1) DataControlRowType.Header (1) DataControlRowType.Footer (1) DataControlRowType (1) GridView1.Columns (1) Convert.ToInt32 (1) GridView (1) Autogenerated columns are not included in the Columns collection. You have to handle RowDataBound event instead: protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e) { if (e.Row.RowType = = DataControlRowType.DataRow | | e.Row.RowType = = DataControlRowType.Header | | e.Row.RowType = = DataControlRowType.Footer) { e.Row.Cells[1].Visible = false; } } hope this helps
text like on an html image to a DataGridViewRow. Is it possible? ASP.NET Discussions DataControlRowType.DataRow (1) GridViewRowEventArgs (1) DataControlRowType (1) GridView (1) DataRow (1) ASP.NET (1) DataGridViewRow (1) RowDataBound (1) It is definitely give you a push in the right direction: OnRowDataBound = "MyGridView_RowDataBound" . . . . . . .> . . . . . . . protected void MyGridView_RowDataBound(object sender, GridViewRowEventArgs e) { if (e.Row.RowType = = DataControlRowType.DataRow) { e.Row.Attributes.Add("onmouseover", } } I'm sure you can adapt it for your