Saturday, September 12, 2009

Using ITemplate for creating grid columns dynamically

Using ITemplate in Creating Detail columns in Master-Detail using Grid.

I got requirement to create a master-detail grid. Where detail columns will be dynamically created on selected keycolumn.
Detail columns are create in table.
A GridView template is a class that implements the ITemplate interface. It defines the controls that will be displayed on the GridView in a column,
Below sample with static data, GetDetailDataSource() this method can be used for access retreival.
public class DetailGridTemplate : ITemplate
public void InstantiateIn(Control container)
parent = container;
masterKey = ((GridViewDetailRowTemplateContainer)parent).KeyValue;
private void CreateDetailGrid()
detailGrid = new ASPxGridView();
detailGrid.ID = "detailGrid";
detailGrid.AutoGenerateColumns = false;

detailGrid.KeyFieldName = "StudentID";
detailGrid.DataSource = GetDetailDataSource();
private void CreateDetailColumns(ASPxGridView detailGrid)

detailGrid.Columns.Add(new GridViewDataColumn("StudentID", "StudentNo"));
detailGrid.Columns.Add(new GridViewDataColumn("Name", "StudentName"));
detailGrid.Columns.Add(new GridViewDataColumn("DOB", "DateofBirth"));
detailGrid.Columns.Add(new GridViewDataColumn("MarksSubject1", "Subject1"));
detailGrid.Columns.Add(new GridViewDataColumn("MarksSubject2", "Subject2"));

private DataTable GetDetailDataSource()
DataTable detail = new DataTable("Detail");
//Add Columns
detail.Columns.Add("StudentID", typeof(String));
detail.Columns.Add("Name", typeof(String));
detail.Columns.Add("DOB", typeof(String));
detail.Columns.Add("MarksSubject1", typeof(String));
detail.Columns.Add("MarksSubject2", typeof(String));
// Add data rows to the table.
for (int i = 0; i < 2; i++)
detail.Rows.Add(new object[] { "004270" + i, "John", "11/08/1989", "56", "65"});
return detail;


1 comment:

Vijay said...

nice blog..
Keep posting