Thursday, August 27, 2009

Set color of rows in XtraDatagrid in C#

Sometimes we need to set backcolor of row of XtraDatagrid.


I have found the solution of this problem. We should use RowStyle event of XtraDatagrid.

private void gridView1_RowStyle(object sender, DevExpress.XtraGrid.Views.Grid.RowStyleEventArgs e)
{
if (e.RowHandle >= 0)
{
DataRow DR = gridView1.GetDataRow(e.RowHandle);
if (DR["SEX"].ToString() == "Male")
e.Appearance.BackColor = System.Drawing.Color.LightGoldenrodYellow;
if (DR["Age"].ToString() < "20")
e.Appearance.BackColor = System.Drawing.Color.Bisque;
if (DR["OCCUPATION"].ToString() == "Engineer")
e.Appearance.BackColor = System.Drawing.Color.LightBlue;
// etc...
}
}


Read more!!!

To get a value of a cell of focused row of XtraDatagrid in C#

Before I used the code below to get cell value of XtraDatagrid.

string
FirstName = gridView1.GetFocusedDataRow().ItemArray[2].ToString();
But in this case I must know the index of fields in Datasource. it is very difficult in very big tables.


Now I found very way to get a value of a cell of focused row of XtraDatagrid.

DataRow
DR = gridView1.GetDataRow(gridView1.FocusedRowHandle);
string FirstName = DR["FIRSTNAME"].ToString();


Read more!!!