Results 1 to 7 of 7
  1. #1
    Lounger
    Join Date
    May 2006
    Posts
    33
    Thanks
    0
    Thanked 0 Times in 0 Posts

    multi-level datagrids? (vb.net)

    While still working on my previous login hurdle (I see the post is still up on top), I've started doing some other parts of the guts of the site, and need to create a 2-tier datagrid or something of the sort... you know, pull categories from a database which are listed with their children underneath them like so:
    Category A
    - Item in category A
    - Another item
    Category B
    - Item in category B
    - More rows

    something like that.
    Now, I did some searching and the best article I could come up with was here:
    http://www.howtodothings.com/computers/a14...l-datagrid.html
    But first of all, it's in C# and I am using vb... and second of all, are you friggin' kidding me? This seems so rediculously overcomplicated for such a simple task. Hopefully there is eome more obvious and straightforward way to accomplish this task? I know that this is something that could be accomplished in something like PHP in around 10 minutes... it's only 2 levels of recursion.
    If this article really uses the easiest and best way to do it, then I might benefit from a little english translation [img]/forums/images/smilies/smile.gif[/img]
    Grateful as always for the assistance!

  2. #2
    Silver Lounger
    Join Date
    Jan 2001
    Location
    Indianapolis, Indiana, USA
    Posts
    1,862
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: multi-level datagrids? (vb.net)

    Hi Lancer,

    Yep - that article is essentially the way it's done. But it doesn't really have to be too complicated. Here's my high-level overview:
    - Build a Parent Datagrid, including all of the columns you desire
    - Create a hidden column that contains the primary key (for passing to the child record)
    - Create a template column that will hold the child datagrid
    - Add the Child Datagrid inside the template column of the parent datagrid. It may be helpful to build it outside the parent datagrid first, then copy/paste to get it there

    In the codebehind, do the following:
    - Pull one dataset (or data table) with the parent records
    - Pull a second dataset (or data table) with the child records (be sure to include the foreign key that joins it to the parent data)
    - In the ItemDataBound event of the Parent datagrid, you'll need to make references to 2 items that are located in the datagrid item - the parent record primary key from the hidden column and the child datagrid itself. Use the parent record's primary key as a filter (you can use a Data View) for the child dataset/table. Simply set the data source of the child datagrid and call its databind method.
    - Don't forget to set the data source and call DataBind for the parent grid in the OnLoad (or subsequently called) event.

    There may be an easier way with .NET 2.0, but that's my "old-school" method for .NET 1.1.

    You might also check out http://www.4guysfromrolla.com for some great articles.

    Finally, check <!help=19>Help 19<!/help> for info on creating linked URLs in the forum. It's super easy ( [ url ] your url [ /url ] - without the spaces).

    Hope this helps.

  3. #3
    Lounger
    Join Date
    May 2006
    Posts
    33
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: multi-level datagrids? (vb.net)

    (ah, link parsing! I will use that in the future)
    Your explanation does make it sound a little bit better-- kind of weird in my previous searches that I didn't find any 4guysfromrolla articles.... however they do have some good stuff on the datagrid which looks like it could be used in my situation, good call [img]/forums/images/smilies/smile.gif[/img]
    It is just that extra step of passing the key value to the child grid that is the tricky part.

    Now here's an interesting question... a bit more of a challenge I think... if one were to build a calendar (with an unlimited number of "events" per calendar date), either via a repeater or a grid of some type, would a technique similar to this one be useful? I have a project like this on the backburner, and I will eventually need to figure it out. The easy way out would just be to query the database for each "calendar cell" or date... but that would be lame, and would be an unnecessarily huge drag on the db server... I just haven't come up with a good way to get away with using one query... would the use of a dataset like in this example be the way to go...?

  4. #4
    Silver Lounger
    Join Date
    Jan 2001
    Location
    Indianapolis, Indiana, USA
    Posts
    1,862
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: multi-level datagrids? (vb.net)

    Passing the key value to the child grid is pretty easy. All you need to do is read it from the DataGridItemEventArgs argument (usually passed as "e") of the ItemDataBound event. So your code would look something like:
    Sub Item_Bound(sender As Object, e As DataGridItemEventArgs)
    Dim parentPrimaryKey as Integer = Integer.Parse(e.Item.Cells(<Index_of_PrimaryKey_Co lumn_Mentioned_Earlier>))

    'Now use parentPrimaryKey to filter your child dataset and so forth...
    End Sub 'Item_Bound

    Note: I usually work with C#, so my syntax may be a bit off. Use at your own risk <img src=/S/evilgrin.gif border=0 alt=evilgrin width=15 height=15>

    As for a calendar, I trust you're planning to use the .NET Calendar control, right? (System.Web.UI.WebControls.WebControl.Calendar)

    Depending on how you manage the data, it doesn't have to be that bad of a load on the server. The idea is that you pull the data from the server into a cached dataset (as long as it's a reasonable number of rows), then check the dataset for any data that needs to be presented on the calendar as each day is rendered. I've done that before and it's surprisingly fast.

    Sounds like you're definitely thinking in the right direction!

  5. #5
    Lounger
    Join Date
    May 2006
    Posts
    33
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: multi-level datagrids? (vb.net)

    ah ha... sounds like this is the way to go after all... found a good tutorial on how to make just such a calendar widget on 4guysfromrolla... I think if I expand on it, it will do what I need it to... all seems to be well! : )
    thanks!

  6. #6
    Plutonium Lounger
    Join Date
    Dec 2000
    Location
    Sacramento, California, USA
    Posts
    16,775
    Thanks
    0
    Thanked 1 Time in 1 Post

    Re: multi-level datagrids? (vb.net)

    There are also 3rd party grids that support banding, which allows you to bind different bands to different tables related on the linking keys.
    Charlotte

  7. #7
    Lounger
    Join Date
    May 2006
    Posts
    33
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: multi-level datagrids? (vb.net)

    I think I have it figured out-- I tried the aforementioned method, and it seemed to work well, however I wasn't able to refer to the child grid effectively, and I couldn't figure out if I needed to use findcontrol(), since my attempts at doing so failed... so I wasn't able to get the primary key value communicated between the two... perhaps I am just missing something small...
    I did, however, create a child datagrid programatically (in the vb code instead of the aspx file), and added it to one of the cells in the parent grid, and it performs the same functionality as my front-side child datagrid would have done so far.
    I also assume there would also be a way to find a series of (numeric) text boxes within these child grids and post a sum of their values at the bottom...?
    I envision an "update total" button which fires an event that loops through each parent gridItem... and in that loop would use use findcontrol to locate the child datagrid, and would then loop through the child grid in a similar manner, adding to a running total, which would get stored in a label control in the footer of my parent grid...That should be the right way to go about doing so...?
    That seems to be my biggest pain with these different recursive controls with controls inside and such... it's sometimes hard to get ahold of what I need to modify.. arg!

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •