在 GridView 的 ItemTemplate 中传递 LINQ DataRow 引用

发布于 2024-08-26 16:58:35 字数 1411 浏览 8 评论 0原文

已解决。代码已被编辑以反映解决方案。

给定以下 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>

它引用以下代码隐藏方法:

protected String ShowListingTitle( object /* was DataRow */ row )
{
    Listing listing = ( Listing ) row;

    return NicelyFormattedString( listing.field1, listing.field2, ... );
}

DataRowListing 的转换失败(无法从 >DataRowListing)我确定问题在于我从 ItemTemplate 中传递的内容,这根本不是从 LINQ to SQL 到当前记录的正确引用我创建的数据集,如下所示:

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

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

半枫 2024-09-02 16:58:35

IIRC,您应该只调用 Container.DataItem,这将是一个 Listing

通过执行 <%# Container.DataItem.ToString() %> 作为测试来尝试一下。

不过我已经很多年没有使用 ASP.NET 了,所以记忆力可能已经生锈了。

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.

东走西顾 2024-09-02 16:58:35

LINQ to SQL 根本不使用 DataRow 类;每个实体都是它自己的类并且没有基类,因此您不能将 LINQ to SQL 对象转换为 DataRow...

除非我遗漏了某些内容,否则您可以这样做:

<%# ShowListingTitle( ( ( Listing ) ( Container.DataItem ) ).Row ) %>

并且

protected String ShowListingTitle( Listingrow )
{
    return NicelyFormattedString( listing.field1, listing.field2, ... );
}

您还可以附加到 RowDataBound 并访问当前通过 e.Row.DataItem 绑定对象,然后使用它通过 e.Row.Cells[] 向单元格提供值,并向文本属性提供值(对于绑定字段)或使用 FindControl 将其提供给控制。

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.

思慕 2024-09-02 16:58:35

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

并将你的方法更改为:


protected String ShowListingTitle( int field1,string field2 )
{
    Listing listing = ( Listing ) row;

    return NicelyFormattedString( listing.field1, listing.field2, ... );
}


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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文