开发者

ASP.NET LINQ with Databinding

I have a collection of books as

static public Book[] Books =
    {
      new Book {
        Title="Funny Stories",
        Publisher=Publishers[0],
        Authors=new[]{Authors[0], Authors[1]},
        PageCount=101,
        Price=25.55M,
        PublicationDate=new DateTime(2004, 11, 10),
        Isbn="0-000-77777-2",
        Subject=Subjects[0]
      },
      new Book {
        Title="LINQ rules",
        Publisher=Publishers[1],
        Authors=new[]{Authors[2]},
        PageCount=300,
        Price=12M,
        PublicationDate=new DateTime(2007, 9, 2),
        Isbn="0-111-77777-2",
        Subject=Subjects[0]
      },
      new Book {
        Title="C# on Rails",
        Publisher=Publishers[1],
        Authors=new[]{Authors[2]},
        PageCount=256,
        Price=35.5M,
        PublicationDate=new DateTime(2007, 4, 1),
        Isbn="0-222-77777-2",
        Subject=Subjects[0]
      },
      new Book {
        Title="All your base are belong to us",
        Publisher=Publishers[1],
        Authors=new[]{Authors[3]},
        PageCount=1205,
        Price=35.5M,
        PublicationDate=new DateTime(2006, 5, 5),
        Isbn="0-333-77777-2",
        Subject=Subjects[2]
      },
      new Book {
        Title="Bonjour mon Amour",
        Publisher=Publishers[0],
        Authors=new[]{Authors[1], Authors[0]},
        PageCount=50,
        Price=29M,
        PublicationDate=new DateTime(1973, 2, 18),
        Isbn="2-444-77777-2",
        Subject=Subjects[1]
      }
    };

I have two to bind nested GridView (First GridView Takes Book Title,the nested GridView displays list of authors).

The Linq Query for Selecting Title and Authors is

List<Author> alist=new List<Author>();

var Authors = 
SampleData.Books.Select(c=>new {Title=c.Title,list=c.Authors.ToList() });

GridView1.DataSource = Authors;
GridView1.DataBind();              

and the HTML code is

<asp:GridView ID="GridView1" runat="server"  
        DataKeyNames="Title,list">
        <Columns>
            <asp:TemplateField HeaderText="Authors">
                <ItemTemplate>
                    <asp:GridView ID="GridView2" runat="server"
                     DataSource='<%# Eval("list") %>'>
                    </asp:GridView>
                </ItemTemplate>
            </asp:TemplateField>
        </Columns>
    </asp:GridView>

How to bind G开发者_开发知识库ridView1 and GridView2 when AutoGeneratedColumns is enabaled on both?


[This is a summary of the (partly deleted) comment discussion, to make sure that the question has an answer]

You need to add DataKeyNames="FirstName,LastName" (or whatever the properties of your Author class are called) to GridView2, so that the GridView knows which properties of your custom Author class it should bind to.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜