Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get value of GridView Cell in RowCommand

I need to get the value of a cell from the RowCommand event, but the value is not in the PrimaryKeyNames parameter of the GridView.

Currently I have:

if (e.CommandName == "DeleteBanner")
        {
            GridViewRow row = gvCurrentPubBanner.SelectedRow;
            string BannerName = row.Cells[1].Text;

This doesn't work (index out of range error), I've also tried:

    int index = Convert.ToInt32(e.CommandArgument);
GridViewRow row = gvCurrentBanners.Rows[index];

This doesn't work either as the CommandArgument (the banner ID) is not the row ID.

like image 426
DarrylGodden Avatar asked May 28 '12 11:05

DarrylGodden


3 Answers

@Romil: your code in C#:

protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
{
    GridViewRow row = (GridViewRow)(((LinkButton)e.CommandSource).NamingContainer);
    int id = (int)GridView1.DataKeys[row.RowIndex].Value;
}

(LinkButton is button in template field).

like image 108
Chưa biết Avatar answered Oct 20 '22 21:10

Chưa biết


Dim row As GridViewRow = CType(CType(e.CommandSource, Control).NamingContainer, GridViewRow)

Then get the key or get the cell and cast to a datacontrolfield.

Dim id As Guid = GridView1.DataKeys(row.RowIndex).Value

Dim email As String = CType(row.Cells(2), DataControlFieldCell).Text

Remark: this only works with Boundfields.

like image 32
Romil Kumar Jain Avatar answered Oct 20 '22 20:10

Romil Kumar Jain


Try out this one..

GridViewRow gvRow = (GridViewRow)(((ImageButton)e.CommandSource).NamingContainer);
int rowIndex=gvRow.RowIndex
like image 37
Bijoy K Jose Avatar answered Oct 20 '22 20:10

Bijoy K Jose