DataGridView - DataTable - 显示属性 [英] DataGridView - DataTable - displaying properties

查看:216
本文介绍了DataGridView - DataTable - 显示属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

(.NET 2.0,System.Windows.FormsDataGridView和DataTable)

( .NET 2.0, System.Windows.FormsDataGridView and DataTable )

我有1个datagrid连接到1个datatable。

I have 1 datagrid connected to 1 datatable.

该datatable包含1列:包含我自己的类型MyObject的对象。

MyObject有一个 public 字符串属性MyProp。

That datatable contains 1 column: containing objects of my own type "MyObject".
MyObject has a public string property "MyProp".

我想在网格中显示1列:显示表中MyObjects属性MyProp的字符串值。

I want to display 1 column in the grid: showing the string values of the property MyProp of the MyObjects in the table.

但是我似乎无法让它工作。

But I can't seem to get it to work.

原理图:


  • myTable.Column1.Name:Obj

  • myDataSet包含1个表:myTable(填充几行)

  • myBindingSource.datasource = myDataSet myDataGridView中的myBindingSource.DataMember = myTable

  • myDataGridView.DataSource = myBindingSource

  • DataGridViewTextBoxColumn

  • myTable.Column1.Name: "Obj"
  • myDataSet contains 1 table: myTable (filled with a few rows)
  • myBindingSource.datasource = myDataSet
  • myBindingSource.DataMember = myTable
  • myDataGridView.DataSource = myBindingSource
  • in myDataGridView: 1 DataGridViewTextBoxColumn

试用1:

.DataPropertyName =Obj(默认生成)

Column.DataPropertyName="Obj" (generated by default)

显示(覆盖)MyObject.ToString()
的结果不是真的我想要的,这个函数用于记录。

Displays the result of (overridden) MyObject.ToString() Not really what I want, this function is in use for logging.


  • tryout 2:

GridViewColumn.DataPropertyName =Obj.MyProp

GridViewColumn.DataPropertyName="Obj.MyProp"

不显示任何内容(MyProp的获得从未被调用)

Doesn't display anything (MyProp's get is never called)


  • 试用3:
    使MyProp属性可绑定:

  • tryout 3: Made the MyProp property bindable:

[Bindable (BindableSupport.Yes)]
public string MyProp
{ ...

Samesame,与试用版没有不同2

Samesame, not different from tryout 2

简而言之:

GridViewColumn.DataPropertyName似乎不支持进一步向下钻取到datatable列中的对象。

GridViewColumn.DataPropertyName doesn't seem to support drilling further down into objects in a datatable's column.

有任何想法吗?

提前感谢

Jan

推荐答案

尝试将Obj.MyProp字符串值放入数据表排。将一个对象放在那里将需要将它转换成一个字符串表示(Obj.ToString()),它可以让你看到你的输出。

Try just putting the Obj.MyProp string value into the datatable row. Putting an object into there will require it to be converted into a string representation (Obj.ToString()) which is getting you the output you are seeing.

数据表与电子表格非常相似,不像Array(Of objects)。你必须这样对待它。

A datatable is much like a spreadsheet, not like an Array(Of objects). You have to treat it as such,.

这篇关于DataGridView - DataTable - 显示属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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