Your Privacy Matters: We use our own and third-party cookies to improve your experience on our website. By continuing to use the website we understand that you accept their use. Cookie Policy
100
Dynamic Setting Web hierarachical Data Source
posted

Hi,

 The below code sets the Web hierarchical Data Source with Web Hierarchical Data Grid. The Complete flow is given.

The code is wrieen in VB .NET.

 

 Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
       
        Dim webgriddataset As New System.Data.DataSet
        Dim sqlconn As SqlClient.SqlConnection
       
        sqlconn = New SqlClient.SqlConnection(ConfigurationManager.ConnectionStrings("AConnectionString").ToString())
        sqlconn.Open()
        Dim spName = "sp_Name"
        Dim sqlDA As SqlClient.SqlDataAdapter = New SqlClient.SqlDataAdapter(spName, sqlconn)
        sqlDA.Fill(webgriddataset, "T1")


        Dim WebHierarchicalDataSource1 As Infragistics.Web.UI.DataSourceControls.WebHierarchicalDataSource
        Dim datarel(5) As Infragistics.Web.UI.DataSourceControls.DataRelation
        WebHierarchicalDataSource1 = New Infragistics.Web.UI.DataSourceControls.WebHierarchicalDataSource

        datarel(0) = New Infragistics.Web.UI.DataSourceControls.DataRelation
        datarel(0).ParentDataViewID = "SqlDataSource1_view1"
        datarel(0).ChildDataViewID = "SqlDataSource2_view1"
        datarel(0).ParentColumns = datarel(0).ChildColumns


        Dim dataview1(5) As Infragistics.Web.UI.DataSourceControls.DataView
        dataview1(0) = New Infragistics.Web.UI.DataSourceControls.DataView
        dataview1(1) = New Infragistics.Web.UI.DataSourceControls.DataView
        dataview1(0).DataMember = "DefaultView"
        dataview1(1).DataMember = "DefaultView"
        dataview1(0).ID = "SqlDataSource1_view1"
        dataview1(1).ID = "SqlDataSource2_view1"

        dataview1(0).DataSource = webgriddataset.Tables(0)
        dataview1(1).DataSource = webgriddataset.Tables(1)
       

        Dim bf(5) As BoundDataField
       
        Dim i As Integer
        For i = 0 To 1
            bf(i) = New BoundDataField
            bf(i).DataFieldName = webgriddataset.Tables(0).Columns(i).Caption
            bf(i).Key = "Key" & i
            bf(i).Header.Text = webgriddataset.Tables(0).Columns(i).Caption.ToLower()

            WebHierarchicalDataGrid1.Columns.Add(bf(i))

        Next
        Dim band As Band
        band = New Band
        band.DataMember = "SqlDataSource2_view1"
        band.AutoGenerateColumns = False

        For i = 0 To 2
            bf(i) = New BoundDataField
            bf(i).DataFieldName = webgriddataset.Tables(1).Columns(i).Caption
            bf(i).Key = "Key" & (i + 2)
            bf(i).Header.Text = webgriddataset.Tables(1).Columns(i).Caption.ToLower()
            bf(i).Width = New WebControls.Unit(200, UnitType.Pixel)
            band.Columns.Add(bf(i))

        Next
        WebHierarchicalDataGrid1.Bands.Add(band)

        WebHierarchicalDataGrid1.Columns(0).Width = 100
        WebHierarchicalDataGrid1.Columns(1).Width = 500
        WebHierarchicalDataGrid1.Columns(2).Width = 200

      
        WebHierarchicalDataSource1.DataRelations.Add(datarel(0))
        WebHierarchicalDataSource1.DataViews.Add(dataview1(0))
        WebHierarchicalDataSource1.DataViews.Add(dataview1(1))

        WebHierarchicalDataGrid1.DataSource = WebHierarchicalDataSource1
        WebHierarchicalDataGrid1.DataBind()
       

    End Sub

End Class

 

 The output is:

Parents
No Data
Reply
  • 835
    posted

    This works great if the structure of your data stays the same on subsequent postbacks.  In my case, I'm dynamically loading the data with a different column sets on subsequent postbacks.  I've tried clearing the columns and bands but no joy.  I get a binding error on the second attempt.  I was able to get this to work if I turn off  EnableViewState and EnableDataViewState but that breaks my ability to update the data in the grid. 

     

    Any thoughts?

Children
No Data