forum

Home / DeveloperSection / Forums / Get DataRow object from selected row in GridView

Get DataRow object from selected row in GridView

Norman Reedus 2117 25-Nov-2014

I try to display a collection (IEnumerable) of objects (generated via Linq to Sql). Therefore I bind the Gridviews DataSource property to the generated output of my Linq to SQL method. In the SelectedIndexChanged event of the GridView I try to convert the selected rows DataItem back to my original object but end up with a null value instead. 

Here is my code: 

protected void Page_Load(object sender, EventArgs e)
{
    RlDataContext dc = new RlDataContext();
    this.dgvReports.DataSource = dc.GetReports(1);
    this.dgvReports.DataBind();
}
 
protected void dgvReports_SelectedIndexChanged(object sender, EventArgs e)
{
    if (this.dgvReports.SelectedIndex >= 0)
    {
        Report rpt = (Report)this.dgvReports.SelectedRow.DataItem;
    }
}
The return type of GetReports is ISingleResult<Report>


Updated on 26-Nov-2014

Can you answer this question?


Answer

1 Answers

Liked By