如何在绑定到CustomObject的DataGridView中控制列类型? [英] How to control Column Type in DataGridView that is bound to a CustomObject?

查看:46
本文介绍了如何在绑定到CustomObject的DataGridView中控制列类型?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在C#WinForms应用程序中有一个DataGridView,它在运行时是DataBound(通过Form_Load)到自定义对象。

在DataGridView的设计视图中,我没有设置任何列。

加载窗体时,将根据DataBound到的Custom对象中的数据自动创建列。

我的问题是如何控制自动创建的列。

例如,如果我希望这些列之一是DataGridViewLinkColumn而不是自动创建的DataGridViewTextBoxColumn?

I have a DataGridView in a C# WinForms app that is DataBound at runtime (through Form_Load) to a custom Object.
In the design view of the DataGridView I have no columns set up.
When the Form loads the the columns are automatically created based on the data in the Custom object that it is DataBound to.
My question is how can I control the the Columns that are automatically created.
For example if I want one of the columns to be a DataGridViewLinkColumn instead of the DataGridViewTextBoxColumn that is automatically created?

推荐答案

默认列基于数据类型。 我没有检查,但是对于链接,您可以尝试将数据公开为 Uri ,但这可能是有希望的。确实,如果需要特定类型的列,请通过代码添加列并设置 DataGridView.AutoGenerateColumns false

The default columns are based on the data-type. I haven't checked, but for a link you could try exposing the data as Uri, but that might be hopeful. Really, if you want a specific type of column - add the columns through code and set DataGridView.AutoGenerateColumns to false.

正如安德鲁所暗示的;通常情况下,会使用 like 反射来生成列,并且您会为每个(可浏览+公共+可读)属性获得一列。如果需要,可以在此基础上进行一层抽象,但这对添加超链接列无济于事。

As Andrew implies; normally something like reflection is used to generate the columns, and you'll get a column for every (browsable + public + readable) property. There is a layer of abstraction on top of this if you need, but this won't help with adding a hyperlink column.

这篇关于如何在绑定到CustomObject的DataGridView中控制列类型?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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