开发者

Does DataGridView always depend on public properties for column values?

开发者 https://www.devze.com 2023-03-09 00:59 出处:网络
At runtime, I have a collection of rows (Row class). Each of them consist of column values, represented by instances of a ColumnValue class. The name of the columns are determined at runtime, and are

At runtime, I have a collection of rows (Row class). Each of them consist of column values, represented by instances of a ColumnValue class. The name of the columns are determined at runtime, and are in a separate columns descriptor collection (Column class开发者_如何学编程).

I want to create a DataGridView that displays all Row instances. Of course, the DataGridView's columns shall be exactly those specified by the Column instances in the containing collection.

But since DataGridView's columns can fetch their values from a list item's public properties only, and I cannot easily define such a property at runtime, I cannot use DataGridView to display the tabular data.

Correct?

Consider these VB classes for example:

' Classes for table structure representation
Public Class TColumn ' describes my columns
  Public Name As String
  Public Index As Integer

  Public Sub New(ByVal AName As String)
    Name = AName
  End Sub

End Class

Public Class TTable ' describes a table
  Public Name As String
  Public Columns As Collection

  Public Sub New(ByVal AName As String)
    Name = AName
    Columns = New Collection
  End Sub
End Class

' Classes for table data 

Public Class TRow ' Container for one row's column values
  Public ColumnValues As Collection
  Public Sub New()
    MyBase.New()
    ColumnValues = New Collection()
  End Sub
End Class

Public Class TTableData ' Container for a table's rows
  Public Table As TTable
  Public Rows As Collection
  Public Sub New(ByVal ATable As TTable)
    MyBase.New()
    Table = ATable
    Rows = New Collection()
  End Sub
End Class

And this main form code:

Public Class Form1

  Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
    Dim T As TTable
    Dim TD As TTableData
    Dim CV As Collection
    T = New TTable("Sample")
    T.Columns.Add(New TColumn("Column1"))
    T.Columns.Add(New TColumn("Column2"))

    TD = New TTableData(T)
    CV = New Collection
    CV.Add("Row1Col1")
    CV.Add("Row1Col2")
    TD.Rows.Add(CV)

    CV = New Collection
    CV.Add("Row2Col1")
    CV.Add("Row2Col2")
    TD.Rows.Add(CV)

    ' Question: How can I create a DataGridView that displays this:
    ' 
    '     Column1   Column2
    '     Row1Col1  Row1Col2
    '     Row2Col1  Row2Col1
    ' 
    ' WHILE the Columns collection contents are dynamic, i.e. determined at runtime, not at compile- or design-time?
  End Sub
End Class

I wonder what DataGridViewColumn.DataPropertyName value I would specify for example when defining (at runtime) Column1's DataGridView column???

Generally asking: Is it true that DataGridView always wants to fetch the data from object instances with public properties, and those define the column structure and especially values? I cannot display stuff that does not have public properties at all?


You should override the CellFormatting event of DataGridView and set e.Value to table(e.RowIndex)(e.ColumnIndex).

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号