Karthik K

Karthik K

  • 1k
  • 738
  • 192.4k

datatable loop values into another datatable in c# ?

Apr 4 2019 2:20 AM
Hi,
 
I want to get the values from datatable through loop and add it to another datatable.
 
I have a code that adds the current row on every iteration. If anybody knows, Let me know with example Code..
 
Sample_Code:
  1. string ChartQry = "SELECT A.TransDate,concat(DATE_FORMAT(a.transtime,'%h:%00 %p'),'-',DATE_FORMAT(a.transtime,'%h:59 %p')) as Hour,DATE_FORMAT(a.transtime,'%H') as TimeGroup,Date_format(TransTime,'%y-%m-%d %H:00:00') as DateTime, Sum(B.TaxableAmount+B.TaxAmount1+B.TaxAmount2+B.TaxAmount3+B.TaxAmount4+B.TaxAmount5+B.TaxAmount6) As Amount, DATE_FORMAT(a.transtime,'%h:%00 %p') as TM FROM " + Branch_AliasName + ".Trans A JOIN " + Branch_AliasName + ".TransDetail B on A.Transid=B.Transid Left Join " + Branch_AliasName + ".Customers C On a.CustomerNumber=C.CustomerNumber Where (A.BusinessDate Between '" + D1 + "' And '" + D2 + "') And B.ItemStatus<>1 Group by Hour ORDER BY HOUR(a.TransTime);";  
  2. ReadResult = ObjMainCloud.GetData_DT(ChartQry);  
  3. DataTable DtFinaldata = new DataTable();  
  4. DataRow Dr = DtFinaldata.NewRow();  
  5. foreach (DataRow row in ReadResult.Rows)  
  6. {  
  7. RTimeGroupValue = row["TimeGroup"].ToString();  
  8. RAmount = row["Amount"].ToString();  
  9. if (DtFinaldata.Rows.Count > 0)  
  10. {  
  11. Dr["Tm"] = RTimeGroupValue;  
  12. Dr["Amount"] = RAmount;  
  13. DtFinaldata.Rows.Add(Dr);  
  14. DtFinaldata.AcceptChanges();  
  15. }  
  16. else  
  17. {  
  18. DtFinaldata.Columns.Add("Tm"typeof(string));  
  19. DtFinaldata.Columns.Add("Amount"typeof(decimal));  
  20. Dr["Tm"] = RTimeGroupValue;  
  21. Dr["Amount"] = RAmount;  
  22. }  
  23. DtFinaldata.Rows.Add(Dr);  
  24. DtFinaldata.AcceptChanges();  
  25. }

Answers (1)