protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e) { int ID = Convert.ToInt32(GridView1.DataKeys[e.RowIndex].Value.ToString()); GridViewRow row = (GridViewRow)GridView1.Rows[e.RowIndex]; TextBox Name = (TextBox)row.FindControl("TestName"); TextBox Dep = (TextBox)row.FindControl("Dept"); TextBox Pric = (TextBox)row.FindControl("Price"); TextBox Typ = (TextBox)row.FindControl("Type"); TextBox Statu = (TextBox)row.FindControl("Status"); GridView1.EditIndex = -1; con.Open(); SqlCommand cmd = new SqlCommand("update Test set TestName=@TestName,Dept=@Dept,Price=@Price, Type=@Type Status=@Status where ID=@ID", con); cmd.Parameters.Add("@ID", SqlDbType.Int).Value = ID; cmd.Parameters.Add("@TestName", SqlDbType.VarChar).Value = Name.Text.Trim(); cmd.Parameters.Add("@Dept", SqlDbType.VarChar).Value = Dep.Text.Trim(); cmd.Parameters.Add("@Price", SqlDbType.VarChar).Value = Pric.Text.Trim(); cmd.Parameters.Add("@Type", SqlDbType.VarChar).Value = Typ.Text.Trim(); cmd.Parameters.Add("@Status", SqlDbType.VarChar).Value = Statu.Text.Trim(); cmd.ExecuteNonQuery(); con.Close(); Foo(); }
<pre lang="xml"><asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="false" OnSelectedIndexChanging="GridView1_SelectedIndexChanging" onrowcommand="GridView1_RowCommand" onrowcancelingedit="GridView1_RowCancelingEdit" onrowdeleting="GridView1_RowDeleting" onrowediting="GridView1_RowEditing" onrowupdating="GridView1_RowUpdating" DataKeyNames="TestID"> <Columns> <asp:TemplateField HeaderText="ID"> <ItemTemplate> <asp:Label ID="lblTestID" runat="server" Text='<%# Eval("TestID") %>'></asp:Label> </ItemTemplate> </asp:TemplateField> <asp:TemplateField HeaderText="Test Name"> <ItemTemplate> <asp:Label ID="lblTestName" runat="server" Text='<%# Eval("TestName") %>'></asp:Label> </ItemTemplate> </asp:TemplateField> <asp:TemplateField HeaderText="Department"> <ItemTemplate> <asp:Label ID="lblDept" runat="server" Text='<%# Eval("Dept") %>'></asp:Label> </ItemTemplate> </asp:TemplateField> <asp:TemplateField HeaderText="Price"> <ItemTemplate> <asp:Label ID="lblPrice" runat="server" Text='<%# Eval("Price") %>'></asp:Label> </ItemTemplate> </asp:TemplateField> <asp:TemplateField HeaderText="Type"> <ItemTemplate> <asp:Label ID="lblType" runat="server" Text='<%# Eval("Type") %>'></asp:Label> </ItemTemplate> </asp:TemplateField> <asp:TemplateField HeaderText="Status"> <ItemTemplate> <asp:Label ID="lblStatus" runat="server" Text='<%# Eval("Status") %>'></asp:Label> </ItemTemplate> </asp:TemplateField> <asp:TemplateField> <ItemTemplate> <asp:LinkButton ID="lbtnItems" runat="server" CommandName="Items" Text="Items"></asp:LinkButton> </ItemTemplate> </asp:TemplateField> <asp:CommandField ShowEditButton="true" /> <asp:CommandField ShowDeleteButton="true" /> </Columns> </asp:GridView>
var
This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)