Click here to Skip to main content
15,886,137 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi
Everyone

I want set group by color to datagridview rows.

like

eg


1 abc
2 abc
3 abc
4 xyz
5 xyz
6 xyz
7 pqr
8 pqr
9 pqr
10 lmn
11 lmn
12 lmn

Here i want to set color to the rows
lik

abc=red
xyz=blue
pqr=red
lmn=blue

i.e i want to set groupwise color to datagridview row.

Please help me...

Thanks ... In advance.....
Posted
Comments
Emre Ataseven 19-Oct-13 8:40am    
Only 2 colors to seperate groups?
basurajkumbhar 19-Oct-13 8:46am    
Yes only two colors to separate group.

can use item data bound event.On grid binding,in data bound event u can set the color of every row.
 
Share this answer
 
Comments
basurajkumbhar 19-Oct-13 8:23am    
i don't want to set the color to every row.
how to set row colors in gridview.[^]

I already tried this task..and also work it..u can also try it
 
Share this answer
 
Comments
basurajkumbhar 19-Oct-13 8:26am    
In my datagridview there is one column house no
in that column 16 house no is repeated three time then that row set the color red
if there is house no any number then that time set anther color as like alternet color .

thanks...
C#
string tempStr = String.Empty;
private void dataGridView1_RowPrePaint(object sender, DataGridViewRowPrePaintEventArgs e)
{
        if ((dataGridView1.Rows[e.RowIndex].Cells[1].Text) != tempStr)
        {
             dataGridView1.Rows[e.RowIndex].DefaultCellStyle.BackColor = Color.Blue;
             tempStr = dataGridView1.Rows[e.RowIndex].Cells[1].Text;
        }
        else
        {
            dataGridView1.Rows[e.RowIndex].DefaultCellStyle.BackColor = Color.Red;
        }
}



I did not test it, there may be syntax errors.
 
Share this answer
 
Comments
basurajkumbhar 19-Oct-13 9:40am    
Hi thanks for this code
i am convert this code in vb.

Dim dt As New DataTable
dt = ds.Tables(0)

For i = 0 To dt.Rows.Count - 1


ab = dt.Rows(i)("Houseno").ToString()

Dim tempStr As String = [String].Empty
tempStr = ab
MsgBox(dt.Rows(i)("Houseno").ToString())
If dt.Rows(i)("Houseno").ToString() <> tempStr Then
DataGridView1.Rows(i).DefaultCellStyle.BackColor = Color.Blue
tempStr = dt.Rows(i)("Houseno").ToString()
Else
DataGridView1.Rows(i).DefaultCellStyle.BackColor = Color.Red
End If
Next

But it still not give the result like me

thanks for code
Ganesan Natarajan 22-Oct-13 3:24am    
modify your question with real data..

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



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900