468,538 Members | 1,759 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 468,538 developers. It's quick & easy.

[c# 2008] color rows of datagridview

Hi,
i create aprogram that load a table from Access DB.
i want color rows with different color but when i run the program and
i load the datagridview the first time, the rows are white!
only when i insert a new record the rows change color, why?
thanks to all!
bye
Jun 27 '08 #1
3 2623
On May 9, 5:01*am, Christian <christian.per...@gmail.comwrote:
Hi,
i create aprogram that load a table from Access DB.
i want color rows with different color but when i run the program and
i load the datagridview the first time, the rows are white!
only when i insert a new record the rows change color, why?
thanks to all!
bye
how are you assigning it?
post some code
Jun 27 '08 #2
ok....
i create a Method to Load the datagridview....
Expand|Select|Wrap|Line Numbers
  1. public void Visualizza()
  2. {
  3. string querySelect = "select * from Tabella1";
  4. try
  5. {
  6. OleDbCommand command = new OleDbCommand();
  7. DataTable dt = null;
  8. dataGridView1.DataSource = null;
  9. command.Connection = null;
  10. OleDbDataAdapter DAdapter = null;
  11. dataGridView1.Columns.Clear();
  12. command.CommandText = querySelect;
  13.  
  14. command.Connection = cnn;
  15. dt = new DataTable();
  16. DAdapter = new OleDbDataAdapter(command);
  17. DAdapter.Fill(dt);
  18. dataGridView1.DataSource = dt;
  19.  
  20. dataGridView1.AllowUserToAddRows = false;
  21. dataGridView1.ReadOnly = true;
  22.  
  23. ImpostaLarghezzaColonne();
  24.  
  25. ColoraRighe(dataGridView1,dt);
  26. }
  27. catch (Exception e)
  28. {
  29. MessageBox.Show(e.Message);
  30. }
  31.  
in the Method Called "ColoraRighe" i try to colors the rows
Expand|Select|Wrap|Line Numbers
  1. public void ColoraRighe(DataGridView da,DataTable dt)
  2. {
  3. if (da.RowCount != 0)
  4. {
  5. foreach (DataGridViewRow row in da.Rows)
  6. {
  7. if (row.Cells[1].Value.ToString() == "Working")
  8. row.DefaultCellStyle.BackColor = Color.LightGoldenrodYellow;
  9. if (row.Cells[1].Value.ToString() == "Si")
  10. row.DefaultCellStyle.BackColor = Color.LightGreen;
  11. if (row.Cells[1].Value.ToString() == "No")
  12. row.DefaultCellStyle.BackColor = Color.Red;
  13. }
  14. }
  15. dataGridView1.DataSource = dt;
  16. }
  17.  
thanks
Jun 27 '08 #3
Hi Christian,

you will need the
System.Windows.Forms.DataGridViewCellFormattingEve ntHandler to do
this.

Here is an example:

http://msdn.microsoft.com/en-us/library/z1cc356h.aspx

Regards,

Joachim

Jun 27 '08 #4

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

1 post views Thread by Luca Beretta | last post: by
6 posts views Thread by martin1 | last post: by
6 posts views Thread by Terry Olsen | last post: by
4 posts views Thread by craig.wenger | last post: by
reply views Thread by Firona | last post: by
reply views Thread by NPC403 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.