Link to home
Start Free TrialLog in
Avatar of andy12279
andy12279

asked on

get cell value from gridview with row index

I am trying to get the specific cell value from my GridView in the RowDataBound method.

But I am getting an error.  Please help?
protected void gvDisposition_RowDataBound(object sender, GridViewrowEventArgs e)
{
   if (e.Row.RowType == DataControlRowType.DataRow)
   {
     HyperLink editLink = (HyperLink)e.Row.FindControl("DispositionView");
     editLink.Attributes["href"] = "#";
     int index = e.Row.RowIndex ;
     GridViewrow row = gvDisposition.SelectedRow[index]; //get error with index is Zero
     string cellvalue = row.Cells[6].Text;
   }
}

Open in new window

ASKER CERTIFIED SOLUTION
Avatar of Toms Edison
Toms Edison
Flag of India image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
see this is not correct method to get selected row .. because this method always call when your gridview bind to data ... this method is used only for when you append something to your grid ( ie. javascript) or set some value ...

its better try some different method .. like deleting, selection, Editing etc

giving you example on editing event

<asp:GridView ID="MyDataGrid" runat="server" AutoGenerateColumns="false" OnEditcommand="MyDataGrid_EditCommand">

and on codebehind file use this
public void MyDataGrid_EditCommand(object s, DataGridCommandEventArgs e)
{
    MyDataGrid.EditItemIndex = e.Item.ItemIndex;
    BindData(); // this method for rebind your grid
}
Avatar of andy12279
andy12279

ASKER

Thanks guys.  Both of your answers helped solve my problem .