简体   繁体   English

Gridview字段在RowUpdating命令上为空

[英]Gridview field empties on RowUpdating command

I currently am using two GridViews tied to different sqldatasources which both accept a value for each row using a textbox. 我目前正在使用绑定到不同sqldatasources的两个GridView,它们都使用文本框为每一行接受一个值。 When I go to edit and then update the other fields inside the row, the value which was filled by the textbox turns into an empty value. 当我去编辑然后更新行内的其他字段时,由文本框填充的值变成空值。 It was working properly before but after parametrising values as well as adding RowCancelingEdit methods etc it has stopped. 它在参数化参数以及添加RowCancelingEdit方法等之前但之后均已正常运行,但已停止。

What is it thats making the 'Name' value Null? 这是什么使“名称”值变为空?

protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e)
{            
    GridViewRow row = (GridViewRow)GridView1.Rows[e.RowIndex];
    String qry = "UPDATE [PlayerStatistics] SET [Name] = @Name, [Apps] = @Apps, [Minutes] = @Minutes, [Goals] = @Goals, [Assists] = @Assists, [Yellows] = @Yellows, [Reds] = @Reds WHERE [PlayerID] = @original_PlayerID";
    using (SqlCommand cmd = new SqlCommand(qry, con))
    {
        cmd.Parameters.AddWithValue("@Name", SqlDbType.VarChar);
        cmd.Parameters.AddWithValue("@Apps", SqlDbType.Int);
        cmd.Parameters.AddWithValue("@Minutes", SqlDbType.Int);
        cmd.Parameters.AddWithValue("@Goals", SqlDbType.Int);
        cmd.Parameters.AddWithValue("@Assists", SqlDbType.Int);
        cmd.Parameters.AddWithValue("@Yellows", SqlDbType.Int);
        cmd.Parameters.AddWithValue("@Reds", SqlDbType.Int);
        cmd.Parameters.AddWithValue("@original_PlayerID", SqlDbType.Int);
        cmd.CommandType = CommandType.Text;
        con.Open();
        cmd.ExecuteNonQuery();
    }
}

Markup: 标记:

<asp:GridView ID="GridView1" runat="server" AllowSorting="True" AutoGenerateColumns="False" DataKeyNames="PlayerID" DataSourceID="SqlDataSource1" EmptyDataText="Please enter player(s) into the table." OnRowDeleted="GridView1_RowDeleted" OnRowDeleting="GridView1_RowDeleting1" Width="100%" OnRowUpdating="GridView1_RowUpdating" OnRowCancelingEdit="GridView1_RowCancelingEdit">
    <Columns>
        <asp:BoundField DataField="Name" HeaderText="Name" ReadOnly="True" SortExpression="Name" >
        <ControlStyle Width="100%" />
        </asp:BoundField>
        <asp:BoundField DataField="Apps" HeaderText="Apps" SortExpression="Apps" >
        <ControlStyle Width="100%" />
        </asp:BoundField>
        <asp:BoundField DataField="Minutes" HeaderText="Minutes" SortExpression="Minutes" >
        <ControlStyle Width="100%" />
        </asp:BoundField>
        <asp:BoundField DataField="Goals" HeaderText="Goals" SortExpression="Goals" >
        <ControlStyle Width="100%" />
        </asp:BoundField>
        <asp:BoundField DataField="Assists" HeaderText="Assists" SortExpression="Assists" ControlStyle-Width="100%" >
        <ControlStyle Width="100%" />
        </asp:BoundField>
        <asp:BoundField DataField="Yellows" HeaderText="Yellows" SortExpression="Yellows" >
        <ControlStyle Width="100%" />
        </asp:BoundField>
        <asp:BoundField DataField="Reds" HeaderText="Reds" SortExpression="Reds" >
        <ControlStyle Width="100%" />
        </asp:BoundField>
        <asp:TemplateField HeaderText ="Edit/Delete">
            <ItemTemplate>
                <asp:LinkButton ID="BtnEdit" runat="server" CausesValidation="false" CommandName="Edit" Text="Edit" />
                <span onclick="return confirm ('Are you Sure?')" >
                    <asp:LinkButton ID="BtnDelete" runat="server" CausesValidation="False" CommandName="Delete" Text="Delete"/>
                </span>
            </ItemTemplate>
            <EditItemTemplate>
                <asp:Button ID="BtnUpdate" runat="server" CausesValidation="false" CommandName="Update" ConflictDetection="OverwriteChanges" Text="Update" />
                <asp:Button ID="BtnCancel" runat="server" CausesValidation="false" CommandName="Cancel" ConflictDetection="OverwriteChanges" Text="Cancel" />
            </EditItemTemplate>
            <ControlStyle Width="100%" />
        </asp:TemplateField>
    </Columns>
</asp:GridView>


Updated code 更新的代码

Using a stored procedure: 使用存储过程:

protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e)
{
    GridViewRow row = (GridViewRow)GridView1.Rows[e.RowIndex];
    //String qry = "UPDATE [PlayerStatistics] SET [Name] = @Name, [Apps] = @Apps, [Minutes] = @Minutes, [Goals] = @Goals, [Assists] = @Assists, [Yellows] = @Yellows, [Reds] = @Reds WHERE [PlayerID] = @original_PlayerID";
    using (SqlCommand cmd = new SqlCommand("UpdatePlayer", con))
    {
        //cmd.Parameters.AddWithValue("@Name", GridView1.DataKeys[e.RowIndex].Values["Name"]);
        //cmd.Parameters.AddWithValue("@Name", SqlDbType.VarChar);
        cmd.Parameters.AddWithValue("@Apps", e.NewValues["Apps"]);
        cmd.Parameters.AddWithValue("@Minutes", e.NewValues["Minutes"]);
        cmd.Parameters.AddWithValue("@Goals", e.NewValues["Goals"]);
        cmd.Parameters.AddWithValue("@Assists", e.NewValues["Assists"]);
        cmd.Parameters.AddWithValue("@Yellows", e.NewValues["Yellows"]);
        cmd.Parameters.AddWithValue("@Reds", e.NewValues["Reds"]);
        cmd.Parameters.AddWithValue("@PlayerID", GridView1.DataKeys[e.RowIndex].Values["PlayerID"]);
        cmd.CommandType = CommandType.StoredProcedure;
        con.Open();
        cmd.ExecuteNonQuery();
    }
    GridView1.EditIndex = -1;
    GridView1.DataBind();
}

The second argument of AddWithValue is the value, not the data type. AddWithValue的第二个参数是值,而不是数据类型。 And since the Name field is not editable in your GridView, you can remove it from the update query and from the parameters. 并且由于“ Name字段在GridView中不可编辑,因此您可以将其从更新查询和参数中删除。 Your event handler would look like this: 您的事件处理程序将如下所示:

using (SqlCommand cmd = new SqlCommand("UpdatePlayer", con))
{
    cmd.CommandType = CommandType.StoredProcedure;
    cmd.Parameters.AddWithValue("@PlayerID", GridView1.DataKeys[e.RowIndex].Values["PlayerID"]);
    cmd.Parameters.AddWithValue("@Apps", e.NewValues["Apps"]);
    cmd.Parameters.AddWithValue("@Minutes", e.NewValues["Minutes"]);
    ...
}

You should also bind the data to the GridView and reset the EditRowIndex at the end of the RowUpdating event handler: 您还应该将数据绑定到GridView并在RowUpdating事件处理程序的末尾重置EditRowIndex

GridView1.EditIndex = -1;
BindData();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM