DataItem is NULL, problem

  • Thread starter Thread starter Jeff
  • Start date Start date
J

Jeff

hi

asp.net 2.0

I have a GridView on a webpage. This GridView has a TemplateField which
again contain a LinkButton. When I click on that LinkButton I want some
action to take place on that row the LinkButton exists. When I click on the
LinkButton the lbCar_Command method is triggered. But in this line the
variable car gets a NULL value:
CarDetail car = (CarDetail)row.DataItem;
I thought DataItem existed for every row, as I'm using DataItem in the
RowDataBound

protected void gvwCars_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
CarDetail car = (CarDetail)e.Row.DataItem;
LinkButton button = (LinkButton)e.Row.FindControl("lbCar");
button.CommandArgument = e.Row.RowIndex.ToString();
}
}

protected void lbCar_Command(object sender, CommandEventArgs e)
{
int rowIndex = Int32.Parse(e.CommandArgument.ToString());
GridView grid = (GridView)LoginView1.FindControl("gvwCars");
GridViewRow row = grid.Rows[rowIndex];
CarDetail car = (CarDetail)row.DataItem;
}

(I don't want to create a separate field in the GridView with ButtonField
instead, as it will change the GUI of this GridView entirely, this GridView
have only one column but it's a TemplateField containing serveral controls
and divs)

Any suggestions?
 
okay I've kind of solved this problem. I made a workaround the problem.
instead of having rownumber stored in commandargument, I instead placed a
primary key value. Anyway it's this id I need in that command...
 
Back
Top