2010-07-02 86 views
2

我有一個17列和一堆數據的數據表。來自另一個數據表的列的數據表子集

我只有6列的數據表和這6列的數據。

所以我需要原始數據表的一個子集。

如何循環顯示17列的原始數據表,並最終生成一個只包含6列的數據表,以及這6列的相應數據?

+0

你是如何開始填充數據表?如果你從數據庫中填充數據庫,當你通過SQL SELECT命令請求時,它應該是非常容易的。 – 2010-07-02 17:50:41

回答

2

不知道更多有關如何通用的,這需要之前創建

object[] row = new object[]{// Fill your rows manually}; 

是它真的只是......

foreach (DataRow dr in dt.Rows) 
{ 
    newDt.Rows.Add(dr["col1"],dr["col5"],etc); 
} 
+0

我相信這裏有一個錯字 - 你想要dt.Rows [「col5」],而不是博士,不是嗎?或者,或者,你可以有博士[「col1」],博士[「col5」] ... – Melanie 2013-06-05 16:08:21

+0

@Melanie是啊,我認爲後者是正確的,我在寫這個時想的是什麼(雖然是三年前,我真的不記得回答這個問題了)......編輯我的答案以反映你的改正 – heisenberg 2013-06-05 17:59:58

0

數據類型和列呢?這些是一樣的嗎?如果是的話,你可以填充它創建

DataTable dt = new DataTable(); 
dt.Columns.Add("Title",typeof(string etc..));..... 

最後

dt.Rows.Add(row); 
0

就我個人而言,我會避免創建DataTable的另一個實例。

這取決於你的情況,當然,但如果這純粹是爲了可用性,而不是爲了安全性(也就是說你不想在傳輸它之前刪除敏感數據的列),那麼我會創建一個包裝對象封裝您想要公開的列。

使用包裝的好處是如果你正在做任何更新,那麼你可以直接更新源表而不是副本。當然,這是否真的很重要取決於你的情況。

一個簡單的例子具有有限功能:

public class MyFormOrPage 
{ 
    void UsageExample() 
    { 
     DataTable allDataTable = new DataTable(); 
     // populate the data table with whatever logic ... 

     // wrap the data table to expose only the Name, Address, and PhoneNumber columns 
     var limitedDataTable = new DataTableWrapper(allDataTable, "Name", "Address", "PhoneNumber"); 

     // iterate over the rows 
     foreach (var limitedDataRow in limitedDataTable) 
     { 
      // iterate over the columns 
      for (int i = 0; i < limitedDataTable.ColumnCount; i++) 
      { 
       object value = limitedDataRow[i]; 
       // do something with the value ... 
      } 
     } 

     // bind the wrapper to a control 
     MyGridControl.DataSource = limitedDataTable; 
    } 
} 

public class DataTableWrapper : IEnumerable<DataRowWrapper> 
{ 
    private DataTable _Table; 

    private string[] _ColumnNames; 

    public DataTableWrapper(DataTable table, params string[] columnNames) 
    { 
     this._Table = table; 

     this._ColumnNames = columnNames; 
    } 

    public int ColumnCount 
    { 
     get { return this._ColumnNames.Length; } 
    } 

    public IEnumerator<DataRowWrapper> GetEnumerator() 
    { 
     foreach (DataRow row in this._Table.Rows) 
     { 
      yield return new DataRowWrapper(row, this._ColumnNames); 
     } 
    } 

    #region IEnumerable Members 

    System.Collections.IEnumerator System.Collections.IEnumerable.GetEnumerator() 
    { 
     return this.GetEnumerator(); 
    } 

    #endregion 

    // if you _really_ want to make a copy of the DataTable, you can use this method 
    public DataTable CopyToDataTable() 
    { 
     DataTable copyTable = new DataTable(); 
     for (int index = 0; index < this._ColumnNames.Length; index++) 
     { 
      DataColumn column = this._Table.Columns[index]; 
      copyTable.Columns.Add(column); 
     } 
     foreach (DataRow row in this._Table.Rows) 
     { 
      DataRow copyRow = copyTable.NewRow(); 
      for (int index = 0; index < this._ColumnNames.Length; index++) 
      { 
       copyRow[index] = row[this._ColumnNames[index]]; 
      } 
      copyTable.Rows.Add(copyRow); 
     } 
     return copyTable; 
    } 
} 

// let's make this a struct, since potentially very many of these will be instantiated 
public struct DataRowWrapper 
{ 
    private DataRow _Row; 

    private string[] _ColumnNames; 

    public DataRowWrapper(DataRow row, params string[] columnNames) 
    { 
     this._Row = row; 

     this._ColumnNames = columnNames; 
    } 

    // use this to retrieve column values from a row 
    public object this[int index] 
    { 
     get { return this._Row[this._ColumnNames[index]]; } 
     set { this._Row[this._ColumnNames[index]] = value; } 
    } 

    // just in case this is still needed... 
    public object this[string columnName] 
    { 
     get { return this._Row[columnName]; } 
     set { this._Row[columnName] = value; } 
    } 
} 
2
Private Function createSmallCopyofExistingTable(ByVal SourceTable As DataTable) As DataTable 
    Dim newTable As DataTable = New DataTable() 

    'Copy Only 6 columns from the datatable 
    Dim ColumnsToExport() As String = {"ID", "FirstName", "LastName", "DateOfBirth", "City", "State"} 

    newTable = SourceTable.DefaultView.ToTable("tempTableName", False, ColumnsToExport) 



    Return newTable 
End Function 
相關問題