Click here to Skip to main content
Click here to Skip to main content

Tagged as

Customize DataGridView row without iteration

, 18 May 2011 CPOL
Rate this:
Please Sign up or sign in to vote.
here I'm going to shows a helpfull tip. Suppose if I need to assign different colors for each row in the DataGridView, we usually do it iterating through codebehind. There is an alternative for this instance which is directly binded DataGridView rows based on certain conditions.
Advantage :
Avoid loop iteraion after loading
   private void dataGridView1_RowPrePaint(object sender, DataGridViewRowPrePaintEventArgs e)
            DataGridView grd = sender as DataGridView;
            if (grd.Rows[e.RowIndex].Cells[1].Value.ToString() == "STOPED")
                grd.Rows[e.RowIndex].DefaultCellStyle.BackColor = Color.HotPink;
            if (grd.Rows[e.RowIndex].Cells[1].Value.ToString() == "ACTIVE")
                grd.Rows[e.RowIndex].DefaultCellStyle.BackColor = Color.LightGreen;
Private Sub dataGridView1_RowPrePaint(sender As Object, e As DataGridViewRowPrePaintEventArgs)
    Dim grd As DataGridView = TryCast(sender, DataGridView)
    If grd.Rows(e.RowIndex).Cells(1).Value.ToString() = "STOPED" Then
        grd.Rows(e.RowIndex).DefaultCellStyle.BackColor = Color.HotPink
    End If
    If grd.Rows(e.RowIndex).Cells(1).Value.ToString() = "ACTIVE" Then
        grd.Rows(e.RowIndex).DefaultCellStyle.BackColor = Color.LightGreen
    End If
End Sub


This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


About the Author

Software Developer
India India

Hi I' Ambarish from India. I've three and half year of experience in application
development solutions.

Educational Qualification

MCA -  from KristuJyoti College of management and technology (MG

B.Sc (Physics) - from NSS College, Changanacherry (MG University)

Skill Set

C#,winforms,,MVC 3, Java,Design Patterns,VB, Android,JQuery Mobile,SQL,MySqlUML.

Comments and Discussions

Questionwhat about the post paint method Pinmemberrabosa20-Mar-12 7:16 
GeneralReason for my vote of 2 It's too simple for codeproject arti... PinmemberAgah Burak DEMİRKAN23-Jun-11 4:39 
GeneralRe: Thank you Pinmemberambarishtv23-Jun-11 4:54 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Terms of Use | Mobile
Web02 | 2.8.150327.1 | Last Updated 18 May 2011
Article Copyright 2011 by ambarishtv
Everything else Copyright © CodeProject, 1999-2015
Layout: fixed | fluid