상세 컨텐츠

본문 제목

Conditional formatting in Datagrid

C#

by xarfox 2011. 1. 5. 10:42

본문


This code snippet is to format the datagrid.For example our datagrid contains students details with mark.This code is for showing datagrid row with mark >60 in AliceBlue color

[I]CODE FOR FILLING DATA IN DATAGRID


void fill_products()
{
con.Open();
cmd.CommandText = "select * from student";
cmd.Connection = con;
SqlDataAdapter adp = new SqlDataAdapter();
adp.SelectCommand = cmd;
DataSet ds = new DataSet();
adp.Fill(ds);
DataGrid2.DataSource = ds.Tables[0];
DataGrid2.DataBind();
con.Close();
}

[I] CHECKING CONDITION AND FORMAT GRID ROW OR VALUE


protected void DataGrid2_ItemDataBound(object sender, DataGridItemEventArgs e)
{
int mark;
mark = Convert.ToInt16(DataBinder.Eval(e.Item.DataItem, "mark"));
if (mark > 60)
{
e.Item.BackColor = System.Drawing.Color.AliceBlue; // formatting entire row
e.Item.Cells[2].BackColor = System.Drawing.Color.Crimson; //format only a particular cell
}
}







관련글 더보기