如何编程转换列类型在DataGridView的C#?类型、DataGridView

2023-09-06 23:14:38 作者:残阳暮雪

我还动态创建的窗体DataGridView控件,并指定作为数据源:

I have dynamically created DataGridView control on form, with DataSource specified as:

    ((DataGridView)createdControl).DataSource = (IList)(p.GetValue(editedClient, null));

在这里的IList被定义为通用集合下面的类:

where IList is defined as generic collection for following class:

     public class CDocumentOperation

   {
        [DisplayName(@"Time")] 
        public DateTime TimePosted { get; set; }
        [DisplayName(@"User")]
        public CUser User { get; set; }
        [DisplayName(@"Action")]
        public string Action { get; set; }
    }

电网成功地与数据填充,但唯一的问题是所有的列 fields.What我需要的是手动转换列创建为文本 结合到用户现场,有按钮或链接(转换列类型为 DataGridViewButtonColumn )。

grid is populated successfully with data, but the only problem that all columns are created as Text fields.What I need is to manually convert column which binds to User field, to have Buttons or Links (convert column type to DataGridViewButtonColumn).

我能做到这一点,而无需修改网格建立后电网自动填充,无需人工 列建立适当的类型和数据复制?

Can I do this, without modifying grid auto-fill on grid post creation, without manual column creation of appropriate type and data copying ?

推荐答案

简短的答案是,这离不开手动创建的列(并设置 DataPropertyName 属性做)之前绑定。没有属性,你可以用它来装饰你的数据源,在的DataGridView 只会生成一个 DataGridViewTextBoxColumn 为每个数据类型(除了布尔它将解析为一个复选框列)。此行为是内部的,不可改变的。

The short answer is that this cannot be done without manually creating the columns (and setting the DataPropertyName property) before binding. There is no attribute you can use to decorate your data source, the DataGridView will simply generate a DataGridViewTextBoxColumn for every data type (except Boolean which it will resolve to a checkbox column). This behaviour is internal and unchangeable.

您最好的办法是禁用的AutoGenerateColumns 在网格上,写你自己的方法动态生成相应的列类型,也许是基于你自己的自定义属性,如(从你上面的例子):

Your best bet is to disable AutoGenerateColumns on the grid and write your own method that dynamically generates appropriate column types, perhaps based on your own custom attribute, such as (from your example above):

[DisplayName(@"Time"), ColumnType(typeof(DataGridViewButtonColumn))] 
public DateTime TimePosted { get; set; }

属性类是容易写(只延长属性,添加一个键入字段和一个合适的构造函数) 。在这种(直接绑定之前)将生成列的方法,你可以使用反射来抓取属性和检查自定义属性的presence。 ( BindingSource.GetItemProperties()是获得有关集合中的对象的属性信息非常有用。)

The attribute class is easy to write (just extend Attribute, add a Type field and an appropriate constructor). In the method that will generate the columns (immediately before binding), you can use reflection to crawl for properties and check for the presence of the custom attribute. (BindingSource.GetItemProperties() is very useful for obtaining information about the properties on objects in a collection.)

这是不是最优雅的解决方案(而再往一些中间层次的概念),但它是解决这个限制,在该的DataGridView 控制。

This is not the most elegant solution (and it delves into some intermediate-level concepts), but it's the only way to get around this limitation with auto-generated columns in the DataGridView control.