Datagrid control

  • Thread starter Thread starter Paul
  • Start date Start date
P

Paul

I have a datagrid I want to change the color of the datagrid row dependant
upon the contents of the data in the row after I bind it to the dataset.

Any hints to how to do this would be appreciated.

Thanks
 
this may help

public static void SetDataGridColors(ref DataGrid dg)
{
int itemCount = dg.Items.Count;
for(int 1=0;1<itemCount;i++)
{
string myCellValue = dg.Items.Cells[0].Text();
switch(myCellValue)
{
case "this value"
dg.Items.BackColor = System.Drawing.Color.Red;
break;
case "that value"
dg.Items.BackColor = System.Drawing.Color.White;
break;
}
}
}

this will change the color of the entire row based on the value of the 1st cell in each row.
Remember the cells collection of the datarow is 0 based

if you want to change only one cell in the row a second loop will be needed on the cells collection in the datagrid items loop

hth
Harold
 
sorry paul

You call it by sending the completed grid to it.
It does not have to be on ItemDataBound event of the grid, as our helpful friend Diogo said, but it does have to be after it is databound. You may want to change the color of things in the grid if you have edits to the grid without rebinding. This will fire on postback and the ItemDataBound only fires when it's bound up
Like

datagrid1.DataBind()

SetGridColors(ref datagrid1
 
Back
Top