用于更新和删除网格视图中的行的代码

时间:2012-07-24 08:49:53

标签: c# asp.net .net gridview

直接在网络表单中更新和保存网格视图数据的代码是什么。这是我的代码。帮我删除记录和更新记录。

   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)
{}

我需要编辑四个列Emp name,Emp地址,emp dept,emp mail.emp ID是主键.pls帮我这个..

2 个答案:

答案 0 :(得分:2)

如果您正在使用数据库,则为Update和Delete创建函数  例如:      公共无效更新(DataClass数据类){...}      public void删除(int Id){...}

在apsx页面中写下以下代码

                <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>

答案 1 :(得分:0)

将此代码放在gridview Rowdeleting事件

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();