在运行时编辑gridview

本文关键字:gridview 编辑 运行时 | 更新日期: 2023-09-27 18:17:25

我需要从数据库中检索记录并在网格视图中显示它。然后我需要编辑那个特定的记录。但是,当我尝试这样做时,出现以下错误:

" GridView 'GridView1'触发了未处理的RowEditing事件。"

任何帮助都将非常感激。我正在尝试的代码如下:

ASP页面:

<div>
    <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" EnableModelValidation="True" >
        <Columns>
            <asp:BoundField DataField="empid" HeaderText="id" />
            <asp:TemplateField HeaderText="name">
                <EditItemTemplate>
                    <asp:TextBox ID="TextBox2" runat="server" Text='<%# Eval("name") %>'></asp:TextBox>
                </EditItemTemplate>
                <ItemTemplate>
                    <asp:Label ID="Label1" runat="server" Text='<%# Eval("name") %>'></asp:Label>
                </ItemTemplate>
            </asp:TemplateField>
            <asp:TemplateField HeaderText="age">
                <EditItemTemplate>
                    <asp:TextBox ID="TextBox3" runat="server" Text='<%# Eval("age") %>'></asp:TextBox>
                </EditItemTemplate>
                <ItemTemplate>
                    <asp:Label ID="Label2" runat="server" Text='<%# Eval("age") %>'></asp:Label>
                </ItemTemplate>
            </asp:TemplateField>
            <asp:TemplateField HeaderText="gender">
                <EditItemTemplate>
                    <asp:TextBox ID="TextBox4" runat="server" Text='<%# Eval("gender") %>'></asp:TextBox>
                </EditItemTemplate>
                <ItemTemplate>
                    <asp:Label ID="Label3" runat="server" Text='<%# Eval("gender") %>'></asp:Label>
                </ItemTemplate>
            </asp:TemplateField>
            <asp:TemplateField HeaderText="mobile">
                <EditItemTemplate>
                    <asp:TextBox ID="TextBox5" runat="server" Text='<%# Eval("mobile") %>'></asp:TextBox>
                </EditItemTemplate>
                <ItemTemplate>
                    <asp:Label ID="Label4" runat="server" Text='<%# Eval("mobile") %>'></asp:Label>
                </ItemTemplate>
            </asp:TemplateField>
            <asp:TemplateField HeaderText="qual">
                <EditItemTemplate>
                    <asp:TextBox ID="TextBox6" runat="server" Text='<%# Eval("qual") %>'></asp:TextBox>
                </EditItemTemplate>
                <ItemTemplate>
                    <asp:Label ID="Label5" runat="server" Text='<%# Eval("qual") %>'></asp:Label>
                </ItemTemplate>
            </asp:TemplateField>
            <asp:CommandField ShowEditButton="true" ShowCancelButton="true" />
        </Columns>
    </asp:GridView>
    <asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
    <asp:Button ID="Button1" runat="server" OnClick="Button1_Click" Text="Button" />
    <asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:testdb%>" UpdateCommand="update [empdetails] SET [name]=@name,[age]=@age,[gender]=@gender,[mobile]=@mobile,[qual]=@qual WHERE [empid]=@id">
        <UpdateParameters>
            <asp:Parameter Name="name" type="String" />
            <asp:Parameter Name="age" Type="Int32" />
            <asp:Parameter Name="gender" Type="String" />
            <asp:Parameter Name="mobile" Type="Int32" />
            <asp:Parameter Name="qual" Type="String" />
        </UpdateParameters>
    </asp:SqlDataSource>
</div>

代码:

protected void Button1_Click(object sender, EventArgs e)
{
    SqlConnection con = new SqlConnection(constr);
    SqlCommand cmd = new SqlCommand("select * from empdetails where empid ='" + TextBox1.Text + "'", con);
    con.Open();
    SqlDataReader dr = cmd.ExecuteReader();
    GridView1.DataSource = dr;
    GridView1.DataBind();
    con.Close();
}

在运行时编辑gridview

通过像你这样显示编辑按钮:

<asp:CommandField ShowEditButton="true" ShowCancelButton="true" />

它期望在一个RowEditing事件中被处理。您需要添加此处理程序来设置正在编辑的行的索引。

<asp:GridView ID="GridView1" runat="server"
    AutoGenerateColumns="False"
    EnableModelValidation="True"
    OnRowEditing="GridView1_RowEditing">
protected void GridView1_RowEditing(object sender, GridViewEditEventArgs e)
{
    //Set the edit index.
    GridView1.EditIndex = e.NewEditIndex;
    //Bind data to the GridView control.
    BindData();
}

你应该了解ADO。. NET参数以及如何准备它们:

var data = new DataTable();
using (var conn = new SqlConnection(constr))
{
    try
    {
        conn.Open();
        var cmd = new SqlCommand("dbo.pr_EC_SportsGroups_Search_Students", conn);
        cmd.Text = "select * from empdetails where empid = @empId";

        var empId = TextBox1.Text;
        cmd.Parameters.AddWithValue("@empId", empId);
        //create sql adapter by passing command object
        var adapter = new SqlDataAdapter(cmd);
        //fill the data table
        adapter.Fill(data);
        //bind data
        GridView1.DataSource = data;
        GridView1.DataBind();
    }
    catch (Exception ex)
    {
        Response.Write(ex.ToString());
    }
}

对于错误本身,您只需要将事件添加到标记中,参见@j。