Results 1 to 5 of 5

Thread: 2-dimensional DataGridView

  1. #1

    Thread Starter
    Hyperactive Member
    Join Date
    May 2007
    Posts
    300

    2-dimensional DataGridView

    I'm sure this can be done, but can it be done easily?

    For example:
    vb.net Code:
    1. Class ColumnData
    2.     Private _ColName as String
    3.     Private _Data as String
    4.    
    5.     Property ColName as String
    6.         Get
    7.             Return _ColName
    8.         End Get
    9.         Set(ByVal value as String)
    10.             _ColName = value
    11.         End Set
    12.     End Property
    13.    
    14.     Property Data as String
    15.         Get
    16.             Return _Data
    17.         End Get
    18.         Set(ByVal value as String)
    19.             _Data = value
    20.         End Set
    21.     End Property
    22. End Class
    23.  
    24. Claas RowData
    25.     Private _RowName as String
    26.    
    27.     Property RowName as String
    28.         Get
    29.             Return _RowName
    30.         End Get
    31.         Set(ByVal value as String)
    32.             _RowName = value
    33.         End Set
    34.     End Property
    35.    
    36.     Public ColumnBL as new BindingList(Of ColumnData)
    37. End Class
    38.  
    39. 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.

  2. #2
    Member sh4de's Avatar
    Join Date
    Aug 2009
    Location
    Irvine, CA
    Posts
    48

    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...
    Senior Developer @
    Integrated Systems Solutions

  3. #3

    Thread Starter
    Hyperactive Member
    Join Date
    May 2007
    Posts
    300

    Re: 2-dimensional DataGridView

    Thanks for the reply.

    I'd rather stick with objects if I can.

    -Andy.

  4. #4
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    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.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  5. #5

    Thread Starter
    Hyperactive Member
    Join Date
    May 2007
    Posts
    300

    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:
    1. Private Sub CreatemyDataGridView()
    2.         For i = 0 To RowBL(0).ColumnBL.Count - 1
    3.             Dim NewCol As New DataGridViewTextBoxColumn
    4.             NewCol.Name = RowBL(0).ColumnBL(i).ColName
    5.             myDataGridView.Column.Add(NewCol)
    6.         Next
    7. End Sub
    8.  
    9. Private Sub myDataGridView_CellValueNeeded(ByVal sender As System.Object, _
    10.           ByVal e As System.Windows.Forms.DataGridViewCellValueEventArgs) _
    11.           Handles myDataGridView.CellValueNeeded
    12.                 e.Value = RowBL(e.RowIndex).ColumnBL(e.ColumnIndex).Data
    13.         End If
    14. End Sub
    15.  
    16. Private Sub myDataGridView_CellValuePushed(ByVal sender As System.Object, _
    17.           ByVal e As System.Windows.Forms.DataGridViewCellValueEventArgs) _
    18.           Handles myDataGridView.CellValuePushed
    19.                 RowBL(e.RowIndex).ColumnBL(e.ColumnIndex).Data = e.Value
    20.         End If
    21. 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
  •  



Click Here to Expand Forum to Full Width