如何以编程方式确定DataColumn的类型? [英] How Do I Programmatically Determine Type of DataColumn?
问题描述
你好!
感谢您阅读我的文章.
我有以下问题:
我的程序包括一个DataGridView,它显示了用户提供的select语句中的数据.我使用OdbcDataAdapter填充数据表,然后将其分配为DataGridView的数据源.
如果用户提供了返回二进制数据的SQL select语句,则当DataGridView尝试显示数据时,我将收到DataError异常.
如何确定数据表中的给定列是否包含二进制数据?我尝试使用DataColumn.DataType,但是无法识别二进制数据(类型为Byte [])(或者我不知道要比较什么).
如果DataGridView在二进制列中显示了二进制数据的字符串表示形式,或仅在列中显示了二进制数据"的消息,那将起作用.
谢谢,
Jess
Howdy!
Thanks for reading my post.
I have the following problem:
My program includes a DataGridView, which shows data from select statements supplied by the user. I use an OdbcDataAdapter to fill a DataTable, which is then assigned as the DataGridView''s DataSource.
If the user supplies an SQL select statement which returns binary data, I get a DataError exception when the DataGridView tries to display the data.
How do I determine if a given column in the datatable includes binary data? I''ve tried using DataColumn.DataType, but binary data (which is of type Byte[]) isn''t recognized (or I don''t know what to compare to).
If the DataGridView showed a string representation of the binary data, or just a message stating ''binary data'' in the column, that would work.
Thanks,
Jess
推荐答案
我能够使用
I was able to use
if (0 == String.Compare(dc.DataType.ToString().Trim(), "System.Byte[]"))
确定数据类型.我将它的表列索引存储在列表中,并且只是删除了该列.
谢谢,
杰西
to determine the datatype. I stored it''s table column index in a list<int>, and just deleted that column.
Thanks,
Jess
List<int> theseAreBinaryData = new List<int>();
foreach (DataColumn dc in dt.Columns)
{
if (0 == String.Compare(dc.DataType.ToString().Trim(), "System.Byte[]"))
{
theseAreBinaryData.Add(dt.Columns.IndexOf(dc.ColumnName));
}
}
if (theseAreBinaryData.Count > 0)
{
foreach (int i in theseAreBinaryData)
{
dt.Columns.RemoveAt(i);
}
}
dataGridView.DataSource = dt;
如果对进行 ToString()
DataType
,它应该告诉您使用了哪种数据类型,您可以从那里访问.
If you do aToString()
on theDataType
, it should tell you what data type is used and you can go from there.
这篇关于如何以编程方式确定DataColumn的类型?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!