[c# 2008] color rows of datagridview

  • Thread starter Thread starter Christian
  • Start date Start date
C

Christian

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
 
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
 
ok....
i create a Method to Load the datagridview....
Code:
public void Visualizza()
{
string querySelect = "select * from Tabella1";
try
{
OleDbCommand command = new OleDbCommand();
DataTable dt = null;
dataGridView1.DataSource = null;
command.Connection = null;
OleDbDataAdapter DAdapter = null;
dataGridView1.Columns.Clear();
command.CommandText = querySelect;

command.Connection = cnn;
dt = new DataTable();
DAdapter = new OleDbDataAdapter(command);
DAdapter.Fill(dt);
dataGridView1.DataSource = dt;

dataGridView1.AllowUserToAddRows = false;
dataGridView1.ReadOnly = true;

ImpostaLarghezzaColonne();

ColoraRighe(dataGridView1,dt);
}
catch (Exception e)
{
MessageBox.Show(e.Message);
}
in the Method Called "ColoraRighe" i try to colors the rows
Code:
public void ColoraRighe(DataGridView da,DataTable dt)
{
if (da.RowCount != 0)
{
foreach (DataGridViewRow row in da.Rows)
{
if (row.Cells[1].Value.ToString() == "Working")
row.DefaultCellStyle.BackColor = Color.LightGoldenrodYellow;
if (row.Cells[1].Value.ToString() == "Si")
row.DefaultCellStyle.BackColor = Color.LightGreen;
if (row.Cells[1].Value.ToString() == "No")
row.DefaultCellStyle.BackColor = Color.Red;
}
}
dataGridView1.DataSource = dt;
}

thanks
 
Back
Top