假设现在有一个DataTable包含10个列/字段,10万行.如果快速得到一个新的DataTable只包含其中的指定列/字段呢?

解决方案 »

  1.   

    在Fill dataset的时候调用的Stored Procedure中过滤好一点吧。
      

  2.   


    dt.Columns.Remove( "列名称 "); 
    dt.Columns.RemoveAt(列索引);
      

  3.   

    例如修改红色的部分进行过滤SqlDataAdapter ad = new SqlDataAdapter("SELECT * FROm Categories",myConnection);DataSet ds = new DataSet();ad.Fill(ds,"Categories");DataGrid1.DataSource = ds;DataGrid1.DataBind();
      

  4.   

    这样没有得到新的dt啊,原来的dt不能变
      

  5.   

    如果只是要显示,那么可以用DataView得到原DataTable的视图。
      

  6.   

    再创建一个dt2,如:
    DataTable dt2 = dt;
    对dt2进行操作!!
      

  7.   

    使用DataTable.Copy()生成新的DataTable,然后进行处理另外一个就是我说的,创建该表的视图:private void BindDataGrid()
    {
        DataTable table = new DataTable();    // Insert code to populate a DataTable with data.    // Bind grid to DataTable.
        dataGrid1.DataSource = table;
    }private void ChangeRowFilter()
    {
        DataTable gridTable = (DataTable) dataGrid1.DataSource;    // Set the RowFilter to display a company names that 
        // begin with A through I..
        gridTable.DefaultView.RowFilter = "CompanyName < 'I'";
    }
      

  8.   

    1  string sql="select 字段1,字段2 from 表名 ";
    根据要显示的哪些字段就添加上去这是其中的第一种
    2 可以用绑定CheckBox 隐藏自己不想看的字段
      

  9.   

    注意Copy方法是创建跟原表一样的结构,然后你可以对它的列进行增删改。
      

  10.   

    6楼有点误!
    再创建一个dt2,如:
    DataTable dt2=dt.Copy();
    对dt2进行操作!!
      

  11.   

    方法太多了。不过记得datatable 有个select的方法
      

  12.   

    粘个例子,哈哈哈private void GetRowsByFilter()
    {
        DataTable table = DataSet1.Tables["Orders"];    // Presuming the DataTable has a column named Date.
        string expression = "Date > '1/1/00'";    // Sort descending by column named CompanyName.
        string sortOrder = "CompanyName DESC";
        DataRow[] foundRows;    // Use the Select method to find all rows matching the filter.
        foundRows = table.Select(expression, sortOrder);    // Print column 0 of each returned row.
        for(int i = 0; i < foundRows.Length; i ++)
        {
            Console.WriteLine(foundRows[i][0]);
        }
    }