how to mark row on gridview by any value?
i have gridview that contain:
Fnam开发者_StackOverflow中文版e|Born
aa |03/05
bb |19/01
cc |03/08
dd |19/01
if today is: 19/01/2011
how to mark in red color the 2 rows that Satisfies the condition ?
in C# asp.net
thanks for any help
this.MyGridView.RowDataBound += (s, ea) =>
{
if (ea.Row.Cells[1].Text.Contains(string.Format("{0}/0{1}", DateTime.Now.Day, DateTime.Now.Month)))
{
ea.Row.BackColor = Color.Red;
}
};
this.MyGridView.DataBind();
This does what you want.
Notice the string.format has a 0 which shouldn't be there. This is because DateTime.Now.Month returns 1 instead of 01.
[edit]
Noticed sometime ago that there's a better way to compare the date to your value using
if (ea.Row.Cells[1].Text.Contains(DateTime.Now.ToString(MM/dd)))
try using RowDataBound event of the grid to test the date column if (e.row.cell[1]=...) , and from there you can access the e.Row.BackColor= ...
In my opinion a good place to start, if you want to change design-settings of a datagridview programmatically is using the datagridview.CellFormatting-Event
private void _dgvDb_CellFormatting(Object sender, DataGridViewCellFormattingEventArgs e){
if (e.Value != null && !String.IsNullOrEmpty(e.Value.ToString()))
{
switch (_dgvDb.Columns[e.ColumnIndex].Name)
{
case "YOUR_COLUMNNAME":
if("value==yourChosenValue"){
}
break;
case "else":
}
}
}
精彩评论