DataColumn 类 (转)

From :  http://blog.csdn.net/lai123wei/article/details/3503983

 

DataColumn 是用于创建 DataTable 的架构的基本构造块。通过向 DataColumnCollection 中添加一个或多个 DataColumn 对象来生成这个架构。

每个 DataColumn 都有 DataType 属性,该属性确定 DataColumn 所包含的数据的种类。例如,可以将数据类型限制为整数、字符串或小数。由于 DataTable 所包含的数据通常合并回其原始数据源,因此必须使其数据类型与数据源中的数据类型匹配。有关更多信息,请参见 将数据提供程序数据类型映射到 .NET Framework 数据类型。

AllowDBNull、Unique 和 ReadOnly 等属性对数据的输入和更新施加限制,从而有助于确保数据完整性。还可以使用 AutoIncrement、AutoIncrementSeed 和 AutoIncrementStep 属性来控制数据自动生成。有关 AutoIncrement 列的更多信息,请参见 创建 AutoIncrement 列。有关更多信息,请参见 为表定义主键。

还可以通过创建一个 UniqueConstraint 并将其添加到 DataColumn 所属的 DataTable 的 ConstraintCollection 中,来确保 DataColumn 中的值是唯一的。有关更多信息,请参见 将约束添加到表。

若要创建 DataColumn 对象之间的关系,请创建 DataRelation 对象并将其添加到 DataSet 的 DataRelationCollection。

可以使用 DataColumn 对象的 Expression 属性来计算列中的值或创建聚合列。

 

用DataColumn 对象创建 DataTable

ExpandedBlockStart.gif View Code
private  void MakeTable()

     //  Create a DataTable. 
    DataTable table =  new DataTable( " Product ");

     //  Create a DataColumn and set various properties. 
    DataColumn column =  new DataColumn(); 
    column.DataType = System.Type.GetType( " System.Decimal "); 
    column.AllowDBNull =  false
    column.Caption =  " Price "
    column.ColumnName =  " Price "
    column.DefaultValue =  25

     //  Add the column to the table. 
    table.Columns.Add(column); 

     //  Add 10 rows and set values. 
    DataRow row; 
     for( int i =  0; i <  10; i++)
    { 
        row = table.NewRow(); 
        row[ " Price "] = i +  1

         //  Be sure to add the new row to the 
        
//  DataRowCollection. 
        table.Rows.Add(row); 
    } 
   // 添加一个DataGridView控件this.dataGridView1.DataSource = table; 这样可以看到所创建的表
}

 

转载于:https://www.cnblogs.com/nliao/archive/2012/11/18/2776144.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值