选择数据表的某些列 [英] select certain columns of a data table

查看:97
本文介绍了选择数据表的某些列的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I have a datatable and would like to know if its possible for me to select certain columns and input the data on a table. the columns are set out as below

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

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

我想选择列col1,col2,col6,col7,col3。并在数据表中的行的gridview中支付数据。.目前,我正在使用的代码在下面,并且仅选择某些数据。我不是从sql中选择数据,而是从存储在数据表中的另一个excel中选择其数据..但是我也需要另一个区域中的其他列。.此数据正以word

I want to select column col1, col2 col6, col7,col3. and dispay the data in a gridview of the rows within the datatable.. currently the code that i am using is below and onmly selects certain data. I am not selecting the data from sql its data being selected from another excel which is stored in a datatable.. but i am in need of the other columns in another area as well.. this data is being written into a table in 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++;
                }


推荐答案

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

First store the table in a view, then select columns from that view into a new table.

// 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");

使用示例数据来测试此方法,我发现:
创建仅显示选定列的ADO.NET DataView

Used the sample data to test this method I found: Create ADO.NET DataView showing only selected Columns

这篇关于选择数据表的某些列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆