开发者

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 gvDetail开发者_开发知识库s_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 ??


Try the NewValues collection of the gridviewupdateeventargs.

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

e.NewValues("Desc")
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜