C# 选择数据表的某些列
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/13760072/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
select certain columns of a data table
提问by Jim Brad
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 |
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
我想选择列 col1、col2、col6、col7、col3。并在数据表中的行的网格视图中显示数据..目前我使用的代码在下面并且只选择某些数据。我不是从 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++;
}
采纳答案by MrFox
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");
Used the sample data to test this method I found: Create ADO.NET DataView showing only selected Columns
使用示例数据来测试我发现的这个方法: Create ADO.NET DataView show only selected Columns
回答by Derek
The question I would ask is, why are you including the extra columns in your DataTable if they aren't required?
我想问的问题是,如果不需要,为什么要在 DataTable 中包含额外的列?
Maybe you should modify your SQL select statement so that it is looking at the specific criteria you are looking for as you are populating your DataTable.
也许您应该修改您的 SQL 选择语句,以便它在您填充数据表时查看您正在寻找的特定条件。
You could also use LINQ to query your DataTable as Enumerable and create a List Object that represents only certain columns.
您还可以使用 LINQ 将您的 DataTable 查询为 Enumerable 并创建一个仅表示某些列的 List 对象。
Other than that, hide the DataGridView Columns that you don't require.
除此之外,隐藏不需要的 DataGridView 列。
回答by pawciu
Here's working example with anonymous output record, if you have any questions place a comment below:????????????????????
这是匿名输出记录的工作示例,如果您有任何问题,请在下面发表评论:??????????????????????
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();
}
}
回答by Ramkumar Srinivasan
Also we can try like this,
我们也可以这样试试,
string[] selectedColumns = new[] { "Column1","Column2"};
DataTable dt= new DataView(fromDataTable).ToTable(false, selectedColumns);
回答by Sunday Efeh
You can create a method that looks like this:
您可以创建一个如下所示的方法:
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;
}
You can return as many columns as possible.. just add the columns as call parameters as shown below:
您可以返回尽可能多的列。只需将列添加为调用参数,如下所示:
public DataTable SelectedColumns(DataTable RecordDT_, string col1, string col2,string col3,...)
and also add the parameters to this line:
并将参数添加到这一行:
System.Data.DataTable selected = view.ToTable("Selected", false,col1, col2,col3,...);
Then simply implement the function as:
然后简单地将函数实现为:
DataTable myselectedColumnTable=SelectedColumns(OriginalTable,"Col1","Col2",...);
Thanks...
谢谢...
回答by Shivnandan Chauhan
DataView dv = new DataView(Your DataTable);
DataTable dt = dv.ToTable(true, "Your Specific Column Name");
The dt contains only selected column values.
dt 仅包含选定的列值。

