且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

ASP.NET的GridView:如何编辑和删除数据记录

更新时间:2023-10-15 21:32:04

GridView控件支持这些操作。您可以添加 CommandField中其中将包含命令按钮或LinkBut​​tons(你可以选择按钮的类型,并指定每个按钮的文本)。在 patientID 字段应该被包含在GridView的的DataKeyNames 属性,为了找回它在时机成熟时,以更新或删除数据库中的记录。

 < ASP:GridView控件ID =GridView1=服务器的AutoGenerateColumns =假
    的DataKeyNames =patientID
    OnRowEditing =GridView1_RowEditingOnRowCancelingEdit =GridView1_RowCancelingEdit
    OnRowUpdating =GridView1_RowUpdatingOnRowDeleting =GridView1_RowDeleting>
<柱体和GT;
    < ASP:CommandField中ShowEditButton =真ShowCancelButton =真的ShowDeleteButton =真/>
    < ASP:BoundField的的HeaderText =手术的DataField =手术/>
    ...
< /专栏>

您一定要去处理一些事件,code-背后:

  //当数据编辑已要求用户RowEditing事件被称为
//该EditIndex属性应设置为行索引进入编辑模式
保护无效GridView1_RowEditing(对象发件人,GridViewEditEventArgs E)
{
    GridView1.EditIndex = e.NewEditIndex;
    BindData();
}//当编辑被用户取消了RowCancelingEdit事件被称为
//该EditIndex属性应设置为-1,退出编辑模式
保护无效GridView1_RowCancelingEdit(对象发件人,GridViewCancelEditEventArgs E)
{
    GridView1.EditIndex = -1;
    BindData();
}//当由用户选择的更新命令被调用的RowUpdating事件
//该EditIndex属性应设置为-1,退出编辑模式
保护无效GridView1_RowUpdating(对象发件人,GridViewUpdateEventArgs E)
{
    INT patientID =(int)的e.Keys [patientID]
    字符串手术=(字符串)e.NewValues​​ [手术];
    字符串位置=(字符串)e.NewValues​​ [所在地];    //此处更新所选patientID数据库记录    GridView1.EditIndex = -1;
    BindData();
}//当由用户选择的删除命令被称为该RowDeleting事件
保护无效GridView1_RowDeleting(对象发件人,GridViewDeleteEventArgs E)
{
    INT patientID =(int)的e.Keys [patientID]    //删除这里的数据库记录所选patientID    BindData();
}

由于数据必须在每个这些事件处理程序结束时绑定到GridView,您可以在 BindData ​​ code>实用功能,这应该也可以称为做在加载页面时开始:

 私人无效BindData()
{
    CMD的SqlCommand =新的SqlCommand(选择手术,patientID,从细节位置,康恩);
    SqlDataAdapter的SDA =新SqlDataAdapter的(CMD);
    DataTable的DT =新的DataTable();
    sda.Fill(DT);
    conn.Close();
    GridView1.DataSource = DT;
    GridView1.DataBind();
}保护无效的Page_Load(对象发件人,EventArgs的发送)
{
    如果(!的IsPostBack)
    {
        BindData();
    }
}

Hi I have used gridview to create a table. Is there a way to implement edit and delete. I have done it in PHP before. The method I would like to use is create two more columns in the table with edit and delete buttons on each row. Then when the buttons are click it passes the 'id' through the URL and able to edit or delete. Not really sure how to do this in asp.net webforms. Below is my code for the table. Thank you.

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

The GridView supports those operations. You can add a CommandField which will contain the command buttons or LinkButtons (you can choose the type of button and assign the text of each button). The patientID field should be included in the DataKeyNames property of the GridView, in order to retrieve it when the time comes to update or delete the record in the database.

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

You will then need to handle a few events in code-behind:

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

Since the data must be bound to the GridView at the end of each of those event handlers, you can do it in a BindData utility function, which should also be called when the page loads initially:

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