DataItem is NULL, problem

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?
 
J

Jeff

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...
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,743
Messages
2,569,478
Members
44,899
Latest member
RodneyMcAu

Latest Threads

Top