Home > DeveloperSection > Forums > Get DataRow object from selected row in GridView
Norman Reedus
Norman Reedus

Total Post:45

Posted on    November-25-2014 10:03 PM

 ASP.Net Gridview 

 1 Reply(s)
 1000  View(s)
Rate this:

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);




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>

Kamlakar Singh
Kamlakar Singh

Total Post:194

Posted on    November-26-2014 12:14 AM

This is how I always get my information from a datagridview


    string variabel = yourDataGridView["Columnname"].ToString();

You can also use this in a loop then it will be:


    string variabel = yourDataGridView["RowNumber"].Cells["Columnname"].Value.ToString();

I hope this helps!

Don't want to miss updates? Please click the below button!

Follow MindStick