Click here to Skip to main content
15,884,425 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have a gridview that contains 2 item template 1-checkbox 2-textbox like this:

ASP.NET
<asp:GridView ID="Warehouse_gv" runat="server" AllowPaging="True" align="center" 
AllowSorting="True" AutoGenerateColumns="False" DataSourceID="Warehouse_gv_SqlDS" 
                                onselectedindexchanged="Warehouse_gv_SelectedIndexChanged" Width="230px" PageSize="700">
<Columns>
<asp:CommandField ShowDeleteButton="False" ShowEditButton="True" />
<asp:TemplateField HeaderText="Select">
<ItemTemplate>
<asp:CheckBox ID="chkSelect" runat="server" />
</ItemTemplate>
                                         
<HeaderTemplate>
<input id="chkAll"  önclick="javascript:SelectAllCheckboxes(this);" 
 runat="server" type="checkbox" />
</HeaderTemplate>
</asp:TemplateField>
<asp:BoundField DataField="Line Number" HeaderText="Line Number" ReadOnly="True" 
SortExpression="Line Number" />
<asp:BoundField DataField="Reference" HeaderText="Reference" ReadOnly="True" 
SortExpression="Reference" />
 <asp:BoundField DataField="QTY" HeaderText="QTY"
 SortExpression="QTY" />
<asp:TemplateField HeaderText="EditQTY">
                                       
<ItemTemplate>
<asp:TextBox ID ="test" runat="server" />
</ItemTemplate>
                                         
<HeaderTemplate>
EditQTY
</HeaderTemplate>
</asp:TemplateField>
</Columns>
<EmptyDataTemplate>
No data could be find!
</EmptyDataTemplate>
</asp:GridView>

this grid has 6 column 1-editbutton 2-checkbox(first item template) 3-lineNo 4-reference 5-qty 6-editqty(second item template) .
but when I want to save data from this gridview to another database , for textbox field(EditQTY column) that I put in Item template field data doesn't transfer this is my code behind:

C#
protected void SAVE_btn_Click(object sender, EventArgs e)
    {
        int C = 0;
                
        for (int i = 0; i < Warehouse_gv.Rows.Count; i++)
        {
            GridViewRow row = Warehouse_gv.Rows[i];
            bool isChecked = ((CheckBox)row.FindControl("chkSelect")).Checked;
    
            if (isChecked)
            {
                SqlCommand cmd = new SqlCommand();
                cmd.Connection = new SqlConnection(Class1.CnnStr);
                cmd.CommandText = "insert into WarehousePart values (@Reference,@EditQty)";
                cmd.Connection.Open();
                cmd.Parameters.AddWithValue("@Reference", Warehouse_gv.Rows[i].Cells[3].Text); 
                cmd.Parameters.AddWithValue("@EditQty", Warehouse_gv.Rows[i].Cells[5].Text);
                cmd.ExecuteNonQuery();
                cmd.Connection.Close();
                C = C + 1;
            }
        }
    }
Posted

Hi ,
Check this
C#
cmd.Parameters.AddWithValue("@Reference", ((TextBox)Warehouse_gv.Rows[i].FindControl("textbox")).Text);
          cmd.Parameters.AddWithValue("@EditQty", ((TextBox)Warehouse_gv.Rows[i].FindControl("textbox2")).Text);


Best Regards
M.Mitwalli
 
Share this answer
 
Comments
kitykity 8-Sep-12 7:30am    
yes it works,thanks a lot!
Mohamed Mitwalli 8-Sep-12 8:32am    
Your welcome :)

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