0

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();
    }
}
CaioVJesus89
  • 333
  • 1
  • 11
  • 24

2 Answers2

2

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.

Community
  • 1
  • 1
wlyles
  • 2,236
  • 1
  • 20
  • 38
  • @CaioVJesus89 I have added some more info to help you understand why your original code didn't work – wlyles Jul 24 '13 at 19:16
0

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

Smeegs
  • 9,151
  • 5
  • 42
  • 78
  • Occured an error "Index was out of range. Must be non-negative and less than the size of the collection. Parameter name: index" – CaioVJesus89 Jul 24 '13 at 18:46
  • I just tested the code and what I posted is correct. So your problem is elsewhere. You need to debug when the code hits. Is the GridView1.DataKeys array empty? Is the grid populated? Was this during an ajax postback? Is the datakey correct? Mainly, you have to make sure that there are rows in the grid, and the datakey array is not empty. – Smeegs Jul 24 '13 at 18:57