.NET : 데이터 가능한 값을 변경하는 방법
-
06-07-2019 - |
문제
ado.net에서 나는 사용하고 있습니다 getschechatable 결과 세트의 스키마 테이블을 반환합니다.
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 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.
나는 심지어 그리드 뷰를 보았다 databound 이벤트, 값이 렌더링 될 때 값을 변경할 수 있다고 생각합니다.
protected void gridSchema_RowDataBound(object sender, GridViewRowEventArgs e)
{
//todo: magic
//e.Row["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();
해결책
나는 여기서 완전히 꺼져 있을지 모르지만 열의 Readonly 속성을 False로 설정할 수 없습니까? 처럼:
schema.Columns["ProviderType"].ReadOnly = false;
그 후에 문자열 값을 정수 열에 넣으려고 할 때 열 유형 문제가 발생할 수 있습니다. 그러나 이것은 당신을 올바른 방향으로 만들어야합니다.
편집하다:
칼럼 유형 문제 해결 :
DataTable newTable = schema.Clone();
newTable.Columns["ProviderType"].DataType = typeof(string);
foreach (DataRow dr in schema.Rows)
{
DataRow newRow = newTable.NewRow();
// fill newRow with correct data and newly formatted providertype
newTable.Rows.Add(newRow);
}
다른 팁
또는 DataTable의 모든 필드가있는 객체를 만들거나 모든 데이터를 객체에 전달하고 수정하거나 주어진 정수에 achord를 반환하는 읽기 전용 필드를 작성할 수 있습니다.
GridView 및 기타는 객체 배열을 데이터 소스로 허용하므로 자동입니다.