將現有表中的數據行添加到新表 (Adding a datarow from an existing table to a new table)


問題描述

將現有表中的數據行添加到新表 (Adding a datarow from an existing table to a new table)

Hi i want to add a datarow that im getting back from a data table to a new datatable 

this is the code im using:

foreach (DataRow dr1 in dt.Rows)
{
  string AptType = dr1["AppointmentType"].ToString();
  if (AptType == "FreeTime")
  {
    dt2.ImportRow(dr1);
  }
}
RadGrid2.DataSource = dt2;
reader.Close();
conn.Close();

the problem is that when i then go to run the page with the table on it im getting a datakey error and that one of the columns isnt being recognised

thanks in advance 

‑‑‑‑‑

參考解法

方法 1:

Do the two data tables have the same schema?  Those errors might be thrown if they do not match columns, datatypes, or keys.

方法 2:

You should use Typed TableDataAdapters, I would make your life so much easier...

This is very easy to do and to understand.

Follow this tutorial Strongly Typed TableDataAdapters and DataTables

Once you grasp the concept, you should do something like this:

MyTypedTableAdapter tableAdapter = new MyTypedTableAdapter();
    MyTypedDataTable dt = tableAdapter.GetData();
    foreach (MyTypedDataRow row in dt.Rows)
    {
        string AptType = row.AppointmentType;
        if (AptType == "FreeTime")
        {
            dt2.ImportRow(row);
        }
    }
    RadGrid2.DataSource = dt2;

(by kevinwMatthew Jonesmaxbeaudoin)

參考文件

  1. Adding a datarow from an existing table to a new table (CC BY‑SA 3.0/4.0)

#asp.net-3.5 #.net #datarow #ado.net #C#






相關問題

將現有表中的數據行添加到新表 (Adding a datarow from an existing table to a new table)

ASP.NET - 加載具有重 OO 結構的網格數據 (ASP.NET - loading grid data with heavy OO structure)

使用動態數據時,為什麼我的視圖狀態在回發之間增長? (Why is my viewstate growing between postbacks when using dynamic data?)

瀏覽器緩存問題 (problem with browser cache)

如何在創建新線程的同時停止運行線程並恢復同一個線程? (how to stop running thread and resume the same thread beside create new thread?)

如何在sql server中創建一個計劃進程 (how to create a scheduled process in sql server)

向 SharePoint 2007 提供 ASP.Net 3.5 功能的可支持選項有哪些? (What are the supportable options for delivering ASP.Net 3.5 capability to SharePoint 2007?)

具有“插件”能力的 ASP.NET 內網應用程序 (ASP.NET intranet application with "plug-in" ability)

用於 ASP.NET Web 應用程序的 DevExpress 與 Telerik 網格 (DevExpress vs. Telerik grids for ASP.NET web applications)

如何在本地主機和服務器上獲取 URL 路徑? (how to get URL path on local host and on server?)

將 HttpModule 添加到 web.config 時未加載 ASP.NET CSS 文件 (ASP.NET CSS file not loaded when adding HttpModule to web.config)

如何在 Silverlight 2.0 中獲取當前用戶名 (how can i get current user name in Silverlight 2.0)







留言討論