如何动态地向数据集添加列 [英] How to add a column to a dataset dynamically

查看:108
本文介绍了如何动态地向数据集添加列的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用以下代码我想动态添加一列但是我在行中收到此错误

'ds21.Columns.Add(theCol);'





错误13'System.Data.DataSet'不包含'Columns'的定义,也没有扩展方法'Columns'接受类型'System.Data'的第一个参数。可以找到DataSet'(你是否缺少using指令或汇编引用?)



我的代码:



DataSet ds21 = new DataSet(productname);

DataColumn theCol = new DataColumn(Groups,typeof(string));

ds21.Columns。添加(theCol);

da2.Fill(ds21,productname);



如何纠正此问题。

解决方案

DataSet没有列; DataTables可以。



此外,即使您尝试使用DataTable,我也认为这不会起作用。您可以考虑将该列添加到用于获取数据的查询中。


DataSet是DataTables的集合吗?



我知道你可以这样做: -



 DataTable dt21 =  new  DataTable(  productname); 
DataColumn theCol = new DataColumn( Groups typeof string ));
dt21.Columns.Add(theCol);
da2.Fill(dt21, productname);





然后你可能会这样做: -



 DataSet  set  =  new  DataSet( < span class =code-string> productmaster); 
set .Tables.Add(dt21);





例如



....



'g'


< blockquote>试试吧..





 DataTable ds21 =  new  DataTable(  productname); 
da2.Fill(ds21);
DataColumn theCol = new DataColumn( Groups typeof string ));
ds21.Columns.Add(theCol);


With the following code I wanted to add a column dynamically but I get this error at line
'ds21.Columns.Add(theCol);'


Error 13 'System.Data.DataSet' does not contain a definition for 'Columns' and no extension method 'Columns' accepting a first argument of type 'System.Data.DataSet' could be found (are you missing a using directive or an assembly reference?)

My code:

DataSet ds21 = new DataSet("productname");
DataColumn theCol = new DataColumn("Groups", typeof(string));
ds21.Columns.Add(theCol);
da2.Fill(ds21, "productname");

How to rectify this.

解决方案

DataSets don't have Columns; DataTables do.

Additionally, I don't think that will work even if you try to do it with a DataTable. You might consider adding the column to the query you are using to get the data.


Isnt a DataSet a collection of DataTables ?

I know you can do this :-

DataTable dt21 = new DataTable("productname");
DataColumn theCol = new DataColumn("Groups", typeof(string));
dt21.Columns.Add(theCol);
da2.Fill(dt21, "productname");



and then you'd likely do this :-

DataSet set = new DataSet("productmaster");
	set.Tables.Add(dt21);



for example

....

'g'


Try this..


DataTable ds21 = new DataTable("productname");
    da2.Fill(ds21);
    DataColumn theCol = new DataColumn("Groups", typeof(string));
    ds21.Columns.Add(theCol);


这篇关于如何动态地向数据集添加列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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