Click here to Skip to main content
15,922,007 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have datagridview which is populated with 3 columns partno,locality,houseno. Initially my row colour would be say light blue. Now what i want is whenever houseno changes in row,its row colour should be changed to say grey. That means whenever houseno changes,these two colours should be alternately applied to rows of grid.
For example,values that would be populated in houseno column will be
0
0
1
1
2
etc.Now for row with houseno 0 will have row colour as light blue ,for houseno 1 will have row colour grey,for houseno 2 again light blue and so on.
Can any1 help me with this.
Thanks in advance.
Posted
Updated 28-Dec-12 21:09pm
v2

There is no direct method that matches your need. You need to add

subscribe the RowAdded event e.g
C#
Color prevBackColor = Color.LightBlue; // Member variable
string prevHouseNumber = string.Empty; // member variable

private void dataGridView1_RowsAdded(object sender, DataGridViewRowsAddedEventArgs e)
{
  if (e.RowIndex != -1)
  {
       string houseNumber = Convert.ToInt32(dataGridView1.Rows[e.RowIndex].Cells[HOUSE_NUM_COLUMN_INDEX].Value);
 
      if(prevHouseNumber.Length != 0 && houseNumber != prevHouseNumber)
      {
         //set the alternate color
         if( prevBackColor == Color.Gray)
         {
           prevBackColor = Color.LightBlue; 
         } else if ( prevBackColor == Color.LightBlue)
         {
           prevBackColor = Color.Gray;
         }
         prevHouseNumber = houseNumber;        
      }

      dataGridView1.Rows[e.RowIndex].DefaultCellStyle.BackColor = prevBackColor;

   }
}
 
Share this answer
 
v5
Comments
Rachna0309 29-Dec-12 3:01am    
but this event would trigger when user changes houseno value.houseno value changes from database itself and row colour should be changed accordingly.
Ashok19r91d 29-Dec-12 3:09am    
Mumm my be, Somewhat smart you are, Why you try out that code in your style?
Just Execute these Code Periodically For Example by using Timer...
Rachna0309 29-Dec-12 3:11am    
Can you provide me code for that?
Ashok19r91d 29-Dec-12 3:12am    
VB or C#?
Ashok19r91d 29-Dec-12 3:20am    
Question Improved...
Now you need to Display like Alternating Rows, But the Problem is there may be more than one row for each housenumber... Am I Right?
If your Rows are Ordered by HouseNumber then this Code Help you
VB
Private Sub FormatGrid()
Dim j as Integer = -1
Dim Colr As Color
'Here I assume that HouseNumber available in Cell (0) (i.e., First Column)
For i = 0 To DataGridView1.RowCount - 1
    If DataGridView1.Rows(i).Cells(0).Value <> j Then
        j = DataGridView1.Rows(i).Cells(0).Value
        Select Case Colr
        Case Gray: Colr = LightBlue
        Case LightBlue: Colr = Gray
        End Select      
    End If
    DataGridView1.Rows(i).DefaultCellStyle.BackColor = Color  
Next
End Sub
 
Share this answer
 
v2
Comments
Ashok19r91d 29-Dec-12 5:42am    
Call this Function FormatGrid When ever you need to Refresh Your DataGrid...
Add below code in DataGridView's DataBindingComplete event:


private void dgvListData_DataBindingComplete(object sender, DataGridViewBindingCompleteEventArgs e)
{
Color firstColor = Color.White, secondColor = ColorTranslator.FromHtml("#C8DFF1"), prevBackColor = ColorTranslator.FromHtml("#C8DFF1"); // Member variable
string prevVoucherNumber = string.Empty; // Member variable

foreach (DataGridViewRow dRow in dgvListData.Rows)
{
#region < To give alternate row color based on Voucher No >
if (dRow.Index != -1)
{
string voucherNumber = Convert.ToInt32(dgvListData.Rows[dRow.Index].Cells["Voucher_No_With_Prefix"].Value).ToString();

if (prevVoucherNumber.Length == 0 || voucherNumber != prevVoucherNumber)
{
//set the alternate color
if (prevBackColor == firstColor) // firstColor
{
prevBackColor = secondColor; // secondColor
}
else if (prevBackColor == secondColor) // secondColor
{
prevBackColor = firstColor; // firstColor
}
prevVoucherNumber = voucherNumber;
}

dgvListData.Rows[dRow.Index].DefaultCellStyle.BackColor = prevBackColor;

}
#endregion
}
}
 
Share this answer
 
v2

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