LinqDataSouce - DataItem in code behind

Version : Visual Studio 2008 Beta 2

The following examples shows how to use the GridViewRow.DataItem property to retrieve a property of the underlying object to which the GridViewRow is bound when using the LinqDataSource control.

I have a GridView whose DataSouce is a LinqDataSource. The LinqDataSource has been set to retrieve all records from the Product entity in the Northwind Linq to SQL data model like so:

<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" DataSourceID="LinqDataSource1"
 OnRowDataBound="GridView1_RowDataBound">
<Columns>
<asp:BoundField DataField="ProductID" HeaderText="ProductID" ReadOnly="True"></asp:BoundField>
<asp:BoundField DataField="ProductName" HeaderText="ProductName" ReadOnly="True"></asp:BoundField>
</Columns>
</asp:GridView>
<asp:LinqDataSource ID="LinqDataSource1" runat="server" ContextTypeName="NorthwindDataContext"
 TableName="Products">
</asp:LinqDataSource>

In this case, getting a property of the underlying data object is easy. The LinqDataSource is aware that each object in the result set is of Type Product (as defined in the Linq to SQL class) and creates it accordingly. So all we have to do is cast the DataItem to type Product and then retrieve the productName:

protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
 if (e.Row.RowType == DataControlRowType.DataRow)
 {
   Product p = (Product) e.Row.DataItem;
   string productName = p.ProductName;
 }
}

The LinqDataSource also allows you to shape the data that is retrieved. In the example below,  I have specified a declarative select to retrieve only a subset of the values from the Products entity - the ProductID and ProductName.

<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" DataSourceID="LinqDataSource1"
 OnRowDataBound="GridView1_RowDataBound">
 <Columns>
 <asp:BoundField DataField="ProductID" HeaderText="ProductID" ReadOnly="True"></asp:BoundField>
 <asp:BoundField DataField="ProductName" HeaderText="ProductName" ReadOnly="True"></asp:BoundField>
 </Columns>
 </asp:GridView>
 <asp:LinqDataSource ID="LinqDataSource1" runat="server" ContextTypeName="NorthwindDataContext"
 Select="new (ProductID, ProductName)" TableName="Products">
</asp:LinqDataSource>

Since we have defined a custom shape for our result, the LinqDataSouce will create an anonymous type for each object in the resultset like so:

x = {ProductID=17, ProductName=Alice Mutton}

Since we do not know what type to cast the DataItem to, we can use Reflection to retrieve the property. The DataBinder.Eval method already does this so we can retrieve the productName like so:

protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
 {
   if (e.Row.RowType == DataControlRowType.DataRow)
   {
     var x = e.Row.DataItem;
     string productName = (string) DataBinder.Eval(e.Row.DataItem, "ProductName");
   }
}

If you know of a non reflection or better way, please post a comment.

12 Comments

Comments have been disabled for this content.