请问DataGridView的是依靠公共属性列的值? [英] Does DataGridView always depend on public properties for column values?

查看:142
本文介绍了请问DataGridView的是依靠公共属性列的值?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在运行时,我行(类)的集合。他们每个人都包含列中的值,再由 Col​​umnValue 类的实例psented $ P $。列的名称是在运行时决定的,并且是在一个单独的列描述符集合(类)。

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).

我想创建一个的DataGridView 显示所有实例。当然,的DataGridView 的列应是完全那些由指定的实例包含收藏。

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.

不过,由于的DataGridView 的列只能从一个列表项的公共属性获取它们的值,我的不容易的定义,例如属性在运行时,我不能使用的DataGridView 显示表格数据。

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.

正确

考虑到这些VB类,例如:

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

这主要形式code:

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

我不知道什么 DataGridViewColumn.DataPropertyName 值时的定义(在运行时)我会指定例如列1 的DataGridView 列???

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

一般会问:这是真的,的DataGridView 总是要来从对象实例数据与公共属性,而这些定义列的结构,尤其是价值观?我无法显示的东西,根本不具备公共属性?

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?

推荐答案

您应该重写的DataGridView CellFormatting 事件>并设置 e.Value 表(e.RowIndex)(e.ColumnIndex)

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

这篇关于请问DataGridView的是依靠公共属性列的值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆