来自另一个数据表的列的可数据集子集

时间:2010-07-02 17:47:52

标签: c# .net asp.net

我有一个包含17列和一堆数据的数据表。

我只使用了6列的数据表和6列的数据。

所以我需要原始数据表的子集。

如何使用17列遍历原始数据表,最后得到一个只有6列我想要的数据表以及这6列的相应数据?

4 个答案:

答案 0 :(得分:2)

在不了解更多关于如何通用这一点的情况下,它真的只是......

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

答案 1 :(得分: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

答案 2 :(得分:0)

数据类型和列怎么样?这些是一样的吗?如果是,您可以创建

 object[] row = new object[]{// Fill your rows manually};
填写之前

创建

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

最后

dt.Rows.Add(row);

答案 3 :(得分: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; }
    }
}