Solved

Problem with HierarGrid dgi.DataItem = nothing

Posted on 2004-10-26
343 Views
Last Modified: 2012-06-21
Hello,

I have been attempting to implement Denis Bauers Hierargid for a project that I am currently working on.

Everything goes fine, if I use templatedatamode = singlerow.

I can happily bind the row that is passed to the user control to a text box. I do however need to use templatedatamode = table, to pass all of the rows associated with the detail block to the webcontrol, so that I can use a nested datagrid. This is where my problems start :(

The dataitem of the datagriditem is always nothing, I therefore can not then cast it into a dataset, for use with my datagrid. I have seen lots of examples of invalid casts - I do not get that however - it's simply that the datagriditem is always nothing.

Below is my code - I have included both the area where I set the template, and the code for the ascx web control, and i'd really appreciate any help that anyone can offer - as i've completly hit a brick wall with it at the moment.

Please note - I followed Denis's tutorial from ASPAlliance as closely as I could the only difference being that my dataset is populated from my database. Also I know that each row does have child rows - as it works if I dont use table mode but rather singlerow.

Private Sub DG1_TemplateSelection(ByVal sender As Object, ByVal e As DBauer.Web.UI.WebControls.HierarGridTemplateSelectionEventArgs) Handles DG1.TemplateSelection

        e.TemplateFilename = "Detail.ascx"

    End Sub

and now for the web control detail.ascx

Public Class Detail

    Inherits System.Web.UI.UserControl

#Region " Web Form Designer Generated Code "

    'This call is required by the Web Form Designer.

    <System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()

    End Sub

    Protected WithEvents DataGrid1 As System.Web.UI.WebControls.DataGrid

    'NOTE: The following placeholder declaration is required by the Web Form Designer.

    'Do not delete or move it.

    Private designerPlaceholderDeclaration As System.Object


    Private Sub Page_Init(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Init

        'CODEGEN: This method call is required by the Web Form Designer

        'Do not modify it using the code editor.

        InitializeComponent()

    End Sub

#End Region


    Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

        Try

            Dim dgi As DataGridItem = CType(Me.BindingContainer, DataGridItem)

            Dim ds As DataSet = CType(dgi.DataItem, DataSet)

            DataGrid1.DataSource = ds

            DataGrid1.DataMember = ds.Tables(0).TableName

            DataGrid1.DataBind()

        Catch ex As Exception

        End Try
       
    End Sub

End Class

0
Question by:LindzK
    5 Comments
     
    LVL 33

    Expert Comment

    by:raterus
    You realize how you currently have it, this code runs EVERY time the page loads/postsback.  If you are raising events from the datagrid, they will be erased when you rebind it like this.  I don't know anything about the HierarGrid (doubt anyhere here does), but I do know that people run into problems when they don't wrap their databinding code in page_load like this.  

        Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

            If Not Page.IsPostBack Then

            Try
                Dim dgi As DataGridItem = CType(Me.BindingContainer, DataGridItem)
                Dim ds As DataSet = CType(dgi.DataItem, DataSet)
                DataGrid1.DataSource = ds
                DataGrid1.DataMember = ds.Tables(0).TableName
                DataGrid1.DataBind()
            Catch ex As Exception
            End Try

            End If
           
        End Sub
    0
     
    LVL 33

    Expert Comment

    by:raterus
    wait wait wait, I just looked at your code...Me.BindingContainer?  That isn't an asp.net object but a winforms object!  Where did you get that?
    0
     
    LVL 5

    Author Comment

    by:LindzK
    There is a web control, that you can download from www.asp.net called the Hierargrid.

    It enables nested master/detail datagrids, and apart from the detail section - it functions just like a normal datagrid

    I do use if not page.ispostback on my main page - where i bind the first table in my dataset to the hierargrid.

    I'm sorry - I didnt' post that bit of code, the code I posted is from an ascx page, that you ahve to create which acts as a template for the detail ( second table area ) of the hierargrid.  So, although you dont see if not ispostback in the code I posted - it happens only once, as it's parent grid is formed correctly on the main aspx page.

    The hierargrid can either display each child row to it's own template - I did this, and it works correctly
    or have one row per child table of the parent rows.  This is where my problems happen.

    The code I posted, is taken straight from the tutorial on aspalliance written by the guy who wrote the hierargrid, who happens to also work for mircrosoft in Germany.

    I posted this question here - as it seems a lot of people use this grid, and I was sort of hoping someone else who'd used it, may recognise my problem, and have a solution for me.

    Sorry if I confused the matter -  by not explaining how I got the code I did.
    0
     
    LVL 5

    Author Comment

    by:LindzK
    btw I got the Me.BindingContainer bit from his tutorial, and it doesn't throw any errors or anything when I run through the code, it's just that the dataitem of the datagriditem, is nothing.

    I have now managed to do a sort of work around, to get the table out

    DataGrid1.DataSource = CType(Me.BindingContainer, DataGridItem).DataItem.DataView.DataViewManager.DataSet.Tables(1)
                DataGrid1.DataBind()

    That works fine - it eventually after iterating through a few components ( as you can see above ) it obtains the child datatable from the item that is passed to the asxc template.

    This seems rather cumbersome and messy though - and i'm still hoping to find out what the problem is with my implementation of how you are supposed to use this control.  Rather than my messy work around.

    I understand that not everyone will have come across the hierargrid control  - but I am still hoping that someone out there, knows how to implement it, and that I can get the solution working as intended.
    0
     

    Accepted Solution

    by:
    Closed, 500 points refunded.

    modulo
    Community Support Moderator
    Experts Exchange
    0

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone. Privacy Policy Terms of Use

    Featured Post

    Product Review - Android Remix

    Come along for the ride with our Senior Product Manager, Brian Matis, as he reviews the Android Remix.

    AJAX ModalPopupExtender has a required property "TargetControlID" which may seem to be very confusing to new users. It means the server control that will be extended by the ModalPopup, for instance, if when you click a button, a ModalPopup displays,…
    User art_snob (http://www.experts-exchange.com/M_6114203.html) encountered strange behavior of Android Web browser on his Mobile Web site. It took a while to find the true cause. It happens so, that the Android Web browser (at least up to OS ver. 2.…
    With the advent of Windows 10, Microsoft is pushing a Get Windows 10 icon into the notification area (system tray) of qualifying computers. There are many reasons for wanting to remove this icon. This two-part Experts Exchange video Micro Tutorial s…
    how to add IIS SMTP to handle application/Scanner relays into office 365.

    875 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    12 Experts available now in Live!

    Get 1:1 Help Now