如何从C#中的多个数据集中的选定列创建数据集 [英] How to create dataset from selected columns from multiple datasets in C#
问题描述
Hello everyone,
我在一个页面中有5个网格视图。其中4个具有单独的查询和数据集,例如ds1,ds2,ds3,ds4。
现在剩下的一个gridview是从其他数据集的选定列的组合数据中填充的,例如:
ds1col2,ds2col2,ds3col2,ds4col2,ds1col3 + ds2col3 + ds3col3 + ds4col3
所有网格视图都有绑定字段数据字段。
这种组合可能吗?
谢谢.. !!
我尝试了什么:
我尝试过合并,但是它组合了所有表中的所有列,并且一次只能处理2个表。
Hello Everyone,
I have 5 gridviews in a page. 4 of these have separate queries and datasets say ds1, ds2, ds3, ds4.
Now left one gridview is populated from combined data of selected columns of other datasets, like:
ds1col2, ds2col2, ds3col2, ds4col2, ds1col3+ds2col3+ds3col3+ds4col3
All the gridviews have bound-field datafields.
Is this combination possible?
Thank you..!!
What I have tried:
I have tried merging, but that combines all columns from all tables and that works only on 2 tables at a time.
推荐答案
引用这个并根据您的需要定制
refer this and customize for your need
static void Main(string[] args)
{
//ds1col2, ds2col2, ds3col2, ds4col2, ds1col3+ds2col3+ds3col3+ds4col3
DataTable ds1 = new DataTable();
ds1.Columns.Add("CommonCol");
ds1.Columns.Add("Col2");
ds1.Columns.Add("Col3");
ds1.Rows.Add("A", 1, 2);
ds1.Rows.Add("B", 1, 2);
DataTable ds2 = new DataTable();
ds2.Columns.Add("CommonCol");
ds2.Columns.Add("Col2");
ds2.Columns.Add("Col3");
ds2.Rows.Add("A", 1, 2);
DataTable ds3 = new DataTable();
ds3.Columns.Add("CommonCol");
ds3.Columns.Add("Col2");
ds3.Columns.Add("Col3");
ds3.Rows.Add("A", 1, 2);
DataTable ds4 = new DataTable();
ds4.Columns.Add("CommonCol");
ds4.Columns.Add("Col2");
ds4.Columns.Add("Col3");
ds4.Rows.Add("A", 1, 2);
DataTable ds5 = new DataTable();
ds5.Columns.Add("CommonCol");
ds5.Columns.Add("Col1");
ds5.Columns.Add("Col2");
ds5.Columns.Add("Col3");
ds5.Columns.Add("Col4");
ds5.Columns.Add("Col5");
foreach (DataRow row in ds1.Rows)
{
string common = row["CommonCol"].ToString();
double ds1col2 = GetValue(common, ds1);
double ds2col2 = GetValue(common, ds2);
double ds3col2 = GetValue(common, ds3);
double ds4col2 = GetValue(common, ds4);
double total = ds1col2 + ds2col2 + ds3col2+ ds4col2;
var newRow = ds5.NewRow();
newRow["CommonCol"] = common;
newRow["Col1"] = ds1col2;
newRow["Col2"] = ds2col2;
newRow["Col3"] = ds3col2;
newRow["Col4"] = ds4col2;
newRow["Col5"] = total;
ds5.Rows.Add(newRow);
}
}
static double GetValue(string common, DataTable dt) {
double returnValue = 0;
var rows = dt.Select("CommonCol = '" + common + "'");
if (rows.Length > 0)
double.TryParse(rows[0]["Col3"].ToString(), out returnValue);
return returnValue;
}
这篇关于如何从C#中的多个数据集中的选定列创建数据集的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!