Click here to Skip to main content
15,889,335 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
hi, I want two data tables merge side by side if i merge data table am getting tables one below other how can i display side by side .... its urgent please give me suggistion for this

[edit]spelling and "urgent"[/edit]
Posted
Updated 12-Jun-13 23:40pm
v2

C#
<pre> 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;
        }

 
Share this answer
 
Comments
Arkadeep De 5-Nov-15 3:15am    
this code just saved my day.
Member 10714689 8-Mar-19 2:13am    
Thanks a lot, it saved my time
If you want to display data of two datatables as a single table then you will have to create a third datatable with number of columns equal to the sum of number of columns in first datatable and number of columns of second datatable. Iterate both the datatables and copy the data to the third datatable. If the data is coming from database you can use joins. If there is no relationship between tables you can use the temporary table to merge data of two tables. There are many ways. Depends upon what you you find easier and quicker...
 
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