.net c#将特定的数据集列复制到数据表

Copy Specific Dataset Columns to Datatable
2021-06-20
  •  译文(汉语)
  •  原文(英语)

我有一个读取xml数据的DataSet,它有将近20列,我需要从DataSet中获取5列.我已经在我的水平上尝试过,但是无法获得需要的特定列的DataTable.我尝试过的一些代码是:

DataTable dt = new DataTable();  //dt is blank DataTable 
DataSet ds = new DataSet();  //ds is existing DataSet which has single table in it
dt.Columns[0]=dst4.Tables[0].Columns[0];               
dt.Columns.Add(dst4.Tables[0].Columns[0]);  
ds.Tables.Add(dt)
dst4.Tables.Columns.Add(dt);
速聊1:
您要哪几列?您说5,但是其中的代码没有显示.
解决过程1

说您必须将列"ColumnName1","ColumnName2","ColumnName3","ColumnName4","ColumnName5"复制到新 DataTable

DataTable source = dst4.Tables[0];
DataTable dt = source.DefaultView.ToTable(false, "ColumnName1", "ColumnName2", "ColumnName3", "ColumnName4", "ColumnName5"); 
解决过程2

您可以克隆表以创建具有相同列的空表,然后删除多余的列.这里是一个带有命名列的示例,也可以使用索引:

DataTable dt = ds.Tables[0].Clone();
var colToTake = new[] { "Col1", "Col3", "Col7", "Col15", "Col19" };
var colsToRemove = dt.Columns.Cast<DataColumn>()
    .Where(c => !colToTake.Contains(c.ColumnName));
foreach (DataColumn colToRemove in colsToRemove)
    dt.Columns.Remove(colToRemove);
速聊1:
我试图通过从数据集中删除不需要的列来完成任务的另一种方法.ds.Tables [0] .Columns.Remove("column1");
速聊2:
: 你让我困惑.为什么要从原始表中删除列,而不是从空的克隆表中删除?那不是您在问题中提到的.而且我也想念它是否有效.
速聊3:
抱歉,我要告诉我,我必须将数据集显示在datagridview上,这是数据集的唯一用途,这就是为什么我从数据集中删除了特定的不需要的列,现在可以将其绑定到datagridview的原因.
速聊4:
:也许这会有所帮助:stackoverflow.com/questions/208186/…

i have a DataSet which reads xml data and it has almost 20 columns, i need 5 columns from the DataSet . i have tried at my level but not able to get the DataTable with the specific columns i need. some codes which i tried are :

DataTable dt = new DataTable();  //dt is blank DataTable 
DataSet ds = new DataSet();  //ds is existing DataSet which has single table in it
dt.Columns[0]=dst4.Tables[0].Columns[0];               
dt.Columns.Add(dst4.Tables[0].Columns[0]);  
ds.Tables.Add(dt)
dst4.Tables.Columns.Add(dt);
Talk1:
Which columns do you want? You say 5 but which, your code does not show it.
Solutions1

say you have to copy columns "ColumnName1", "ColumnName2", "ColumnName3", "ColumnName4", "ColumnName5" to new DataTable

DataTable source = dst4.Tables[0];
DataTable dt = source.DefaultView.ToTable(false, "ColumnName1", "ColumnName2", "ColumnName3", "ColumnName4", "ColumnName5"); 
Solutions2

You could clone the table which creates an empty table with the same columns, then you can remove the redundant columns.Here is an example with named columns, you could also use the indices:

DataTable dt = ds.Tables[0].Clone();
var colToTake = new[] { "Col1", "Col3", "Col7", "Col15", "Col19" };
var colsToRemove = dt.Columns.Cast<DataColumn>()
    .Where(c => !colToTake.Contains(c.ColumnName));
foreach (DataColumn colToRemove in colsToRemove)
    dt.Columns.Remove(colToRemove);
Talk1:
i tried another way to accomplish task by removong unwanted columns from dataset . ds.Tables[0].Columns.Remove("column1");
Talk2:
: You confuse me. Why do you remove columns from the original tables instead of the empty cloned table? That's not what you have mentioned in your question. And i'm also missing if it works.
Talk3:
sorry , let me tell , i have to show the dataset on to the datagridview, this is the only use of dataset , that's why i removed specific unwanted columns from dataset and now can bind it to datagridview.
Talk4:
转载于:https://stackoverflow.com/questions/18530906/copy-specific-dataset-columns-to-datatable

本人是.net程序员,因为英语不行,使用工具翻译,希望对有需要的人有所帮助
如果本文质量不好,还请谅解,毕竟这些操作还是比较费时的,英语较好的可以看原文

留言回复
我们只提供高质量资源,素材,源码,坚持 下了就能用 原则,让客户花了钱觉得值
上班时间 : 周一至周五9:00-17:30 期待您的加入