Click here to Skip to main content
15,913,758 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
public DataTable Merge(DataTable[] dataTables)
        {
            List<int> oList = new List<int>();
            DataTable mergedDataTable = new DataTable();
            foreach (DataTable dt in dataTables)
            {
                oList.Add(dt.Rows.Count);
                foreach (DataColumn dc in dt.Columns)
                {
                    mergedDataTable.Columns.Add(dt.TableName + "-" + dc.ColumnName, dc.DataType);
                }
                //mergedDataTable.Columns.Add(dt.TableName + "-" + "Space");
            }
            int temp = 0;
            for (int m = 0; m < oList.Count; m++)
            {
                for (int n = 0; n < oList.Count - 1; n++)
                {
                    if (oList[n] > oList[n + 1])
                    {
                        temp = oList[n + 1];
                        oList[n + 1] = oList[n];
                        oList[n] = temp;
                    }
                }
            }
            int maxRow = oList[oList.Count - 1];
            for (int o = 0; o < maxRow; o++)
            {
                DataRow newRow = mergedDataTable.NewRow();
                int mergedDataTableColumn = 0;
                foreach (DataTable dt in dataTables)
                {
                    if (dt.Rows.Count > o)
                    {
                        for (int k = 0; k < dt.Columns.Count; k++)
                        {
                            newRow[mergedDataTableColumn] = dt.Rows[o][k];
                            mergedDataTableColumn++;
                        }
                    }
                    else
                    {
                        for (int k = 0; k < dt.Columns.Count; k++)
                        {
                            newRow[mergedDataTableColumn] = DBNull.Value;
                            mergedDataTableColumn++;
                        }
                    }
                    //newRow[mergedDataTableColumn] = DBNull.Value;
                    //mergedDataTableColumn++;
                }
                mergedDataTable.Rows.Add(newRow);
            }
            return mergedDataTable;
        }


What I have tried:

how to call this method and what to pass in this method. Plz help
Posted
Updated 2-Mar-17 1:54am
Comments
Graeme_Grant 2-Mar-17 7:18am    
So it is not your code?
Member 12857358 2-Mar-17 7:20am    
yes it is not my code but i want to use it
Graeme_Grant 2-Mar-17 7:21am    
this is basic programming.
[no name] 2-Mar-17 7:29am    
You would think that we could teach you all there is to know about programming in a forum posting but no we can't. We also can't teach you programming one "question" at a time.

DataTable[] is an array of DataTable ... propramming 101.
 
Share this answer
 
Comments
Member 12857358 2-Mar-17 7:22am    
how to pass a datatable in it
Graeme_Grant 2-Mar-17 7:25am    
Why do you want to use it when you clearly don't know what it does?
Member 12857358 2-Mar-17 7:27am    
i know but little bit confuse. actually i want to merge two dataTable side by side
Graeme_Grant 2-Mar-17 7:29am    
Follow this example

C#
DataTable dt1 = new DataTable("dt1");
            dt1.Columns.Add("Id");
            dt1.Columns.Add("Name");
            dt1.Rows.Add(1, "aa");

            DataTable dt2 = new DataTable("dt2");
            dt2.Columns.Add("Id");
            dt2.Columns.Add("Name");
            dt2.Rows.Add(2, "bb");

            DataTable Final = Merge(new DataTable[] { dt1, dt2 });
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900