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);
}
}
3 comments:
Hi Junkie,
Thank you for giving this article.It is very important to me.
Thank you,
Ravi
thanks, just what i needed
To generate columns for Data GridView, we can use either automatic column generation based on the data source or C# code to add columns
Post a Comment