Click here to Skip to main content
13,259,847 members (52,894 online)
Click here to Skip to main content
Add your own
alternative version

Tagged as


12 bookmarked
Posted 18 May 2011

Customize DataGridView row without iteration

, 18 May 2011
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.

You may also be interested in...


Comments and Discussions

Questionimproving row alternating Pin
rom32327-Jan-16 16:58
memberrom32327-Jan-16 16:58 
Questionwhat about the post paint method Pin
rabosa20-Mar-12 7:16
memberrabosa20-Mar-12 7:16 
GeneralReason for my vote of 2 It's too simple for codeproject arti... Pin
Agah Burak DEMİRKAN23-Jun-11 4:39
memberAgah Burak DEMİRKAN23-Jun-11 4:39 
GeneralRe: Thank you Pin
ambarishtv23-Jun-11 4:54
memberambarishtv23-Jun-11 4:54 

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

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

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