Click here to Skip to main content
15,893,401 members
Please Sign up or sign in to vote.
4.00/5 (1 vote)
i have a grid view which consists of a DROPDOWN and 2 textboxes in each row. after clicking a button (at the footer) the data in the first row should be inserted to DATABASE tables and new row must be created. also whenever the selected index is changed in the DDL the corresponding textboxes in the row must be populated with the selected value of the item selected in the DDL.how should it be done for each new row created inside the grid.
the code for gridview is as follows.
C#
<asp:GridView ID="GridView1" ShowFooter="True" AutoGenerateColumns="False" 
                    runat="server" >
                    <Columns>
                        <asp:TemplateField HeaderText="Products/Services">
                            <ItemTemplate>
                                <asp:DropDownList ID="DropDownProducts" Width="150px" runat="server" CssClass="RegistryDrop"
                                  AutoPostBack="true" OnSelectedIndexChanged="DropDownProducts_SelectedIndexChanged" DataSourceID="datasourceProducts" DataTextField="Name" DataValueField="ProdId">
                                </asp:DropDownList>
                                <asp:SqlDataSource ID="datasourceProducts" runat="server" ConnectionString="<%$ ConnectionStrings:EasyCut %>"
                                    SelectCommand="SELECT [Name], [ProdId] FROM [tbl_ProdServ]"></asp:SqlDataSource>
                            </ItemTemplate>
                        </asp:TemplateField>
                        <asp:TemplateField HeaderText="Price">
                            <ItemTemplate>
                                <asp:TextBox ID="TextBox2" runat="server"></asp:TextBox>
                            </ItemTemplate>
                        </asp:TemplateField>
                        <asp:TemplateField HeaderText="Quantity">
                            <ItemTemplate>
                                <asp:TextBox ID="TextBox3" runat="server"></asp:TextBox>
                            </ItemTemplate>
                            <FooterStyle HorizontalAlign="Right" />
                            <FooterTemplate>
                                <asp:Button ID="ButtonAdd" OnClick="btn_add_Click" runat="server" Text="Submit" />
                            </FooterTemplate>
                        </asp:TemplateField>
                    </Columns>
                </asp:GridView>


I tried following approach in the code behind but it doesnt seem to work

C#
protected void DropDownProducts_SelectedIndexChanged(object sender, EventArgs e)
        {
            
            TextBox box1=(TextBox)GridView1.Rows[e.currentrow].Cells[2].FindControl("TextBox2");
            DropDownList dl=(DropDownList)GridView1.Rows[e.currentrow].Cells[2].FindControl("DropDownProducts");

            box1.Text = dl.SelectedValue.ToString();
        }
Posted

1 solution

 
Share this answer
 
Comments
sushil sainju 27-Jun-12 2:06am    
i forgot to mention that my gridview is inside the TABCONTROL
so i get this error when i tried the method in the blog
"Unable to cast object of type 'AjaxControlToolkit.TabPanel' to type 'System.Web.UI.WebControls.GridViewRow'."

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