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

How to highlight selected text in notepad++

  –> To highlight a block of code in Notepad++, please do the following steps step-1  :- Select the required text. step-2  :- Right click...