Nested Gridview passing parameters from GV to GV

Last post 07-19-2008 9:34 AM by Freakyuno. 8 replies.

Sort Posts:

  • Nested Gridview passing parameters from GV to GV

    08-19-2007, 11:31 AM
    • Loading...
    • JWalker67
    • Joined on 07-27-2007, 2:02 AM
    • Posts 50
    I am trying to nest two gridviews. But the problem I am having is trying to figure out how to pass a parameter to the ObjectDataSource the the 2nd GV is using. I was trying to use OnRowDataBound on 1st GV, but I am not sure how to get access to the ODS the 2nd GV is using so that I can pass the "id" parameter. I was also looking into the 2nd GV Selecting event, but I don't know the syntax to get access to the 1st GV info to get the value of the current rows id. Visually this is what I am trying to get id name 3 test3 4 test4 id name 5 test5
  • Re: Nested Gridview passing parameters from GV to GV

    08-19-2007, 11:59 AM
    • Loading...
    • Freakyuno
    • Joined on 01-20-2005, 11:57 AM
    • Midwest - United States
    • Posts 1,952
    • Moderator
      TrustedFriends-MVPs

    You're going to want to be carefull doing it this way.  When using a nested gridview, rebinding your childs ODS on every row created in the Parent Gridview is a lot of calls to your DB, and thats multiplied by users.  It'd be easy to get 1000+ connections to your DB open at the same time.

    It's generally better to use whats called a DataRowView - with a DataRowView:  Here's a good article.

    http://programmers.wordpress.com/2006/07/26/nested-gridview-to-show-masterdetails-relationship-in-aspnet-20/

  • Re: Nested Gridview passing parameters from GV to GV

    08-19-2007, 1:34 PM
    • Loading...
    • JWalker67
    • Joined on 07-27-2007, 2:02 AM
    • Posts 50

    I am learning all this stuff as I go so thanks for the input.  I am not sure if what I want to do will be that DB intensive though.

    The final view I am looking for is simply a list of hyperlinks that all point to the same page (items.aspx).  The view I am trying to get is a something of a heirarchial view.  All the examples come very close but I just can't seem to get over the hump.  The view is some what of a Category -- Sub-Category type.

    If I could figure out how to post cost in here and not have the tags stripped it might help to make more sense.

    Category ID Category Name  (GV1)  
    1 cName1    
    2 cName2    
      Sub-CategoryID Sub-CategoryName (GV2)
      3 subCatName1
      4 subCatName2

    Ok, figured out how to post code so maybe this will help with what I am trying to create.  As you can see above there are NO sub-categories to display for cName1.  There are two (2) sub-categories for cName2.  The id=2 and I need to pass this to the objectdatasource in order to get the information that makes up the sub-category information.  If using my objectdatasource isn't the best way please give me a better one with example code. PLEASE

     

     

  • Re: Nested Gridview passing parameters from GV to GV

    08-19-2007, 3:41 PM
    • Loading...
    • Freakyuno
    • Joined on 01-20-2005, 11:57 AM
    • Midwest - United States
    • Posts 1,952
    • Moderator
      TrustedFriends-MVPs

    Lets do it this way, post me example data (with table names) for your 2 rows above, and your 2 subcategories (which appear to be a subcategory of the cName2 record).

    I'll work you up a quick code example. :)

  • Re: Nested Gridview passing parameters from GV to GV

    08-19-2007, 4:45 PM
    • Loading...
    • JWalker67
    • Joined on 07-27-2007, 2:02 AM
    • Posts 50

    First off...Thanks for the help!!

    Sample Data: There is only one table "categories". If I can get this part done, I think I can do the rest as all it is is hyperlinking each catergory to items.aspx page but I will post everything I can think of here.  Include some additional comments that maybe you can comment on. I have created a test page that passes in an ID and the items.apsx page does as expected.  This page & layout is the hurdle at the moment.  I have also created a page that shows just the "top" level categories ( 1 - 4 ) in the table below.  I have hyperlinked cName for each and when I click on the hyperlink it takes the associated ID and passes it to the items.apsx page as expected.

    Db Table:

    ID cName parentCategoryID
    1 cName1 -99
    2 cName2 -99
    3 cName3 -99
    4 cName4 -99
    5 cName5 1
    6 cName6 2
    7 cName7 4
    8 cName8 1
    9 cName9 8
    10 cName10 8
    11 cName11 4

    A parentCategoryID of -99 means this is a "top" level category.  So looking at the table above IDs 1 - 4 are the top most categories.  ID=5 is a sub-category of ID=1 (note its parentCategoryID = 1).  ID=8 is a sub-category of ID=1 and you will notice that ID=9 is a sub-category of ID=8.  I think you can figure out the rest of the hierarchial layout but the table below is what I want to display to the user.

     

    1 cName1    
      5 cName5  
      8 cName8  
        9 cName9
        10 cName10
    2 cName2    
      6 cName6  
    3 cName3    
    4 cName    
      7 cName7  
      11 cName11  

    Each cName# would be hyperlinked to items.apsx and would pass the ID to determine what items to display.

    In my research I found a posting about loading all the 'categories' on page load and then filtering that depending on the id that is passed in.  The other examples I have found about nested GV talk about the rowdatabound.

    I will say again, I am learning this as I go so I am open to anything.  And though I have used DAL and BLL, I did so following examples so that I could learn.  The problem I am running into now is that the nested GV examples don't show or use DAL & BLL, they access the DB directly in the page or code behind.  So I haven't found an example that would show how to update the select parameter for my ODS.  And I am not DB guru either so to hear that it might cause large DB connections raises a red flag.  Last but not least...being a newbie to all this I can't "talk" to much just yet.

    I hope this all makes sense and is usable in helping me find a solution.

    Thanks!!

  • Re: Nested Gridview passing parameters from GV to GV

    08-19-2007, 6:48 PM
    • Loading...
    • Freakyuno
    • Joined on 01-20-2005, 11:57 AM
    • Midwest - United States
    • Posts 1,952
    • Moderator
      TrustedFriends-MVPs

    Ahh, so its nested multiple times.  That gets to be a fun little trick, glad I asked for sample data because it kind of changes things. :)

    I'll work up an example here and show you one way to do it.  It's usually the way I handle multiple nestings in a single table.

  • Re: Nested Gridview passing parameters from GV to GV

    08-19-2007, 7:24 PM
    • Loading...
    • Freakyuno
    • Joined on 01-20-2005, 11:57 AM
    • Midwest - United States
    • Posts 1,952
    • Moderator
      TrustedFriends-MVPs

    K, give this a shot, see if it does what you want.  First I go through and setup my relationships. What I've done is moved to displaying the information in a treeview.  This may or may not be desirable for you - since it's not going to allow a lot of other information, but this should get the concept across.

     

    <asp:TreeView ID="TreeView1" runat="server" DataSourceID="XmlDataSource1">
    </asp:TreeView>
    
    <asp:XmlDataSource ID="XmlDataSource1" runat="server"></asp:XmlDataSource>

     

     Dim result As dataset = New Dataset
            Dim connString As String = ConfigurationManager.ConnectionStrings("CS").ConnectionString
            Using myConnection As New SqlConnection(connString)
                Dim query As String = "SELECT * FROM testing1"
                Dim myCommand As New SqlCommand(query, myConnection)
                Dim myAdapter As New SqlDataAdapter()
                myCommand.CommandType = CommandType.Text
                myAdapter.SelectCommand = myCommand
                myAdapter.Fill(result)
                myAdapter.Dispose()
            End Using
            result.DataSetName = "Categories"
            result.Tables(0).TableName = "Category"
            Dim relation As DataRelation = New DataRelation("ParentChild", result.Tables("Category").Columns("ID"), result.Tables("Category").Columns("parentCategoryID"), False)
            relation.Nested = True
            result.Relations.Add(relation)
            XmlDataSource1.Data = result.GetXml
            TreeView1.DataBind()
     
  • Re: Nested Gridview passing parameters from GV to GV

    07-17-2008, 2:11 PM
    • Loading...
    • HershieSquirts
    • Joined on 03-26-2008, 6:41 PM
    • Rochester, NY
    • Posts 19

    Can anyone post this code in c#?

  • Re: Nested Gridview passing parameters from GV to GV

    07-19-2008, 9:34 AM
    • Loading...
    • Freakyuno
    • Joined on 01-20-2005, 11:57 AM
    • Midwest - United States
    • Posts 1,952
    • Moderator
      TrustedFriends-MVPs

     

    dataset result = new Dataset(); 
        string connString = ConfigurationManager.ConnectionStrings("CS").ConnectionString; 
        using (SqlConnection myConnection = new SqlConnection(connString)) { 
            string query = "SELECT * FROM testing1"; 
            SqlCommand myCommand = new SqlCommand(query, myConnection); 
            SqlDataAdapter myAdapter = new SqlDataAdapter(); 
            myCommand.CommandType = CommandType.Text; 
            myAdapter.SelectCommand = myCommand; 
            myAdapter.Fill(result); 
            myAdapter.Dispose(); 
        } 
        result.DataSetName = "Categories"; 
        result.Tables(0).TableName = "Category"; 
        DataRelation relation = new DataRelation("ParentChild", result.Tables("Category").Columns("ID"), result.Tables("Category").Columns("parentCategoryID"), false); 
        relation.Nested = true; 
        result.Relations.Add(relation); 
        XmlDataSource1.Data = result.GetXml; 
        TreeView1.DataBind(); 
    
     
Page 1 of 1 (9 items)
Microsoft Communities
Page view counter