Get DataRow object from selected row in GridView

Total Post:45

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

  1. Post:194

    Re: Get DataRow object from selected row in GridView

    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!

      Modified On Apr-07-2018 12:57:36 AM