创建 DataTable

        DataTable dt  =   new  DataTable();
        dt.Columns.Add(
" Name " );
        dt.Columns.Add(
" Value " );
        DataRow dr;
        dr 
=  dt.NewRow();
        dr[
" Name " =   " Ntext2 " ;
        dr[
" Value " =   " TesrNtext2 " ;
        dt.Rows.Add(dr);

 

 

private   void  MakeDataTableAndDisplay()
{
    
// Create new DataTable.
    DataTable table = new DataTable("table");

    
// Declare DataColumn and DataRow variables.
    DataColumn column;
    DataRow row;
 
    
// Create new DataColumn, set DataType, 
    
// ColumnName and add to DataTable.    
    column = new DataColumn();
    column.DataType 
= System.Type.GetType("System.Int32");
    column.ColumnName 
= "id";
    table.Columns.Add(column);
 
    
// Create second column.
    column = new DataColumn();
    column.DataType 
= Type.GetType("System.String");
    column.ColumnName 
= "item";
    table.Columns.Add(column);
 
    
// Create new DataRow objects and add to DataTable.    
    for(int i = 0; i < 10; i++)
    
{
        row 
= table.NewRow();
        row[
"id"= i;
        row[
"item"= "item " + i;
        table.Rows.Add(row);
    }

    
// Set to DataGrid.DataSource property to the table.
    dataGrid1.DataSource = table;
}


将 DataTable 保存到 DataSet 中所需的 C# 代码: void  DataTableToDataSet(DataTable dt)
{
   
// Duplicate the table and add it to a DataSet
   DataSet dsTmp = new DataSet();
   DataTable dtTmp 
= dt.Copy();
   dsTmp.Tables.Add(dtTmp);
  }


将DataView保存到DataSet中:
void  DataViewToDataSet(DataView dv)
{
// Clone the structure of the table behind the view
DataTable dtTemp = dv.Table.Clone();
   dtTemp.TableName 
= "Row";  

   
// Populate the table with rows in the view
   foreach(DataRowView drv in dv)
      dtTemp.ImportRow(drv.Row);

DataSet dsTemp 
= new DataSet(dv.Table.TableName);   

   
// Add the new table to a DataSet
   dsTemp.Tables.Add(dtTemp);
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值