动态创建数据表、列、行

 

private   void  DemonstrateRowBeginEdit()
{
    DataTable table 
= new DataTable("table1");
    DataColumn column 
= new 
        DataColumn(
"col1",Type.GetType("System.Int32"));
    table.RowChanged
+=new 
        DataRowChangeEventHandler(Row_Changed);
    table.Columns.Add(column);

    
// Add a UniqueConstraint to the table.
    table.Constraints.Add(new UniqueConstraint(column));

    
// Add five rows.
    DataRow newRow;
   
    
for(int i = 0;i<5; i++)
    
{
        
// RowChanged event will occur for every addition.
        newRow= table.NewRow();
        newRow[
0]= i;
        table.Rows.Add(newRow);
    }

    
// AcceptChanges.
    table.AcceptChanges();

    
// Invoke BeginEdit on each.
    Console.WriteLine(
        
"  Begin Edit and print original and proposed values  ");
    
foreach(DataRow row in table.Rows)
    
{
   
        row.BeginEdit();
        row[
0]=(int) row[0]+10;
        Console.Write(
" able Original  able" + 
            row[
0, DataRowVersion.Original]);
        Console.Write(
" able Proposed  able" + 
            row[
0,DataRowVersion.Proposed] + " ");
    }

    Console.WriteLine(
" ");
    
// Accept changes
    table.AcceptChanges();
    
// Change two rows to identical values after invoking BeginEdit.
    table.Rows[0].BeginEdit();
    table.Rows[
1].BeginEdit();
    table.Rows[
0][0]= 100;
    table.Rows[
1][0]=100;
    
try
    
{
        
/* Now invoke EndEdit. This will cause the UniqueConstraint
           to be enforced.
*/

        table.Rows[
0].EndEdit();
        table.Rows[
1].EndEdit();
    }

    
catch(Exception e)
    
{
        
// Process exception and return.
        Console.WriteLine("Exception of type {0} occurred."
            e.GetType());
    }

}

 
private   void  Row_Changed( object  sender, 
    System.Data.DataRowChangeEventArgs e)
{
    DataTable table 
= (DataTable)  sender;
    Console.WriteLine(
"RowChanged " + e.Action.ToString() 
        
+ " able" + e.Row.ItemArray[0]);
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值