简体   繁体   English

从onrowupdating GridView事件获取行值

[英]Get Row Values from onrowupdating GridView Event

Hi I'm trying to do some Updates on a grid row programatically ... I have this so far ... 嗨,我正在尝试以编程方式对网格行进行一些更新...到目前为止,我已经做到了...

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

Code Behind: 背后的代码:

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

How can I get the row values in the onrowupdating GridView Event ?? 如何在onrowupdating GridView事件中获取行值?

Try the NewValues collection of the gridviewupdateeventargs. 尝试使用gridviewupdateeventargs的NewValues集合。

http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.gridviewupdateeventargs.newvalues.aspx http://msdn.microsoft.com/zh-CN/library/system.web.ui.webcontrols.gridviewupdateeventargs.newvalues.aspx

e.NewValues("Desc")

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

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