views:

234

answers:

3

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>
        <%# 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();
    }
}
+1  A: 

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.

leppie
That's all it took! Thank you! I'm impressed at how it's so straightforward using LINQ vs other kinds of DataSets.
Bob Kaufman
@Bob Kaufman: Of course it is simple :) You just need to know how.
leppie
A: 

Hey,

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.

Brian
A: 

<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, ... );
}

lakhlaniprashant.blogspot.com
Good idea, but I am working with several fields from Listing and want to preserve their data types.
Bob Kaufman
This method is just and example, you can take any parameters to the method
lakhlaniprashant.blogspot.com