.NET:如何更改数据表中的值
在 ADO.NET 中,我使用 GetSchemaTable 返回结果集的架构表。
DataTable schema = rdr.GetSchemaTable();
gridSchema.DataSource = schema;
gridSchema.DataBind();
不幸的是,“ProviderType”值显示为整数,而不是OleDbType 枚举值:
ProviderType Desired Display Value
============ =====================
129 Char
3 Integer
129 Char
129 Char
3 Integer
3 Integer
129 Char
135 DBTimeStamp
129 Char
129 Char
...
所有这些整数都是 OleDbType 枚举:
public enum OleDbType
{
Empty = 0,
SmallInt = 2,
Integer = 3,
Single = 4,
Double = 5,
Currency = 6,
Date = 7,
BSTR = 8,
IDispatch = 9,
Error = 10,
Boolean = 11,
Variant = 12,
IUnknown = 13,
Decimal = 14,
TinyInt = 16,
UnsignedTinyInt = 17,
UnsignedSmallInt = 18,
UnsignedInt = 19,
BigInt = 20,
UnsignedBigInt = 21,
Filetime = 64,
Guid = 72,
Binary = 128,
Char = 129,
WChar = 130,
Numeric = 131,
DBDate = 133,
DBTime = 134,
DBTimeStamp = 135,
PropVariant = 138,
VarNumeric = 139,
VarChar = 200,
LongVarChar = 201,
VarWChar = 202,
LongVarWChar = 203,
VarBinary = 204,
LongVarBinary = 205,
}
我想将数据类型显示为人类可读的内容,而不是整数。
我尝试循环遍历架构 DataTable 并修改 DataTable 内的值:
DataTable schema = rdr.GetSchemaTable();
//Change providerType column to be readable
foreach (DataRow row in schema.Rows)
{
OleDbType t = (OleDbType)row["ProviderType"];
row["ProviderType"] = t.ToString();
}
gridSchema.DataSource = schema;
gridSchema.DataBind();
但这会引发异常:
Column 'ProviderType' is read only.
我什至查看了 GridView 的 RowDataBound 事件,认为我可以在呈现时更改该值:
protected void gridSchema_RowDataBound(object sender, GridViewRowEventArgs e)
{
//todo: magic
//e.Row["ProviderType"]
}
但事实并非如此看起来您可以使用渲染值。
任何人都可以建议一种很好的方法来处理 ProviderType 列的值,以便当我将其显示给人类时它是人类可读的吗?
更新
我现在使用的解决方法是在末尾添加一个额外的列:
DataTable schema = rdr.GetSchemaTable();
schema.Columns.Add("OleDbDataType", typeof(String));
schema.Columns.Add("CLRDataType", typeof(String));
foreach (DataRow row in schema.Rows)
{
//Show the actual provider type
OleDbType t = (OleDbType)row["ProviderType"];
row["OleDbDataType"] = t.ToString();
//Show the corresponding CLR type while we're doing a hack
row["CLRDataType"] = row["DataType"].ToString();
}
gridSchema.DataSource = schema;
gridSchema.DataBind();
In ADO.NET i'm using GetSchemaTable to return the schema table for a results set.
DataTable schema = rdr.GetSchemaTable();
gridSchema.DataSource = schema;
gridSchema.DataBind();
Unfortunatly the "ProviderType" value is displaying as an integer, rather than the OleDbType enumeration value that it is:
ProviderType Desired Display Value
============ =====================
129 Char
3 Integer
129 Char
129 Char
3 Integer
3 Integer
129 Char
135 DBTimeStamp
129 Char
129 Char
...
All these integers are the the enumeration values for the OleDbType enumeration:
public enum OleDbType
{
Empty = 0,
SmallInt = 2,
Integer = 3,
Single = 4,
Double = 5,
Currency = 6,
Date = 7,
BSTR = 8,
IDispatch = 9,
Error = 10,
Boolean = 11,
Variant = 12,
IUnknown = 13,
Decimal = 14,
TinyInt = 16,
UnsignedTinyInt = 17,
UnsignedSmallInt = 18,
UnsignedInt = 19,
BigInt = 20,
UnsignedBigInt = 21,
Filetime = 64,
Guid = 72,
Binary = 128,
Char = 129,
WChar = 130,
Numeric = 131,
DBDate = 133,
DBTime = 134,
DBTimeStamp = 135,
PropVariant = 138,
VarNumeric = 139,
VarChar = 200,
LongVarChar = 201,
VarWChar = 202,
LongVarWChar = 203,
VarBinary = 204,
LongVarBinary = 205,
}
i want to display the data type as something human readable, rather than an integer.
i've tried looping through the schema DataTable and modify the values inside the DataTable:
DataTable schema = rdr.GetSchemaTable();
//Change providerType column to be readable
foreach (DataRow row in schema.Rows)
{
OleDbType t = (OleDbType)row["ProviderType"];
row["ProviderType"] = t.ToString();
}
gridSchema.DataSource = schema;
gridSchema.DataBind();
But that throws an exception:
Column 'ProviderType' is read only.
i even looked at the GridView's RowDataBound event, thinking i could change the value as it is rendered:
protected void gridSchema_RowDataBound(object sender, GridViewRowEventArgs e)
{
//todo: magic
//e.Row["ProviderType"]
}
But it doesn't look like you can play with rendered values.
Can anyone suggest a nice way to much with the value of the ProviderType column so that it is human readable when i display it to humans?
Update
The workaround i'm using right now is tack an extra column on the end:
DataTable schema = rdr.GetSchemaTable();
schema.Columns.Add("OleDbDataType", typeof(String));
schema.Columns.Add("CLRDataType", typeof(String));
foreach (DataRow row in schema.Rows)
{
//Show the actual provider type
OleDbType t = (OleDbType)row["ProviderType"];
row["OleDbDataType"] = t.ToString();
//Show the corresponding CLR type while we're doing a hack
row["CLRDataType"] = row["DataType"].ToString();
}
gridSchema.DataSource = schema;
gridSchema.DataBind();
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我可能完全不在意,但是你不能将列的 ReadOnly 属性设置为 false 吗? 例如:
之后,当您尝试将字符串值放入整数列时,您可能会遇到列类型问题。 但这应该会让你走向正确的方向。
编辑:
解决列类型问题:
I might be completely off here, but can't you just set the ReadOnly property of the column to false? Like:
Afterwards you might get a columntype problem as you're trying to put a string value into a integer column. But this should get you into the right direction.
edit:
Solving the columntype issue:
或者,您可以创建一个包含数据表中所有字段的对象,将所有数据传递到您的对象中并修改它,或者创建一个只读字段,根据给定的整数返回字符串。
GridView 和其他控件也接受对象数组作为数据源,因此它是自动的。
Or you could create an object with all the fields from the datatable, pass all the data into your object and modify it or create a read only field that returns the string acording to the integer given.
GridView and other also accept object arrays as datasources so it is automatic.