当我将DataTable连接到DataGridView时如何控制生成的列类型 [英] How do I control the column types generated when I connect a DataTable to a DataGridView

查看:83
本文介绍了当我将DataTable连接到DataGridView时如何控制生成的列类型的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个MyDataType对象作为数据表的一部分返回.我有一个MyDataTypeColumn类型,它将同时显示MyDataType中的字符串和图像值(如资源管理器详细信息视图中的名称列).

I have a MyDataType object being returned as part of a datatable.  I have a MyDataTypeColumn type that will display both a string and image value from MyDataType (like the name column in explorer details view).

 

当我将DataTable放在其中时我的DataGridView的DataSource创建一个DataGridViewTextBox列,并使用MyDataType.ToString()的结果填充它,这是不可接受的.  

When I put the DataTable in as my DataGridView''s DataSource it creates a DataGridViewTextBox column and populates it with the result of MyDataType.ToString() which isn''t acceptable.  

 

如何将自动生成的列强制为MyDataTypeColumn?还是DGV使用预定义的列,而不是从其DataSource加载时简单地在现有列的右边插入新列?

How can I either force the autogenerated column to be a MyDataTypeColumn; or the DGV to use predefined columns instead of simply inserting new ones to the right of the existing ones when loading from its DataSource?

MyDataTable.Columns.Add("Symbol", typeof(MyDataType)); 

//finish creating columns and loading data.  This is all manual since my data isn''t coming out of a database.

MyDataGridView.DataSource = MyDataTable;

推荐答案


您可以将DGV上的AutoGenerateColumns道具设置为false.然后,您需要手动创建列.只需确保指定的DataPropertyName与数据集中的DataColumn名称匹配即可.


 You could set the AutoGenerateColumns prop on the DGV to false. Then You need to create the columns manually. Just make sure the DataPropertyName you specify matches the DataColumn name in the dataset.

此方法还允许您在同一方法调用中指定标题文本与列名称分开.

This method also allows you specify the header text to be separate from the column name in the same method call.


这篇关于当我将DataTable连接到DataGridView时如何控制生成的列类型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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