[VB.NET] Gridview Row Color


Description :
         to give row color gridview in vb.net.

How To :
          we want to give row color gridview in vb.net like this , white grey white grey ....


white grey white grey white grey (haha LoL )

   Step coloring row field  :
        1. give name control grid as 'gridView' (as you like :D ).
        2. create sub, for example give name "setColorGrid" , here's the details below :
       
        Public Sub setColorGrid()
        For i As Integer = 0 TogridView.Rows.Count - 1 'get max count row ...
            If i = 0 Then
                gridView.Rows(i).Cells(0).Style.BackColor = Color.LightGray
            Else
                If i Mod 2 = 1 Then
                    gridView.Rows(i).Cells(0).Style.BackColor = Color.White
                Else
                   gridViewRows(i).Cells(0).Style.BackColor = Color.LightGray
                End If
            End If
        Next i
        End Sub
       
       3. add button , on event click add setColorGrid() , for example look details below :

    Private Sub command1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles command1.Click
        setColorGrid()
    End Sub

       4. Click the button , and you'll get the row field color has been changed .... but if user click header (for sort from ascending to descending) the color will be return normal (white), to handle this just use event RowPrePaint(), look the details code below :

     Private Sub gridView_RowPrePaint(ByVal sender As System.Object, ByVal e As  System.Windows.Forms.DataGridViewRowPrePaintEventArgs) Handles gridView.RowPrePaint
        setColorGrid()
     End Sub

      5. all code only affect 1 column, but if you wish add more color for many column just follow the code below (i know you will understand quick) and create your iteration for the column you want ...
  
        Public Sub setColorGrid()
        For i As Integer = 0 TogridView.Rows.Count - 1 'get max count row ...
            If i = 0 Then
                gridView.Rows(i).Cells(0).Style.BackColor = Color.LightGray
                                     ................................................................
                gridView.Rows(i).Cells(X).Style.BackColor = Color.LightGray
            Else
                If i Mod 2 = 1 Then
                    gridView.Rows(i).Cells(0).Style.BackColor = Color.White
                                     ................................................................
                    gridView.Rows(i).Cells(X).Style.BackColor =  Color.White
                Else
                   gridViewRows(i).Cells(0).Style.BackColor = Color.LightGray
                                     ................................................................
                gridView.Rows(i).Cells(X).Style.BackColor = Color.LightGray
                End If
            End If
        Next i
        End Sub


UPDATE :
ahhh. .NET framework already such a feature for coloring even / odd :
* right click on dataGrid > Property > AlternatingRowsDefaultCellStyle > set back color

if you want use syntax :
DataGrid.AlternatingRowsDefaultCellStyle.BackColor = ColorTranslator.FromHtml("#xxx")

i recomended use .NET feature than my method. because the minus using my method is when the data and the colomn is big , the render is too slow and it's very fatal , make the memory TO THE MAX hahaha

but if u change 1 or 2 rows it's okay ....   

Post a Comment

Harap gunakan bahasa yang baik dan sopan, terima kasih