C# 将数据集追加到其他数据集
数据集1C# 将数据集追加到其他数据集,c#,asp.net,.net,C#,Asp.net,.net,数据集1 col1 col2 ------- 1a 2b 3c 数据集2 col1 ---- 医生 工程师 程序员 我希望在网格视图中这样做 col1 col2 col3 -------- 1 adoctor 2b工程师 在您的情况下,您不需要合并两个数据表 您可以改用linq var datasource = from r1 in table1.AsEnumerable().Select((r, i) => new { Value = r, Index = i })
col1 col2
-------
1a
2b
3c
数据集2
col1
----
医生
工程师
程序员
我希望在网格视图中这样做
col1 col2 col3
--------
1 adoctor
2b工程师
在您的情况下,您不需要合并两个数据表 您可以改用linq
var datasource = from r1 in table1.AsEnumerable().Select((r, i) => new { Value = r, Index = i })
from r2 in table2.AsEnumerable().Select((r, i) => new { Value = r, Index = i })
where r1.Index == r2.Index
select new
{
col1 = r1.Value["col1"].ToString(),
col2 = r1.Value["col2"].ToString(),
col3 = r2.Value["col1"].ToString(),
};
然后,像装订一样
datagridview.datasource = datasource ;
如果您想将两个表关联起来,那么在同一数据集中实现这一点的推荐方法是 这是linq方法的替代方法 为此,必须使用DataRelation 当然,您必须创建一个foreignkeycontracint来实现这一点 下面是一个例子:
Dim relation As DataRelation
Dim ParentColumn As DataColumn
Dim ChildColumn As DataColumn
Dim foreignkey As ForeignKeyConstraint
ParentColumn = Me.dataset.Tables("table1").Columns(PKTable1)
ChildColumn = Me.dataset.Tables("table2").Columns(FKTable2)
foreignkey = New ForeignKeyConstraint("fkey_table1_table2", ParentColumn, ChildColumn)
foreignkey.DeleteRule = Rule.Cascade
foreignkey.UpdateRule = Rule.Cascade
foreignkey.AcceptRejectRule = AcceptRejectRule.Cascade
relation = New DataRelation("Relation_table1_table2", ParentColumn, ChildColumn, True)
table2.Constraints.Add(foreignkey)
Me.dataset.Relations.Add(relation)
现在您只需将表2指定为数据网格的数据源
datagrid.datasource=dataset
datagrid.datamember= "table2"
在dataset1中创建新列(实际上列在DataTable中,DataSets保存表)并从dataset2添加值为什么不在dataaccess层实现它呢?那么您想合并两个表吗?数据集没有列