【问题标题】:select certain columns of a data table选择数据表的某些列
【发布时间】:2012-11-25 10:52:55
【问题描述】:

我有一个数据表,想知道我是否可以选择某些列并在表中输入数据。列如下所示

|col1 |col2 |col3|col4 |col5 |col6|col7 |col8 |col9 |col10 |col11 |

我想选择列 col1、col2、col6、col7、col3。并在数据表中行的网格视图中显示数据。目前我正在使用的代码在下面,并且只选择某些数据。我没有从 sql 中选择数据,它的数据是从存储在数据表中的另一个 excel 中选择的。但我也需要另一个区域中的其他列。这个数据正在被写入 word 中的表中

 for (int i = 1; i < table.Rows.Count; i++)
            {
                for (int j = 0; j < table.Columns.Count; j++)
                {
                    if (j == 0)
                    {
                        val = filteredData.Rows[row][col].ToString();
                    }
                    else
                    {
                        val = filteredData.Rows[row][col].ToString();

                        if (val == "-" || val == "")
                        {
                            val = filteredData.Rows[row][col].ToString();

                        }
                        else
                        {
                            val = Convert.ToString(Math.Round(Convert.ToDouble(filteredData.Rows[row][col]), MidpointRounding.AwayFromZero));

                        }
                    }

                    table[j, i].TextFrame.Text = val;
                    col++;
                }

【问题讨论】:

  • 你为什么不能试试 dt.Columns.Remove("column_name")
  • 我已经尝试过了,但我需要下一部分的其他列

标签: c# datatable ado.net


【解决方案1】:

我要问的问题是,如果不需要,为什么要在 DataTable 中包含额外的列?

也许您应该修改您的 SQL 选择语句,以便它在您填充 DataTable 时查看您正在寻找的特定条件。

您还可以使用 LINQ 将您的 DataTable 查询为 Enumerable 并创建一个仅表示某些列的列表对象。

除此之外,隐藏不需要的 DataGridView 列。

【讨论】:

  • @Derek 是否可以在选择中添加条件?
【解决方案2】:

首先将表存储在视图中,然后从该视图中选择列到新表中。

// Create a table with abitrary columns for use with the example
System.Data.DataTable table = new System.Data.DataTable();
for (int i = 1; i <= 11; i++)
    table.Columns.Add("col" + i.ToString());

// Load the table with contrived data
for (int i = 0; i < 100; i++)
{
    System.Data.DataRow row = table.NewRow();
    for (int j = 0; j < 11; j++)
        row[j] = i.ToString() + ", " + j.ToString();
    table.Rows.Add(row);
}

// Create the DataView of the DataTable
System.Data.DataView view = new System.Data.DataView(table);
// Create a new DataTable from the DataView with just the columns desired - and in the order desired
System.Data.DataTable selected = view.ToTable("Selected", false, "col1", "col2", "col6", "col7", "col3");

使用样本数据测试我发现的这个方法: Create ADO.NET DataView showing only selected Columns

【讨论】:

    【解决方案3】:

    这里是匿名输出记录的工作示例,如果您有任何问题,请在下面发表评论:           

    public partial class Form1 : Form
    {
        DataTable table;
        public Form1()
        {
            InitializeComponent();
            #region TestData
            table = new DataTable();
            table.Clear();
            for (int i = 1; i < 12; ++i)
                table.Columns.Add("Col" + i);
            for (int rowIndex = 0; rowIndex < 5; ++rowIndex)
            {
                DataRow row = table.NewRow();
                for (int i = 0; i < table.Columns.Count; ++i)
                    row[i] = String.Format("row:{0},col:{1}", rowIndex, i);
                table.Rows.Add(row);
            }
            #endregion
            bind();
        }
    
        public void bind()
        {
            var filtered = from t in table.AsEnumerable()
                           select new
                           {
                               col1 = t.Field<string>(0),//column of index 0 = "Col1"
                               col2 = t.Field<string>(1),//column of index 1 = "Col2"
                               col3 = t.Field<string>(5),//column of index 5 = "Col6"
                               col4 = t.Field<string>(6),//column of index 6 = "Col7"
                               col5 = t.Field<string>(4),//column of index 4 = "Col3"
                           };
            filteredData.AutoGenerateColumns = true;
            filteredData.DataSource = filtered.ToList();
        }
    }
    

    【讨论】:

      【解决方案4】:

      我们也可以这样试试,

       string[] selectedColumns = new[] { "Column1","Column2"};
      
       DataTable dt= new DataView(fromDataTable).ToTable(false, selectedColumns);
      

      【讨论】:

      • 这很简洁,正是我已经设计为具有 selectedCol 字符串数组的 MVC 控制器所需要的
      • 这不是和 MrFox 发布的完全一样吗?唯一不同的是,您还创建了selectedColumns 数组,该数组仅使用过一次,而 2 行中只有 1 个。
      • 没有。性能方面,它比多列表的查询和优化代码更好。
      【解决方案5】:

      您可以创建如下所示的方法:

        public static DataTable SelectedColumns(DataTable RecordDT_, string col1, string col2)
          {
              DataTable TempTable = RecordDT_;
      
              System.Data.DataView view = new System.Data.DataView(TempTable);
              System.Data.DataTable selected = view.ToTable("Selected", false, col1, col2);
              return selected;
          }
      

      您可以返回尽可能多的列。只需将列添加为调用参数,如下所示:

      public DataTable SelectedColumns(DataTable RecordDT_, string col1, string col2,string col3,...)
      

      并将参数添加到这一行:

      System.Data.DataTable selected = view.ToTable("Selected", false,col1, col2,col3,...);
      

      然后简单的实现函数为:

       DataTable myselectedColumnTable=SelectedColumns(OriginalTable,"Col1","Col2",...);
      

      谢谢...

      【讨论】:

        【解决方案6】:
        DataView dv = new DataView(Your DataTable);
        
        DataTable dt = dv.ToTable(true, "Your Specific Column Name");
        

        dt 仅包含选定的列值。

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2017-03-21
          • 1970-01-01
          • 2019-04-18
          • 2019-12-17
          • 2013-11-22
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多