473,239 Members | 1,566 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,239 software developers and data experts.

[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 2784
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Luca Beretta | last post by:
I think this a simple question but i'm breaking my brain without results. I've a datagridview bounded to a datatable, with an imagecolumn that should contain a different bitmap depending from a...
6
by: martin1 | last post by:
Hi, all, Is there any way to change DataGridView row color based on different row data? for example, if row data in first column = 1, it is yellow, if it is 2, it is blue, if 3 it is white. That...
2
by: Ryan | last post by:
I want to programmatically add a row to DataGridView (a datasource was binded with it), but when I call the method DataGridView.Rows.Add, a System.InvalidOperationException will be thrown and the...
6
by: Terry Olsen | last post by:
I have two DataGridView's loaded from their respective datasources. I want to highlight rows in one DataGridView that are also in the other DataGridView. But the following code does not work....
4
by: craig.wenger | last post by:
I am using a DataGridView to display information stored in classes. I am linking to the information with the Tag property of the DataGridView rows. Unfortunately, whenever I set the Tag property,...
4
by: Vidhura | last post by:
This article explains how to make all the rows height the same in DataGridView in .Net 2.0 while using AutoSizeRowMode In DataGridView 2.0,there is a option to resize row to fit the content of...
3
by: frostbb | last post by:
Greetings: Working on a VS2005 C# program. I have a DataGridView control that is bound to dataviews generated by various database queries. i.e. the user enters search criteria and the query...
0
by: Firona | last post by:
Hi, I'm trying to add several rows to a dataGridView. My current code adds the rows by calling the dataGridView.Rows.Add() method for every row. I know there is the dataGridView.Rows.AddRange()...
0
by: evilson | last post by:
Hi, I have two textbox, one of the textbx is is set to multiline. Problem is when i try to insert the value of the multiline textbox to the datagridview, it never work. The datagrid's warpmode is...
3
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 3 Jan 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). For other local times, please check World Time Buddy In...
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 7 Feb 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:30 (7.30PM). In this month's session, the creator of the excellent VBE...
0
by: stefan129 | last post by:
Hey forum members, I'm exploring options for SSL certificates for multiple domains. Has anyone had experience with multi-domain SSL certificates? Any recommendations on reliable providers or specific...
0
Git
by: egorbl4 | last post by:
Скачал я git, хотел начать настройку, а там вылезло вот это Что это? Что мне с этим делать? ...
1
by: davi5007 | last post by:
Hi, Basically, I am trying to automate a field named TraceabilityNo into a web page from an access form. I've got the serial held in the variable strSearchString. How can I get this into the...
0
by: MeoLessi9 | last post by:
I have VirtualBox installed on Windows 11 and now I would like to install Kali on a virtual machine. However, on the official website, I see two options: "Installer images" and "Virtual machines"....
0
by: DolphinDB | last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation. Take...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.