简体   繁体   中英

Get column value an GridView

I have an GridView with button "Delete". I need get value to column or cell of index[3]. But with GridView1.SelectedRow.Cells[3].Text; it return null. Someone help me? I do not want use javascript.

My aspx:

<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="false" GridLines="None"
        CssClass="table table-bordered table-striped">
        <Columns>
            <asp:BoundField DataField="DisplayName" HeaderText="Display Name" />
            <asp:BoundField DataField="UserName" HeaderText="User Name" />
            <asp:BoundField DataField="Email" HeaderText="Email" />
            <asp:BoundField DataField="Name" HeaderText="Name" />
            <asp:TemplateField>
                <ItemTemplate>
                    <asp:Button ID="btnDelete" runat="server" Text="Delete" OnClick="btnDelete_OnClick" />
                </ItemTemplate>
            </asp:TemplateField>
        </Columns>
    </asp:GridView>

My .cs:

protected void btnDelete_OnClick(object sender, EventArgs e)
{
    using (objConexao = new SqlConnection(strStringConexao))
    {
        SqlConnection oConn = new SqlConnection();
        SqlCommand oCommand = new SqlCommand();
        string strConn = ConfigurationManager.ConnectionStrings["XXX"].ConnectionString;
        oConn.ConnectionString = strConn;
        oCommand.Connection = oConn;
        oCommand.CommandText = "proc_Delete";
        oCommand.CommandType = CommandType.StoredProcedure;

        SqlParameter myParam = oCommand.Parameters.Add("@UserRequestor", SqlDbType.NVarChar);
        myParam.Value = User;

        SqlParameter myParam2 = oCommand.Parameters.Add("@UserName", SqlDbType.NVarChar);
        myParam2.Value = GridView1.Columns[3].ToString();

        oConn.Open();
        oCommand.ExecuteNonQuery();
        oConn.Close();
    }
}

Try this:

protected void btnDelete_OnClick(object sender, EventArgs e)
{
    Button btn = (Button)sender;
    GridViewRow row = (GridViewRow)btn.NamingContainer
    string desiredText = row.Cells[3].Text;
    using (objConexao = new SqlConnection(strStringConexao))
    {
        // ...
        myParam2.Value = desiredText;
        // ...
    }        
}

NamingContainer will get you the row of the Button that was clicked

I don't think that the SelectedRow property worked because clicking a button does not constitute selecting a row. That is done with CommandName="Select" on a button, or with AutoGenerateSelectButton="true" on your GridView . This question provides an explanation.

It's been a while, but I know you can use datakeynames for this.

In the markup add a key for whatever value you want to access

<asp:GridView ID="GridView1" DataKeyNames="Email" runat="server" AutoGenerateColumns="false" GridLines="None"
    CssClass="table table-bordered table-striped">

Then you should be able to get the value with the row index as such.

GridView1.DataKeys[3].Value.ToString()

It's been a while, so you might have to play with this.

Here's an article with more information, and how to have more than one datakey

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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