|
-
Aug 6th, 2009, 06:29 PM
#1
Thread Starter
Hyperactive Member
2-dimensional DataGridView
I'm sure this can be done, but can it be done easily?
For example:
vb.net Code:
Class ColumnData
Private _ColName as String
Private _Data as String
Property ColName as String
Get
Return _ColName
End Get
Set(ByVal value as String)
_ColName = value
End Set
End Property
Property Data as String
Get
Return _Data
End Get
Set(ByVal value as String)
_Data = value
End Set
End Property
End Class
Claas RowData
Private _RowName as String
Property RowName as String
Get
Return _RowName
End Get
Set(ByVal value as String)
_RowName = value
End Set
End Property
Public ColumnBL as new BindingList(Of ColumnData)
End Class
Public RowBL = new BindingList(Of RowData)
How would I build a data-bound DataGridView with the column names coming from the ColumnData, Row Names from the RowData and the values from the RowBL(row).ColumnBL(column).Data
I know how to create DataSources bound to objects, but I have only done it with fixed columns. How do I add columns on the fly and data-bind each cell properly?
Thanks for your help with this!
-Andy.
-
Aug 6th, 2009, 07:42 PM
#2
Member
Re: 2-dimensional DataGridView
Why not just create a data set and modify the data tables and rows..and then bind your grid to the dataset?
The Grid will automatically create the rows/columns...
-
Aug 6th, 2009, 10:03 PM
#3
Thread Starter
Hyperactive Member
Re: 2-dimensional DataGridView
Thanks for the reply.
I'd rather stick with objects if I can.
-Andy.
-
Aug 6th, 2009, 10:20 PM
#4
Re: 2-dimensional DataGridView
Data-binding works by utilising PropertyDescriptors. Generally speaking PropertyDescriptors are generated automatically for each property in the type of the items in the list, e.g. if you bind a List(Of Person) then a PropertyDescriptor is generated for each property of the Person class and the columns in your grid will match those.
If you want your type to behave differently then you need to generate the PropertyDescriptors yourself, which means that you have to define a custom TypeDescriptor for your type. This is how columns can be generated from a bound DataTable when the columns don't represent actual properties.
You've got some reading to do on how data-binding works and on the TypeDescriptor and PropertyDescriptor classes.
-
Aug 7th, 2009, 03:18 PM
#5
Thread Starter
Hyperactive Member
Re: 2-dimensional DataGridView
Thanks, jmcilhinney;
You're right, as always. I read up on it but that's just not easy enough for a lazy guy like me! 
So here's what I did:
vb.net Code:
Private Sub CreatemyDataGridView()
For i = 0 To RowBL(0).ColumnBL.Count - 1
Dim NewCol As New DataGridViewTextBoxColumn
NewCol.Name = RowBL(0).ColumnBL(i).ColName
myDataGridView.Column.Add(NewCol)
Next
End Sub
Private Sub myDataGridView_CellValueNeeded(ByVal sender As System.Object, _
ByVal e As System.Windows.Forms.DataGridViewCellValueEventArgs) _
Handles myDataGridView.CellValueNeeded
e.Value = RowBL(e.RowIndex).ColumnBL(e.ColumnIndex).Data
End If
End Sub
Private Sub myDataGridView_CellValuePushed(ByVal sender As System.Object, _
ByVal e As System.Windows.Forms.DataGridViewCellValueEventArgs) _
Handles myDataGridView.CellValuePushed
RowBL(e.RowIndex).ColumnBL(e.ColumnIndex).Data = e.Value
End If
End Sub
Of course it's not particularly dynamic, but it gets the job done quite easily. This of course is just an example, it would need some range-checking, etc. to be complete.
Oh, for anyone following along, the DataGridView needs have VirtualMode set to "true"
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|