简体   繁体   English

C#将一个数据表拆分为多个数据集

[英]c# split one datatable to several into dataset

I have correct source DataTable "sourceDataTable" and I call method to split it into several and store the result into DataSet "ds": 我有正确的源DataTable“ sourceDataTable”,并且我调用了将其拆分为多个并将结果存储到DataSet“ ds”中的方法:

 DataSet ds = MyClass.SplitDataTables(sourceDataTable);

Here is the method MyClass.SplitDataTables(): 这是方法MyClass.SplitDataTables():

public static DataSet SplitDataTables(DataTable sourceDataTable)
    {
        using (DataSet dsOut = new DataSet())
        {
            DataTable dt1 = new DataTable;
            DataTable dt2 = new DataTable;
            DataTable dt3 = new DataTable;

            dt1 = sourceDataTable.Clone();
            dt2 = sourceDataTable.Clone();
            dt3 = sourceDataTable.Clone();

            foreach (DataRow row in sourceDataTable.Rows)
            {
                //column is for example "City" and some row has "Boston" in it, so I put this row into dt1
                if (row["ColumnName"].ToString() == "something")
                {
                    dt1.ImportRow(row);
                }
                else if (...)
                {  } //for other DataTables dt2, dt3, etc...

                else .......... ;
            }


            //here I put resulting DataTables into one DataSet which is returned
            string[] cols= { "dt1", "dt2", "dt3" };

            foreach (string col in cols)
            {
                dsOut.Tables.Add(col);
            }

            return dsOut;

        }
    }

So with this returned DataSet I display new Windows each with one DataTable 因此,使用此返回的数据集,我将显示每个带有一个数据表的新Windows

foreach (DataTable dtt in ds.Tables)
                    {
                        string msg = dtt.TableName;
                        Window2 win2 = new Window2(dtt, msg);
                        win2.Show();
                    }

All I get shown is Windows with placeholder for "empty DataGrid" Windows code is correct, as it works whith "unsplit DataTable". 我所看到的只是带有“空DataGrid”占位符的Windows Windows代码正确,因为它可以与“ unsplit DataTable”一起工作。

I assume code in splitting DataTables is all wrong as it does not output DataSet with filled DataTables. 我认为拆分DataTables中的代码都是错误的,因为它不会输出填充了DataTables的DataSet。 I will greatly appreciate any help on this issue. 我将非常感谢您对此问题的任何帮助。 Thank you! 谢谢!

You don't need a for loop here 您这里不需要for循环

Replace below code 替换下面的代码

string[] cols= { "dt1", "dt2", "dt3" };

foreach (string col in cols)
   {
     dsOut.Tables.Add(col);
   }

With this 有了这个

dsOut.Tables.Add(dt1);
dsOut.Tables.Add(dt2);
dsOut.Tables.Add(dt3);

Thanks to @Krishna I got this solved. 感谢@Krishna,我解决了这个问题。 So if you ever encounter similar problem here are 2 things to note: 因此,如果您遇到类似的问题,请注意以下两点:

string[] cols = { "dt1", "dt2", "dt3", ... };

            foreach (string col in cols)
            {
                dsOut.Tables.Add(col);
            }

This cycle does not have access to objects of DataTables with same name and writes only empty DataTables into DataSet collection (regardless of same name!). 此循环无权访问具有相同名称的DataTables对象,并且仅将空的DataTables写入DataSet集合(无论名称相同!)。

If you create new DataTable and you will be making it a clone of another DataTable, dont bother yet with setting its name. 如果创建新的DataTable,并将其克隆为另一个DataTable,则不必设置名称。

DataTable dt1 = new DataTable();

Make new DataTable of same format as source DataTable: 制作与源数据表格式相同的新数据表:

dt1 = sourceDataTable.Clone();
dt2 = sourceDataTable.Clone();
//etc...

Now you have to set unique DataTable names to each DataTable cloned from source DataTable: 现在,您必须为从源数据表克隆的每个数据表设置唯一的数据表名称:

dt1.TableName = "Name1";
dt2.TableName = "Name2";
//and so on

Now all works as intended. 现在所有工作都按预期进行。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM