C# 从onrowupdating GridView事件获取行值

C# 从onrowupdating GridView事件获取行值,c#,asp.net,C#,Asp.net,嗨,我正试着以编程方式对网格行进行一些更新。。。到目前为止我有这个 <asp:TextBox ID="TextBox1" runat="server"></asp:TextBox><%--to test cmd str--%> <asp:GridView ID="_gv_Cus" runat="server" AutoGenerateColumns="False" Auto

嗨,我正试着以编程方式对网格行进行一些更新。。。到目前为止我有这个

            <asp:TextBox ID="TextBox1" runat="server"></asp:TextBox><%--to test cmd str--%>
            <asp:GridView ID="_gv_Cus" runat="server" AutoGenerateColumns="False" 
                AutoGenerateEditButton="True" style="font-size: medium" onrowupdating="gvDetails_RowUpdating" 
                onrowediting="gvDetails_RowEditing" onrowcancelingedit="gvDetails_RowCancelingEdit" >
                <HeaderStyle BackColor="#57768f" ForeColor="White" />
                <RowStyle BackColor="#dae2e8" ForeColor="Black" HorizontalAlign="left" />
                <AlternatingRowStyle BackColor="#ffffff" ForeColor="Black" />
                <Columns>
                    <asp:BoundField DataField="code" HeaderText="code" ReadOnly="True" SortExpression="code" Visible="false" />
                    <asp:BoundField DataField="Desc" HeaderText="Decription" SortExpression="Desc" >
                    <ItemStyle HorizontalAlign="Left" />
                    </asp:BoundField>
                </Columns>
            </asp:GridView>

如何在OnRowUpdateGridView事件中获取行值???

尝试gridviewupdateeventargs的NewValues集合


您的代码极易受到SQL注入攻击。看一看下面的文章(密切关注第3步。在动态SQL中使用参数)
// Having Problems Here 
protected void gvDetails_RowUpdating(object sender, GridViewUpdateEventArgs e)
{
    var tbl = dd_CusCodes.SelectedValue;
    using (var con = new SqlConnection())
    {
        con.ConnectionString = ConfigurationManager.ConnectionStrings["RM_V1.0CS"].ConnectionString;

        int index = Convert.ToInt32(e.RowIndex);
        GridViewRow row = _gv_Cus.Rows[index];
        string code = row.Cells[0].Text;
        string desc = row.Cells[1].Text;

        con.Open();
        var cmd = new SqlCommand("update " + tbl + " set [Desc]='" + desc + "' where code=" + code, con) {CommandType = CommandType.Text};
        //TextBox1.Text = cmd.CommandText;
        cmd.ExecuteNonQuery();
        con.Close();

        _gv_Cus.EditIndex = -1;
        BindGrid();
    }
}

protected void gvDetails_RowEditing(object sender, GridViewEditEventArgs e)
{
    _gv_Cus.EditIndex = e.NewEditIndex;
    BindGrid();
}

protected void gvDetails_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
{
    _gv_Cus.EditIndex = -1;
    BindGrid();
}

protected void BindGrid()
{
    var tbl = dd_CusCodes.SelectedValue;

    if (tbl != string.Empty)
    {

        using (var con = new SqlConnection())
        {
            con.ConnectionString = ConfigurationManager.ConnectionStrings["RM_V1.0CS"].ConnectionString;
            con.Open();

            try
            {
                var cmd = new SqlCommand("SELECT [code] " +
                                         ",[desc] " +
                                         "FROM [dbo].[" + tbl + "] order by [desc]", con) { CommandType = CommandType.Text };

                _tx_Recruit.Text = cmd.CommandText.ToString();

                var read = cmd.ExecuteReader();
                _gv_Cus.DataSource = read;
                _gv_Cus.DataBind();
                read.Close();
            }
            catch (Exception ex)
            {
                ErrHandler.WriteError(ex.ToString());
            }
            finally
            {
                con.Close();
            }
        }
    }
}
e.NewValues("Desc")