Click here to Skip to main content
15,896,118 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
XML
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>


Up to that everything is working fine....

The problem starts when i assign product value to each product textbox column of GridView, click Save button for saving data in database. So the "TextBox" disappears, while the two columns does not disappears which where added from .aspx page, and the controls added from .cs page disappears on post back...



How should i save the result, any help....
Posted
Comments
[no name] 7-Sep-13 13:48pm    
You can write css class before adding any column. while you add the grid dynamically, you will have to add the css.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900