I have a GridView with 2 columns (id, dname). these columns i have taken as a TemplateField with there header Text property, label and dropdownlist control as an ItemTemplate inside TemplateField for each column. Now at runtime i have added some new columns, number of columns are not defined, depends on the product list selected in checkbox. I add product to GridView at runtime. at Its working fine... Here is my code... .aspx Page..... <asp:GridView ID="grid1" runat="server" DataKeyNames="id" CssClass="stdtable stdtablequick" AutoGenerateColumns="false" Width="100%" onrowcommand="grid1_RowCommand" onrowdatabound="grid1_RowDataBound"> <Columns> <asp:TemplateField HeaderText="S.NO"> <ItemTemplate> <asp:Label ID="lblid" Text='<%# Bind("id") %>'> </asp:Label> </ItemTemplate> </asp:TemplateField> <asp:TemplateField HeaderText="Dr.NAME"> <ItemTemplate> <asp:DropDownList ID="ddldoctor" DataSourceID="SqlDataSource1" DataTextField="dname" DataValueField="dname" runat="server" AppendDataBoundItems="true"><asp:ListItem>--Select--</asp:ListItem></asp:DropDownList> </ItemTemplate> </asp:TemplateField> </Columns> </asp:GridView> Here in code <pre lang="cs"> foreach (ListItem item in chk1.Items) { if (item.Selected) { TemplateField tfield = new TemplateField(); tfield.HeaderText = item.Value; tfield.ItemTemplate = new templatehander(ListItemType.Item, item.Value, item.Value); grid1.Columns.Add(tfield); } } grid1.DataSource = dt2; grid1.DataBind();</pre>
var
This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)