Flávio Costa
Flávio Costa

Reputation: 957

How to get the value of a cell specifies the GridView ASP.NET

I need to get the value of a cell of a GridView. When the user click on the grid, I need to know which line button, and then get the value of the cell. I'm trying to use the following code, but not work.

 protected void gvSearch_RowCommand(object sender, GridViewCommandEventArgs e)
    {
        if (e.CommandName == "Select")
        {
            int row = Convert.ToInt32(e.CommandArgument);

            string tst = gvSearch.Rows[row].Cells[1].Text;
        }
    }

I do not know if I'm using the right event to get on my goal. If someone can help me, I thank you.

Upvotes: 1

Views: 20093

Answers (2)

Esmaeilkhan
Esmaeilkhan

Reputation: 16

You can use this code

protected void gvSearch_RowCommand(object sender, GridViewCommandEventArgs e)
{
    if (e.CommandName == "Select")
    {
        string pc = gVSearch.Rows[int.Parse(e.CommandArgument.ToString())].Cells[1].Text;
    }
}

Upvotes: 0

Flávio Costa
Flávio Costa

Reputation: 957

I found a solution!

Am using BoundField.

<asp:BoundField HeaderText="Código Área" DataField=" <asp:BoundField HeaderText="Denominação Área" DataField="areaName" />

protected void gvSearch_SelectedIndexChanged(object sender, EventArgs e)
    {
        String lastName = gvSearch.SelectedRow.Cells[1].Text;
    }

Thanks everyone!

Upvotes: 2

Related Questions