开发者

Traversing DataRelations in ASP.NET Gridview with ADO.NET and .NET 4

When one has a Gridview a开发者_StackOverflow中文版nd two datatables and one wishes to get the parent row and bind it to a gridview, how would this be done?


I seemed to resolve the issue. One can cast the datasource to the typed dataset and bind the linq object to the column. A sample solution follows:

    protected void Page_Load(object sender, EventArgs e)
    {
        BindGridView();
    }

    private void BindGridView()
    {
        DataSet1 ds = new DataSet1();
        DataSet1TableAdapters.deb_MenuRecipeTableAdapter mr = new DataSet1TableAdapters.deb_MenuRecipeTableAdapter();
        DataSet1TableAdapters.deb_MenuRecipeCategoryTableAdapter mrc = new DataSet1TableAdapters.deb_MenuRecipeCategoryTableAdapter();
        DataSet1TableAdapters.rec_RecipeCategoryTableAdapter rc = new DataSet1TableAdapters.rec_RecipeCategoryTableAdapter();
        mr.Fill(ds.deb_MenuRecipe);
        mrc.Fill(ds.deb_MenuRecipeCategory);
        rc.Fill(ds.rec_RecipeCategory);

        GridView1.DataSource = ds;
        GridView1.DataMember = ds.deb_MenuRecipe.TableName;
        GridView1.DataBind();
    }

On can then resolve the reference through the dataset using Server tags:

<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="false" 
        AllowPaging="true" BorderStyle="Dotted" 
        onpageindexchanging="GridView1_PageIndexChanging" PageSize="1" >
<Columns>
    <asp:BoundField DataField="MenuRecipeID" />
    <asp:TemplateField>
    <ItemTemplate>
         <asp:Label ID="lblName" runat="server" Text='<%# ((TestingPlatform.DataSet1) GridView1.DataSource).deb_MenuRecipe[Container.DataItemIndex].deb_MenuRecipeCategoryRow.MenuRecipeCategoryID %>' />
         </ItemTemplate>
    </asp:TemplateField>
    <asp:TemplateField>
    <ItemTemplate>
         <asp:Label ID="lblName" runat="server" Text='<%# ((TestingPlatform.DataSet1) GridView1.DataSource).deb_MenuRecipe[Container.DataItemIndex].deb_MenuRecipeCategoryRow.rec_RecipeCategoryRow.RecipeCategory %>' />
         </ItemTemplate>
    </asp:TemplateField>
</Columns>
</asp:GridView>

Please note that in the solution the Namespace that the project and aspx file exists in is TestingPlatForm. The Gridview datasource is then Cast to the typed Dataset type and we traverse the row that is being bound by the gridview through the linq extensions for the relations.

I hope this solution is of use to others who may still be using ADO.NET.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜