Passing a LINQ DataRow Reference in a GridView's ItemTemplate
SOLVED. Code has been edited to reflect solution.
Given the following GridView
:
<asp:GridView runat="server" ID="GridView1" AutoGenerateColumns="false" DataKeyNames="UniqueID"
OnSelectedIndexChanging="GridView1_SelectedIndexChanging" >
<Columns>
<asp:BoundField HeaderText="Remarks" DataField="Remarks" />
<asp:TemplateField HeaderText="Listing">
<ItemTemplate开发者_如何学Python>
<%# ShowListingTitle( Container.DataItem ) %>
</ItemTemplate>
</asp:TemplateField>
<asp:BoundField HeaderText="Amount" DataField="Amount" DataFormatString="{0:C}" />
</Columns>
</asp:GridView>
which refers to the following code-behind method:
protected String ShowListingTitle( object /* was DataRow */ row )
{
Listing listing = ( Listing ) row;
return NicelyFormattedString( listing.field1, listing.field2, ... );
}
The cast from DataRow
to Listing
was failing (cannot convert from DataRow
to Listing
) I'm certain the problem lies in what I'm passing from within the ItemTemplate, which is simply not the right reference to the current record from the LINQ to SQL data set that I've created, which looks like this:
private void PopulateGrid()
{
using ( MyDataContext context = new MyDataContext() )
{
IQueryable < Listing > listings = from l in context.Listings where l.AccountID == myAccountID select l;
GridView1.DataSource = listings;
GridView1.DataBind();
}
}
IIRC, you should just be calling Container.DataItem
, and that will be a Listing
.
Try it by doing <%# Container.DataItem.ToString() %>
as a test.
I havent used ASP.NET in years though, so memory might be rusty.
LINQ to SQL doesn't use the DataRow class at all; each entity is its own class and has no base class, so you can't cast a LINQ to SQL object as a DataRow...
Unless I am missing something, you can do:
<%# ShowListingTitle( ( ( Listing ) ( Container.DataItem ) ).Row ) %>
and
protected String ShowListingTitle( Listingrow )
{
return NicelyFormattedString( listing.field1, listing.field2, ... );
}
You can also attach to RowDataBound and access the current bound object via e.Row.DataItem, and then use this to supply the value to a cell via e.Row.Cells[] and supply a value to the text property (for a bound field) or use FindControl to supply it to a control.
<asp:GridView runat="server" ID="GridView1" AutoGenerateColumns="false" DataKeyNames="UniqueID"
OnSelectedIndexChanging="GridView1_SelectedIndexChanging" >
<Columns>
<asp:BoundField HeaderText="Remarks" DataField="Remarks" />
<asp:TemplateField HeaderText="Listing">
<
<%# ShowListingTitle( Convert.ToInt23(Eval("Field1")),Eval("Field2").ToString(),Eval("Field3").ToString() ) %>
</ItemTemplate>
</asp:TemplateField>
<asp:BoundField HeaderText="Amount" DataField="Amount" DataFormatString="{0:C}" />
</Columns>
</asp:GridView>
and change your method to:
protected String ShowListingTitle( int field1,string field2 )
{
Listing listing = ( Listing ) row;
return NicelyFormattedString( listing.field1, listing.field2, ... );
}
精彩评论