Can anyone write me a code where I can create a hierarchical grid programmatically by adding each row separately? I know data binding is a nice cute feature but our software design doesn't permit us using the binding directly. So I need to add data one by one in the grid, that too in hierarchical fashion.
The concept of bands is kinda confusing.
Can anyone please just write a small like of code where I create a grid, add couple of columns and a couple of rows to it. And for each row I need to two columns and 1 row each.
Thanks. I just need an idea of the thing.
Good morning Edward,
I overlooked and missed the link you provided. Thank you for the nice working code you posted! I added a few more lines just to help someone new like I am.
Reference to Edward McCarroll's original post
Public Function CreateDataset() As DataSet Dim lResult As New DataSet("myDataset") Dim getLevel1QStr As String = "SELECT InvoiceNo, Description FROM Level1" Dim getLevel2QStr As String = "SELECT InvoiceNo, PartNo, Description FROM Level2" Dim getLevel3QStr As String = "SELECT PartNo, SerialNo, Description FROM Level3"
Dim level1Table As New DataTable("Level1") lResult.Tables.Add(level1Table) lResult.Tables("Level1").Load(GetDataReader(getLevel1QStr))
Dim level2Table As New DataTable("Level2") lResult.Tables.Add(level2Table) lResult.Tables("Level2").Load(GetDataReader(getLevel2QStr))
lResult.Relations.Add(lResult.Tables("Level1").Columns("InvoiceNo"), lResult.Tables("Level2").Columns("InvoiceNo"))
Dim level3Table As New DataTable("Level3") lResult.Tables.Add(level3Table) lResult.Tables("Level3").Load(GetDataReader(getLevel3QStr))
lResult.Relations.Add(lResult.Tables("Level2").Columns("PartNo"), lResult.Tables("Level3").Columns("PartNo"))
Return lResultEnd Function
Public Function GetDataReader(ByVal queryString As String) As SqlDataReader
'AppConnectionString = connection string Dim myConnection As New SqlConnection(AppConnectionString) Dim myCommand As New SqlCommand(queryString, myConnection) 'Set the command myCommand.CommandType = CommandType.Text 'Execute the command myConnection.Open() Dim result As SqlDataReader = myCommand.ExecuteReader(CommandBehavior.CloseConnection) 'Return SqlDataReader Return result
End Function
Private Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles MyBase.Load
If Not IsPostBack = True Then Me.UG1.DataSource = CreateDataset() 'Create and return a dataset Me.UG1.DataBind() 'Bind to Grid Me.UG1.DisplayLayout.ActiveRow = Me.UG1.Rows(0) 'Set pointer to first row End If
End Sub 'Page_Load
Thanks,Thai Nguyen
You could save youself (and us) a bit if time if you read the entire thread before you post a question that has already been answered.
*grin*
See my post, above, that provides a link to an example. If that turns out not to be what you're looking for, then explain why it doesn't work for you.
Good luck.
Hello everyone,
I got around creating the Hierarchical Grid in 2 levels (level1 and level2), but how do you bind to a grid with N-Level (Level3....and so on). Please advise!
Thanks
neebz said:None of the events are working. Is it because I am creating the grid without binding any data to it?
I would expect so. Most of the events I use are part of the basic binding sequence.
What events do you expect to fire without any binding?
(And, out of curiosity, what data do you expect you grid to display without any binding? Are you creating rows at design time?)
I am struggling to fire Any event from my grid. None of the events are working. Is it because I am creating the grid without binding any data to it? What could be the possible reasons?