Home Articles

选择数据表的某些列

Asked
Viewed 1295 times
17

我有一个数据表,想知道我是否可以选择某些列并在表格上输入数据 . 列列出如下

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

我想选择列col1,col2 col6,col7,col3 . 并在数据表中的行的网格视图中显示数据..当前我正在使用的代码在下面并且onmly选择某些数据 . 我不是从sql中选择数据,而是从另一个存储在数据表中的excel中选择数据..但是我也需要另一个区域中的其他列...这个数据被写入一个表中的单词

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++;
                }

6 Answers

  • 35

    我们也可以这样试试,

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

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

    System.Data.DataTable table = new System.Data.DataTable();
    for (int i = 1; i <= 11; i++)
        table.Columns.Add("col" + i.ToString());
    
    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);
    }
    
    System.Data.DataView view = new System.Data.DataView(table);
    System.Data.DataTable selected = view.ToTable("Selected", false, "col1", "col2", "col6", "col7", "col3");
    

    使用示例数据来测试我找到的这个方法:Create ADO.NET DataView showing only selected Columns

  • 1

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

    也许您应该修改SQL select语句,以便在填充DataTable时查看您要查找的特定条件 .

    您还可以使用LINQ将DataTable作为Enumerable进行查询,并创建仅表示某些列的List对象 .

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

  • 0

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

    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",...);
    

    谢谢...

  • 0
    DataView dv = new DataView(Your DataTable);
    
    DataTable dt = dv.ToTable(true, "Your Specific Column Name");
    

    dt仅包含选定的列值 .

  • -1

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

    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();
        }
    }
    

Related