Bound DataGrid using nested XML

Discussion in 'ASP .Net' started by Christopher Jedlicka, Nov 9, 2003.

  1. Hi,

    I am trying to use a datagrid control to display hierarchical data in
    a flat format essentially in a master-detail relationship. i.e.

    <Data>
    <Customer CustomerName="Customer1">
    <Order OrderNumber="1">
    <Item ItemName="Item1"></Item>
    <Item ItemName="Item2"></Item>
    </Order>
    <Order OrderNumber="2">
    <Item ItemName="Item3"></Item>
    <Item ItemName="Item4"></Item>
    </Order>
    </Customer>
    <Customer CustomerName="Customer2">
    <Order OrderNumber="3">
    <Item ItemName="Item1"></Item>
    <Item ItemName="Item2"></Item>
    </Order>
    </Customer>
    </Data>

    When I load the xml data into the dataset, .NET creates the
    relationships for me. My confusion happens when I bind this data to
    my datagrid. I have set AutoGenerateColumns to false and manually
    added each column. I can't seem to figure out how to set the
    DataField for relational columns. i.e.

    DataSet ds = new DataSet();
    DataGrid dg = new DataGrid();
    BoundColumn colCustomerName = new BoundColumn();
    BoundColumn colOrderNumber = new BoundColumn();
    BoundColumn colItemName = new BoundColumn();

    ds.ReadXml("Data.xml", XmlReadMode.InferSchema);

    colCustomerName.DataField =
    ds.Tables["Customer"].Columns["CustomerName"].toString();
    colOrderNumber.DataField =
    ds.Tables["Order"].Columns["OrderNumber"].toString();
    colItemName.DataField =
    ds.Tables[Item].Columns["ItemName"].toString();

    dg.DataSource = ds;
    dg.DataBind();

    I tried to build in the relationships by doing this a little bit
    different, but this didn't seem to work either:

    colCustomerName.DataField =
    ds.Relations["Customer_Order"].ParentTable.Columns["CustomerName"].toString();
    colOrderNumber.DataField =
    ds.Relations["Order_Item"].ParentTable.Columns["OrderNumber"].toString();
    colItemName.DataField = "ItemName";

    dg.DataSource = ds;
    dg.DataMember = "Item";
    dg.DataBind();

    How do I have to reference the DataSource and DataField properties to
    display this relational data? I'd be especially grateful for any
    assistance. Thanks.

    -Chris
     
    Christopher Jedlicka, Nov 9, 2003
    #1
    1. Advertising

  2. Christopher Jedlicka

    Natty Gur Guest

    Natty Gur, Nov 10, 2003
    #2
    1. Advertising

  3. Thanks.. I think the solution you offered will probably work for me.
    What I had really hoped to accomplish in this particular project
    though, was more of a "flat" display of the data using just a single
    datagrid. i.e.

    Customer Name | Order Number | Item Name
    --------------------------------------------
    Customer 1 | Order 1 | Item 1
    Customer 1 | Order 1 | Item 2
    Customer 1 | Order 2 | Item 3
    Customer 1 | Order 2 | Item 4
    Customer 2 | Order 3 | Item 1
    Customer 2 | Order 3 | Item 2

    Can this be done, or is it necessary to nest additional datagrids as
    in your example?

    -Chris

    Natty Gur <> wrote in message news:<O$>...
    > Hi,
    >
    > I think this link[1] will help you :
    >
    > [1] - Displaying Hierarchical data with the ASP.NET Data Grid
    > : http://www.wimdows.net/articles/article.aspx?aid=19
    >
    > Natty Gur[MVP]
    >
    > blog : http://weblogs.asp.net/ngur
    > Mobile: +972-(0)58-888377
    >
    >
    > *** Sent via Developersdex http://www.developersdex.com ***
    > Don't just participate in USENET...get rewarded for it!
     
    Christopher Jedlicka, Nov 10, 2003
    #3
  4. Christopher Jedlicka

    Natty Gur Guest

    Yes,

    You can always create your own dataset with datatable that holds your
    data (in certain format and form) and bind it to data grid.

    Natty Gur[MVP]

    blog : http://weblogs.asp.net/ngur
    Mobile: +972-(0)58-888377


    *** Sent via Developersdex http://www.developersdex.com ***
    Don't just participate in USENET...get rewarded for it!
     
    Natty Gur, Nov 11, 2003
    #4
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. sunil panda

    Lower bound & Upper bound

    sunil panda, Dec 25, 2003, in forum: Java
    Replies:
    9
    Views:
    7,355
    thushara wijeratna
    Oct 7, 2008
  2. Rhiner Dan
    Replies:
    1
    Views:
    773
    Mike Wahler
    Mar 27, 2005
  3. Mario Krsnic
    Replies:
    0
    Views:
    398
    Mario Krsnic
    Jun 23, 2006
  4. Phillip Vong
    Replies:
    0
    Views:
    433
    Phillip Vong
    Jul 27, 2006
  5. Christopher Jedlicka

    Bound DataGrid using nested XML

    Christopher Jedlicka, Nov 9, 2003, in forum: ASP .Net Datagrid Control
    Replies:
    0
    Views:
    122
    Christopher Jedlicka
    Nov 9, 2003
Loading...

Share This Page