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:
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?
Would you be able to send me the source? I'm not able to get this to work.
I"m not changing the width in initializerow. I am changing it in a sub called InitializeGrid that is called on page load (that's also included in the example). Doesn't work.
Tami
I too tried the Same in changing Initialize Row(). It wont Work. It is recommendable to write your own function and trying to change or set width
In vb we have with... end with statement. Inside that I changed all coluumn width. Try a function and set the Dataset into your Function and modify.
I wanted to have more control over the formatting of my columns before loading, so I moved my webhierarchicaldatasource to the code behind. I used the VB example as a model, but mine is C#. The grid does display with both the parent and details; however, it seems to ignore everything in my FOR loop where I am defining the datafields. You can see in my attached example that on the parent, I set the header text to "Test" and I also changed a couple of column widths just before binding. Neither works as expected. Any ideas?
Thanks!