AjithKumar
AjithKumar

Reputation: 13

Code for update and delete a row in grid view

what is the code for updating and saving from grid view data directly in a web form.This is the code i have.Help me out for delete record and update record.

   protected void CancelRecord(object sender, GridViewCancelEditEventArgs e)
{
    gridRegistrationTableDetails.EditIndex = -1;
    BindData();
}
protected void EditRecord(object sender, GridViewEditEventArgs e)
{
    gridRegistrationTableDetails.EditIndex = e.NewEditIndex;
    BindData();
}
protected void UpdateRecord(object sender, GridViewUpdateEventArgs e)
{

}
protected void DeleteRecord(object sender, GridViewDeleteEventArgs e)
{}

i need to edit the four columns Emp name,Emp address,emp dept,emp mail.emp ID is the primary key.pls help me on this..

Upvotes: 0

Views: 1078

Answers (2)

Vivek Krishna
Vivek Krishna

Reputation: 1

place this code in gridview Rowdeleting event

GridViewRow row = (GridViewRow)gv_Type.Rows[e.RowIndex];
d.con_Open();
SqlCommand cmd = new SqlCommand("Delete from tbl_Category where Catno=" + Convert.ToInt32(gv_Type.Rows[e.RowIndex].Cells[1].Text) + "", d.con);
cmd.ExecuteNonQuery();
d.con_Close();

Upvotes: 0

Muhamed Shafeeq
Muhamed Shafeeq

Reputation: 1214

If you are using database ,then create functions for Update and Delete Eg: Public void Update(DataClass dataclass){...} public void Delete(int Id){...}

Write these following code in your apsx page

                <Columns>
                    <asp:CommandField ShowDeleteButton="True" ShowEditButton="True" />
                </Columns>

            </asp:GridView>
            <asp:ObjectDataSource ID="ObjectDataSource1" runat="server" 
                DataObjectTypeName="DataClass" DeleteMethod="Delete" 
                TypeName="YourServiceClass" UpdateMethod="Update">
                <DeleteParameters>
                    <asp:Parameter Name="id" Type="Int32" />
                </DeleteParameters>
            </asp:ObjectDataSource>

Upvotes: 2

Related Questions