Wednesday 22 August 2018

Merge Data table form data set into single tables using c#

//////////////// cs


public DataTable Merge_Tables(DataSet ds, string sortConditions, string sortType)
    {
        DataTable dt = new DataTable(), Sqldt = new DataTable();
        try
        {
            if (ds.Tables.Count > 0)
            {
                dt = ds.Tables[0].Copy();
                for (int i = 1; i < ds.Tables.Count; i++)
                {
                    DataTable dtMain = ds.Tables[i].Copy();
                    for (int r = 0; r < dtMain.Rows.Count; r++)
                    {
                        dt.ImportRow(dtMain.Rows[r]);
                    }
                }
            }
            dt.AcceptChanges();
            if (sortConditions != "")
            {
                DataView dv = dt.DefaultView;
                dv.Sort = "" + sortConditions + " " + sortType + "";
                Sqldt = dv.ToTable();
            }
            else
            {
                Sqldt = dt.Copy();
            }
            Sqldt.AcceptChanges();
        }
        catch (Exception)
        {
        }

        return Sqldt;
    }


No comments:

Post a Comment

Excel Sort values in ascending order using function TEXTJOIN

 Excel ::  Text ::  1,3,5,2,9,5,11 Result :: 1,2,3,5,5,9,11 Formula ::     TEXTJOIN ( ",",1,SORT(MID(SUBSTITUTE( A1 ,","...