Using the Template Class in GridView
Using dynamic template in gridview is slightly different
from datalist i.e. we will create the dynamic gridview in column wise with
header template, item template and footer template from the first column till
the last.
Steps:
1. Create a Gridview Object.
2. Create an instance of
TemplateField object.
3. Instantiate the Dynamic
template class with proper ListItemType and assign it to corresponding template
property of TemplateField object and finally add this object to the column
collection of GridView. Refer the below code for better understanding.
Templates of GridView
TemplateField tf = new TemplateField();
tf.HeaderTemplate = new
DynamicGridViewTextTemplate("ArticleID", DataControlRowType.Header);
tf.ItemTemplate = new
DynamicGridViewTextTemplate("ArticleID", DataControlRowType.DataRow);
tf.FooterTemplate = new
DynamicGridViewTextTemplate(DataControlRowType.Footer,
ds.Tables[i].Rows.Count);
If you compare the implementation of DataList, in
Gridview we won’t create dynamic template for the grid instead we create it for
the grid’s column (TemplateField). Refer the below code (Using Template class)
for clear understanding.
Using Template class
for (int i = 0; i < ds.Tables.Count; i++)
{
if (ds.Tables[i].Rows.Count > 0)
{
GridView gvDynamicArticle = new
GridView();
gvDynamicArticle.Width =
Unit.Pixel(700);
gvDynamicArticle.BorderWidth =
Unit.Pixel(0);
gvDynamicArticle.Caption = "<div
id=\"nifty\" class=\"PostCategory\"> +
ds.Tables[i].Rows[0]["Category"].ToString() + " Articles</div>";
gvDynamicArticle.AutoGenerateColumns =
false;
gvDynamicArticle.ShowFooter = true;
TemplateField tf = null;
tf = new TemplateField();
tf.HeaderTemplate = new
DynamicGridViewTextTemplate("ArticleID", DataControlRowType.Header);
tf.ItemTemplate = new
DynamicGridViewTextTemplate("ArticleID", DataControlRowType.DataRow);
tf.FooterTemplate = new
DynamicGridViewTextTemplate(DataControlRowType.Footer,
ds.Tables[i].Rows.Count);
gvDynamicArticle.Columns.Add(tf);
tf = new TemplateField();
tf.HeaderTemplate = new
DynamicGridViewTextTemplate("Title", DataControlRowType.Header);
tf.ItemTemplate = new
DynamicGridViewTextTemplate("Title", DataControlRowType.DataRow);
gvDynamicArticle.Columns.Add(tf);
tf = new TemplateField();
tf.HeaderTemplate = new
DynamicGridViewTextTemplate("Description", DataControlRowType.Header);
tf.ItemTemplate = new
DynamicGridViewTextTemplate("Description", DataControlRowType.DataRow);
gvDynamicArticle.Columns.Add(tf);
tf = new TemplateField();
tf.HeaderTemplate = new
DynamicGridViewURLTemplate("Title", "URL", DataControlRowType.Header);
tf.ItemTemplate = new
DynamicGridViewURLTemplate("Title", "URL", DataControlRowType.DataRow);
gvDynamicArticle.Columns.Add(tf);
tf = new TemplateField();
tf.HeaderTemplate = new
DynamicGridViewTextTemplate("Author", DataControlRowType.Header);
tf.ItemTemplate = new
DynamicGridViewTextTemplate("CreatedBy", DataControlRowType.DataRow);
gvDynamicArticle.Columns.Add(tf);
gvDynamicArticle.RowDataBound += new
GridViewRowEventHandler(this.DynamicGrid_RowDataBound);
gvDynamicArticle.DataSource =
ds.Tables[i];
gvDynamicArticle.DataBind();
phDynamicGridHolder.Controls.Add(gvDynamicArticle);
}
}
In the above code (Using Template class), we can clearly
understand that we are creating the dynamic template for the gridview’s column
as opposed to Datalist where we created the template for the grid itself. To
throw more light on this it means that we are creating the first column’s
header, item and footer and adding it to the gridview’s column list through
TemplateField object till the last column as I said earlier.
Download the source with the article and see it in
action.
|