I recently had a situation where I had to highlight a row in the gridview if the value of the column Today was 0. Found out there was a very easy way to do it using the OnRowDataBound Event of the GridView, the code's below..
protected void GridViewID_OnRowDataBound(object sender, GridViewRowEventArgs e)
{
GridViewRow row = e.Row;
if (row.RowType == DataControlRowType.DataRow)
{
DataRowView drv = row.DataItem as DataRowView;
string fieldName= drv["FIELDNAME"].ToString();
if (fieldName.Equals("0"))
row.CssClass = "YourCssClassName";
}
}
Showing posts with label Grid View. Show all posts
Showing posts with label Grid View. Show all posts
Monday, 13 August 2007
DataFormatString - Formatting Data in a GridView -HTMLEncode to false
The data in a datagrid can be formatted using the DataFormatString. For example you can add a £ sign or make sure the numbers are formatted to 2 decimal points and so on. The link below gives you a reference list of the things you can do but the most important point is, you need to set the HtmlEncode Property to false, or else you will not get the desired results. This one can get you because it is not stated clearly anywhere and spent half an hour trying to figure out why it wasnt working!
http://msdn2.microsoft.com/en-us/library/system.web.ui.webcontrols.boundfield.dataformatstring.aspx
http://msdn2.microsoft.com/en-us/library/system.web.ui.webcontrols.boundfield.dataformatstring.aspx
Programmatically Adding Columns to GridView
There are cases where you might want to use the same GridView to display diferent columns in a page. This is possible by programmatically adding the columns using the BoundField Object. The key thing to note here is the diference in adding a column to a DataGrid and a GridView, to add a bound column in the DataGrid you should use the BoundColumn object and to add a column to the GridView, you should use a BoundField object. You can create the gridview in your .aspx page and set the sorting and things like that or you can create the complete gridview in your code behind using the GridView class.
The code below shows how to add columns to a gridview that was created in the .aspx page..
--.aspx page--
AllowPaging="True" AllowSorting="True" PageSize="21" EmptyDataText="No data available."
CssClass="gridstyle" GridLines="None" OnSorting="GridViewID_Sorting" OnPageIndexChanging="GridViewID_Paging"
Width="100%">
--.cs page--
private void DefineGridViewColumns()
{
BoundField column = new BoundField(); //You can use the TemplateField object to add a template column
column.HeaderText = "XX";
column.DataField = "ID";
column.SortExpression = "ID";
column.HeaderStyle.CssClass = "titletext";
column.ItemStyle.Width = Unit.Percentage(7);
GridViewID.Columns.Add(column);
column = new BoundField();
column.HeaderText = "RANK";
column.DataField = "RANKNO";
column.SortExpression = "RANKNO";
column.HeaderStyle.CssClass = "titletext";
column.ItemStyle.Width = Unit.Percentage(7);
GridViewID.Columns.Add(column);
if (TimeSpan.Equals("TW"))
{
column = new BoundField();
column.HeaderText = "THIS WEEK";
column.DataField = "XX_TARGET";
column.SortExpression = "XX_TARGET";
column.HeaderStyle.CssClass = "titletext";
column.ItemStyle.Width = Unit.Percentage(10);
GridViewID.Columns.Add(column);
}
}
The code below shows how to add columns to a gridview that was created in the .aspx page..
--.aspx page--
CssClass="gridstyle" GridLines="None" OnSorting="GridViewID_Sorting" OnPageIndexChanging="GridViewID_Paging"
Width="100%">
--.cs page--
private void DefineGridViewColumns()
{
BoundField column = new BoundField(); //You can use the TemplateField object to add a template column
column.HeaderText = "XX";
column.DataField = "ID";
column.SortExpression = "ID";
column.HeaderStyle.CssClass = "titletext";
column.ItemStyle.Width = Unit.Percentage(7);
GridViewID.Columns.Add(column);
column = new BoundField();
column.HeaderText = "RANK";
column.DataField = "RANKNO";
column.SortExpression = "RANKNO";
column.HeaderStyle.CssClass = "titletext";
column.ItemStyle.Width = Unit.Percentage(7);
GridViewID.Columns.Add(column);
if (TimeSpan.Equals("TW"))
{
column = new BoundField();
column.HeaderText = "THIS WEEK";
column.DataField = "XX_TARGET";
column.SortExpression = "XX_TARGET";
column.HeaderStyle.CssClass = "titletext";
column.ItemStyle.Width = Unit.Percentage(10);
GridViewID.Columns.Add(column);
}
}
Subscribe to:
Posts (Atom)