How to get value of editable field in Gridview

1.6k Views Asked by At

I have a gridview with an ObjectDataSource which I want to update with one click. Therefore the gridview loads with editable and fixed fields. I loop through the gridview to update all in once. I am able to load the values from the fixed fields (Cells[0]) into the Products object (p.ProductID), but I am not able to load the values from the editable fields (Cells[1]) into the Products object (p.Productname).

How can I get this value in the object so I can send the object to the BLL method. How come row.Cells[0].Text works for fixed fields, but not for editable fields?

    protected void Update_Click(object sender, EventArgs e)
    {
        BLLProducts BLLProducts = new BLLProducts();
        Products p = new Products();
        foreach(GridViewRow row in GridView1.Rows)
        {
            p.ProductID = Convert.ToInt16(row.Cells[0].Text);
         // p.Productname = row.Cells[1].Text;

            BLLProducts.update(p);
        }
    }

Markup:

<asp:Content ID="HeaderContent" runat="server" ContentPlaceHolderID="HeadContent">
</asp:Content>
<asp:Content ID="BodyContent" runat="server" ContentPlaceHolderID="MainContent">
        <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" 
            DataSourceID="test">
            <Columns>
                <asp:BoundField DataField="ProductID" 
                    HeaderText="ProductID" SortExpression="ProductID" />
                <asp:TemplateField HeaderText="Productname" SortExpression="Productname">
                    <EditItemTemplate>
                        <asp:TextBox ID="TextBox1" runat="server" Text='<%# Bind("Productname") %>'></asp:TextBox>
                    </EditItemTemplate>
                    <ItemTemplate>
                        <asp:TextBox ID="TextBox1" runat="server" Text='<%# Bind("Productname") %>'></asp:TextBox>
                    </ItemTemplate>
                </asp:TemplateField>             
            </Columns>
        </asp:GridView>
        <asp:ObjectDataSource ID="test" runat="server" 
            SelectMethod="SelectAllProducts" TypeName="BLLProducts">
        </asp:ObjectDataSource>
        <asp:Button ID="Button3" runat="server" onclick="Update_Click" 
            Text="Update All" />
    </p>
</asp:Content>
1

There are 1 best solutions below

0
On BEST ANSWER

which editable field you are using?

if you have used text box then you should get value like this:

       foreach (GridViewRow gvr in GridView2.Rows)  
       {  
           TextBox tb = (TextBox)gvr.FindControl("TextBox1");  
           string txt = tb.Text;              
       }