DataColumn.DataType プロパティ
定義
重要
一部の情報は、リリース前に大きく変更される可能性があるプレリリースされた製品に関するものです。 Microsoft は、ここに記載されている情報について、明示または黙示を問わず、一切保証しません。
列に格納されているデータの型を取得または設定します。
public:
property Type ^ DataType { Type ^ get(); void set(Type ^ value); };
[System.ComponentModel.TypeConverter(typeof(System.Data.ColumnTypeConverter))]
public Type DataType { get; set; }
public Type DataType { get; set; }
[System.ComponentModel.TypeConverter(typeof(System.Data.ColumnTypeConverter))]
[System.Data.DataSysDescription("DataColumnDataTypeDescr")]
public Type DataType { get; set; }
[<System.ComponentModel.TypeConverter(typeof(System.Data.ColumnTypeConverter))>]
member this.DataType : Type with get, set
member this.DataType : Type with get, set
[<System.ComponentModel.TypeConverter(typeof(System.Data.ColumnTypeConverter))>]
[<System.Data.DataSysDescription("DataColumnDataTypeDescr")>]
member this.DataType : Type with get, set
Public Property DataType As Type
プロパティ値
列のデータ型を表す Type オブジェクト。
- 属性
例外
この列には既にデータが格納されています。
例
次の例では、複数のデータ型の列を DataTable1 つに追加し、テーブルに 1 つの行を追加します。
public DataTable MakeDataTable(){
DataTable myTable;
DataRow myNewRow;
// Create a new DataTable.
myTable = new DataTable("My Table");
// Create DataColumn objects of data types.
DataColumn colString = new DataColumn("StringCol");
colString.DataType = System.Type.GetType("System.String");
myTable.Columns.Add(colString);
DataColumn colInt32 = new DataColumn("Int32Col");
colInt32.DataType = System.Type.GetType("System.Int32");
myTable.Columns.Add(colInt32);
DataColumn colBoolean = new DataColumn("BooleanCol");
colBoolean.DataType = System.Type.GetType("System.Boolean");
myTable.Columns.Add(colBoolean);
DataColumn colTimeSpan = new DataColumn("TimeSpanCol");
colTimeSpan.DataType = System.Type.GetType("System.TimeSpan");
myTable.Columns.Add(colTimeSpan);
DataColumn colDateTime = new DataColumn("DateTimeCol");
colDateTime.DataType = System.Type.GetType("System.DateTime");
myTable.Columns.Add(colDateTime);
DataColumn colDecimal = new DataColumn("DecimalCol");
colDecimal.DataType = System.Type.GetType("System.Decimal");
myTable.Columns.Add(colDecimal);
DataColumn colByteArray = new DataColumn("ByteArrayCol");
colByteArray.DataType = System.Type.GetType("System.Byte[]");
myTable.Columns.Add(colByteArray);
// Populate one row with values.
myNewRow = myTable.NewRow();
myNewRow["StringCol"] = "Item Name";
myNewRow["Int32Col"] = 2147483647;
myNewRow["BooleanCol"] = true;
myNewRow["TimeSpanCol"] = new TimeSpan(10,22,10,15,100);
myNewRow["DateTimeCol"] = System.DateTime.Today;
myNewRow["DecimalCol"] = 64.0021;
myNewRow["ByteArrayCol"] = new Byte[] { 1, 5, 120 };
myTable.Rows.Add(myNewRow);
return myTable;
}
Public Function MakeDataTable() As DataTable
Dim myTable As DataTable
Dim myNewRow As DataRow
' Create a new DataTable.
myTable = New DataTable("My Table")
' Create DataColumn objects of data types.
Dim colString As New DataColumn("StringCol")
colString.DataType = System.Type.GetType("System.String")
myTable.Columns.Add(colString)
Dim colInt32 As New DataColumn("Int32Col")
colInt32.DataType = System.Type.GetType("System.Int32")
myTable.Columns.Add(colInt32)
Dim colBoolean As New DataColumn("BooleanCol")
colBoolean.DataType = System.Type.GetType("System.Boolean")
myTable.Columns.Add(colBoolean)
Dim colTimeSpan As New DataColumn("TimeSpanCol")
colTimeSpan.DataType = System.Type.GetType("System.TimeSpan")
myTable.Columns.Add(colTimeSpan)
Dim colDateTime As New DataColumn("DateTimeCol")
colDateTime.DataType = System.Type.GetType("System.DateTime")
myTable.Columns.Add(colDateTime)
Dim colDecimal As New DataColumn("DecimalCol")
colDecimal.DataType = System.Type.GetType("System.Decimal")
myTable.Columns.Add(colDecimal)
' Populate one row with values.
myNewRow = myTable.NewRow()
myNewRow("StringCol") = "Item Name"
myNewRow("Int32Col") = 2147483647
myNewRow("BooleanCol") = True
myNewRow("TimeSpanCol") = New TimeSpan(10,22,10,15,100)
myNewRow("DateTimeCol") = System.DateTime.Today
myNewRow("DecimalCol") = 64.0021
myNewRow("ByteArrayCol") = New [Byte]() {1, 5, 120}
myTable.Rows.Add(myNewRow)
MakeDataTable = myTable
End Function
注釈
値の DataType 設定は、データ ソース内のデータの正しい作成と更新を保証するために非常に重要です。
このプロパティはDataType、次の基本.NET Frameworkデータ型をサポートしています。
次の配列型と同様に、次の配列型も使用できます。
Byte[]
列がデータの格納を開始した後にこのプロパティを変更すると、例外が生成されます。
AutoIncrementプロパティを設定DataTypeする前にtrue
設定し、整数型を除く任意の値に型を設定しようとすると、例外が生成されます。
注意
データ型の列は、基本 .NET Frameworkデータ型Byte[]
とは異なり、参照データ型であるため、特定のケースでは特別な処理を必要とします。 データ型Byte[]
の列を 、またはキーとしてRowFilter Sort DataViewPrimaryKey使用する場合、列の値を変更するには、個別にインスタンス化されたByte[]
オブジェクトに列値をByte[]
割り当てる必要があります。 この割り当ては、並べ替え、フィルター処理、および主キー操作によって使用される内部インデックスの更新をトリガーするために必要です。 この例を次に示します。
byte[] columnValue = (byte[])myDataTable.Rows[0][0];
byte[] newValue = (byte[])columnValue.Clone();
newValue[1] = 2;
myDataTable.Rows[0][0] = newValue;
注意
基本.NET FrameworkByte[]
データ型以外のデータ型として列を定義することはできますが、このような列は、次の使用制限に従って、ユーザー定義型として扱われます。 (ユーザー定義型の詳細については、「 User-Defined型の作成と使用」を参照してください)
列が不変フィールドとしてPrimaryKey、または a としてSortDataView使用される場合は、変更できないフィールドとして扱う必要があります。列データは、テーブルに追加された後で変更しないでください。
これは ColumnMapping 、次に設定
MappingType.Element
することのみ可能です。列のデータ型を実装するクラスは、必要に応じて、またはIXmlSerializableインターフェイスをSerializableAttribute実装する 、でマークするISerializable必要があります。
変更追跡のサポートは限られています。 クラスの変更追跡メカニズムをDataTable利用するには、列のデータ型を実装するクラスがインターフェイスをIChangeTracking実装するか、行を呼び出SetModifiedすか、列値オブジェクトをDataRow個別にインスタンス化された列値オブジェクトに割り当てることによって、列の値が変更されたタイミングを通知する責任を引き継ぐ必要があります。