c# - ASP.NET GridView : How to edit and delete data records

标签 c# asp.net webforms aspxgridview

您好,我已经使用 gridview 创建了一个表。 有没有办法实现编辑和删除。 我以前用 PHP 做过。我想使用的方法是在表中再创建两列,每行都有编辑和删除按钮。然后,当单击按钮时,它会通过 URL 传递“id”并能够编辑或删除。不太确定如何在 asp.net webforms 中执行此操作。下面是我的表格代码。谢谢。

 <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="false">
<Columns>
    <asp:BoundField HeaderText="Surgery" DataField="surgery" />
    <asp:BoundField HeaderText="PatientID" DataField="patientID" />
    <asp:BoundField HeaderText="Location" DataField="location" />

</Columns>          

SqlCommand cmd = new SqlCommand("select surgery, patientID, location from details", conn);
SqlDataAdapter sda = new SqlDataAdapter(cmd);
DataTable dt = new DataTable();
sda.Fill(dt);

conn.Close();

GridView1.DataSource = dt;
GridView1.DataBind();

最佳答案

GridView 支持这些操作。您可以添加一个 CommandField,它将包含命令按钮或 LinkBut​​ton(您可以选择按钮类型并分配每个按钮的文本)。 patientID 字段应包含在 GridView 的 DataKeyNames 属性中,以便在需要更新或删除数据库中的记录时检索它。

<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="false"
    DataKeyNames="patientID" 
    OnRowEditing="GridView1_RowEditing" OnRowCancelingEdit="GridView1_RowCancelingEdit"
    OnRowUpdating="GridView1_RowUpdating" OnRowDeleting="GridView1_RowDeleting" >
<Columns>
    <asp:CommandField ShowEditButton="true" ShowCancelButton="true" ShowDeleteButton="true" />
    <asp:BoundField HeaderText="Surgery" DataField="surgery" />
    ...
</Columns>

然后您需要在代码隐藏中处理一些事件:

// The RowEditing event is called when data editing has been requested by the user
// The EditIndex property should be set to the row index to enter edit mode
protected void GridView1_RowEditing(object sender, GridViewEditEventArgs e)
{
    GridView1.EditIndex = e.NewEditIndex;
    BindData();
}

// The RowCancelingEdit event is called when editing is canceled by the user
// The EditIndex property should be set to -1 to exit edit mode
protected void GridView1_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
{
    GridView1.EditIndex = -1;
    BindData();
}

// The RowUpdating event is called when the Update command is selected by the user
// The EditIndex property should be set to -1 to exit edit mode
protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e)
{
    int patientID = (int)e.Keys["patientID"]
    string surgery = (string)e.NewValues["surgery"];
    string location = (string)e.NewValues["location"];

    // Update here the database record for the selected patientID

    GridView1.EditIndex = -1;
    BindData();
}

// The RowDeleting event is called when the Delete command is selected by the user
protected void GridView1_RowDeleting(object sender, GridViewDeleteEventArgs e)
{
    int patientID = (int)e.Keys["patientID"]

    // Delete here the database record for the selected patientID

    BindData();
}

由于数据必须在每个事件处理程序的末尾绑定(bind)到 GridView,您可以在 BindData 实用程序函数中执行此操作,该实用程序函数也应在页面最初加载时调用:

private void BindData()
{
    SqlCommand cmd = new SqlCommand("select surgery, patientID, location from details", conn);
    SqlDataAdapter sda = new SqlDataAdapter(cmd);
    DataTable dt = new DataTable();
    sda.Fill(dt);
    conn.Close();
    GridView1.DataSource = dt;
    GridView1.DataBind();
}

protected void Page_Load(object sender, EventArgs e)
{
    if (!IsPostBack)
    {
        BindData();
    }
}

关于c# - ASP.NET GridView : How to edit and delete data records,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36827111/

相关文章:

c# - 旋转图像导致内存不足异常

c# - 从解决方案中的文件夹加载图像?

c# - SortedDictionary 抛出带有两个不同条目的 "same key already exists"

javascript - 使用 JavaScript 显示/隐藏 Div 内容

Asp.net Web 表单和不同文件夹的基于角色的权限

c# - 智能卡 CMS 解密

c# - 在devexpress gridview Templates DetailRow中查找控件

c# - 创建通用用户ID的最佳实践

c# - 如何发布数据并重定向到外部页面?

css - 使用外部样式表设置页面上所有 .net 文本框实例的样式