C# Tutorials and offshore development in India
    Tutorials   Resources   Forum   Reviews   Communities   Interview   Jobs   Projects   Training   Your Ad Here    
Silverlight Games | Mentor | Code Converter | Articles | Code Factory | Computer Jokes | Members | Peer Appraisal | IT Companies | Bookmarks | Polls | Revenue Sharing | Lobby | Gift Shop |


Prizes & Awards
My Profile



Active Members
TodayLast 7 Days more...






Resources » Code Snippets » DataGridView »

Conditional formatting in Datagrid


Posted Date: 04 Sep 2009    Resource Type: Code Snippets    Category: DataGridView
Author: HygeenaMember Level: Gold    
Rating: 1 out of 5Points: 7



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
}
}





Responses


No responses found. Be the first to respond and make money from revenue sharing program.

Feedbacks      
Popular Tags   What are tags ?   Search Tags  
Sign In to add tags.
Conditional formatting in datagrid  .  

Post Feedback


This is a strictly moderated forum. Only approved messages will appear in the site. Please use 'Spell Check' in Google toolbar before you submit.
You must Sign In to post a response.
Next Resource: Insertion of more than one record at a time.
Previous Resource: Select and Highlight an entire row in DataGridView
Return to Discussion Resource Index
Post New Resource
Category: DataGridView


Post resources and earn money!
 
More Resources



dotNet Slackers

About Us    Contact Us    Privacy Policy    Terms Of Use