Hye,
I want to design a GridView in the following format
| Product 1 | Product 2| Product 3 |
Customer1| | | |
Customer2| | | |
Customer3| | | |
1) Generating Number of Columns from Table Products i-e (Product 1, Product 2, Product 3) as new product is added to the table new column should be generated
2) Each Row must have Header and Header values should be picked from table Customers(Customer1,Customer2,Customer3)
3) As Columns and Row Headers are loaded from the tables remaining cells should be used to collect values
4) A Save button to save all the record present in the cells simultaneously.
Please guide me how to accomplish this task.
With Regards,
Shariz
Hi Shariz,
Based on your description, I think we can generate TemplateField and then insert it to Columns of GridView.
For details please refer to:
private void BindGridView()
{
//Automatically generate column and data
GridView genGridView = new GridView();
DataTable dt = new DataTable();
dt.Columns.Add("FirstName");
dt.Columns.Add("LastName");
dt.Columns.Add("Age", typeof(System.Int32));
dt.Columns.Add("Extend");
DataRow oItem = dt.NewRow();
oItem[0] = "Shawpnendu";
oItem[1] = "Bikash";
oItem[2] = 32;
oItem[3] = "Ex1";
dt.Rows.Add(oItem);
oItem = dt.NewRow();
oItem[0] = "Bimalendu";
oItem[1] = "Bikash";
oItem[2] = 20;
oItem[3] = "Ex2";
//Automatically generate clumn of GridView and insert to the specified location
//BoundField extendColumn = new BoundField();
TemplateField templateField = new TemplateField();
templateField.HeaderText = "Header 1";
templateField.ItemTemplate = new CustomGridViewTemplate(
DataControlRowType.DataRow,
"FirstName",
"string");
//create headerTemplate
templateField.HeaderTemplate = new CustomGridViewTemplate
(DataControlRowType.Header,
"FirstName",
"string");
templateField.EditItemTemplate = new CustomGridViewTemplate(DataControlRowType.DataRow,
"FirstName",
"string");
genGridView.Columns.Add(templateField);
genGridView.DataSource = dt;
genGridView.DataBind();
this.Page.Form.Controls.Add(genGridView);
}
Which data is from code, you can also according to the data from database to generate the Column.
Hope it helps.
Best Regards,
Terry Guo
沒有留言:
張貼留言