Click here to Skip to main content
15,908,112 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi.. friends a window based project is being developed by me, I am getting the record of students from SQL database and show in Datagridview. The students are divided in different - 2 category for example Commerce, Medical and Arts.The problem is that if the student is in Commerce category then I want to show a red color circle in a Datagridview column in front of associated student record, if he is in Medical category then yellow color circle and if in Arts then green color circle, I add the column at design time and take an image type column to show circle and place an image of circle in it, but how can I change the color of this image according to my conditions.

Please help me I need it very urgent if anybody have some idea about it.

Thanks & Regards

Parveen Rathi
Posted

1 solution

Itz pretty simple. Sample code is:

C#
private void myDataGridView_CellValueChanged(object sender, DataGridViewCellEventArgs e)
  {
   int colIndex = e.ColumnIndex;
   int rowIndex = e.RowIndex;

   if (rowIndex >= 0 && colIndex >= 0)
   {
    DataGridViewRow theRow = dataGridView1.Rows[rowIndex];
    if (theRow.Cells[colIndex].Value.ToString() == "Commerce")
     theRow.DefaultCellStyle.BackColor = Color.Red;
    elseif (theRow.Cells[colIndex].Value.ToString() == "Medical")
     theRow.DefaultCellStyle.BackColor = Color.Yellow;
    if (theRow.Cells[colIndex].Value.ToString() == "Arts")
     theRow.DefaultCellStyle.BackColor = Color.Green;
   }
  }
 
Share this answer
 
Comments
Parveen Rathi 22-Nov-11 23:46pm    
Sir it will ginve error in vb.net coding show that index is out of range and it change the whole row's color change I want to change only the image color which is in coloum

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