数据表添加到数据集

时间:2011-06-22 10:40:14

标签: c#

我需要将表设置为数据集

DataSet ds = EventDal.GetEvents();
DataSet dsReturn = new DataSet();
DataTable dtReturn = dsReturn.Tables.Add();
dtReturn.Columns.Add("id");
dtReturn.Columns.Add("description");
dtReturn.Columns.Add("status");

foreach (DataRow row in ds.Tables[0].Rows)
{
  if(Convert.ToInt32(row[1]) == status )
  {
    DataRow newrowdata = dtReturn.NewRow();
    dsReturn.Tables["dtReturn"].ImportRow((row));///i'm getting object ref not set to an instance of object..
    //DataRow drReturn = dtReturn.NewRow();
    //dsReturn.Tables["dtReturn"].Rows.Add(row);
  }
}
return dsReturn;

1 个答案:

答案 0 :(得分:1)

DataSet customerOrders = new DataSet("CustomerOrders");

DataTable ordersTable = customerOrders.Tables.Add("Orders");

DataColumn pkOrderID = 
    ordersTable.Columns.Add("OrderID", typeof(Int32));
ordersTable.Columns.Add("OrderQuantity", typeof(Int32));
ordersTable.Columns.Add("CompanyName", typeof(string));

ordersTable.PrimaryKey = new DataColumn[] { pkOrderID };