0

I have tried to create a data table in C#, but I want to achieve similar results to these shown in this screenshot:

enter image description here

I can create dynamic columns but can not create rows.

DataTable dt_bq = new DataTable();
dt_bq.Columns.Add("Date", typeof(string));
dt_bq.Columns.Add("12/8", typeof(string));
dt_bq.Columns.Add("13/8", typeof(string));
dt_bq.Columns.Add("Total", typeof(string));
DataRow dr_tot = dt_bq.NewRow();
marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
Shivani
  • 11
  • 2
  • [DataRowExtensions.SetField Method](https://learn.microsoft.com/en-us/dotnet/api/system.data.datarowextensions.setfield?view=netframework-4.8). Remember that `.NewRow` will only create an instance of new row, you need manually add it to the datatable later. – Fabio Dec 02 '19 at 04:21
  • Got it. But I wanted to achieve a result similar to the image. – Shivani Dec 02 '19 at 04:27
  • So what is the problem? Specific problem, not just "not working" please ;) – Fabio Dec 02 '19 at 04:36
  • You want to display this on your `View` or only need it in your backend to process your data? – Rahul Sharma Dec 02 '19 at 04:54
  • Does this answer your question? [How can I manually / programmatically create a DataRow?](https://stackoverflow.com/questions/44010345/how-can-i-manually-programmatically-create-a-datarow) – Circle Hsiao Dec 02 '19 at 06:35

1 Answers1

0

https://learn.microsoft.com/en-us/dotnet/api/system.data.datatable?view=netframework-4.8

// Setting up header of table
DataTable dt_bq = new DataTable();
dt_bq.Columns.Add("Date", typeof(string));
dt_bq.Columns.Add("12/8", typeof(string));
dt_bq.Columns.Add("13/8", typeof(string));
dt_bq.Columns.Add("14/8", typeof(string));
dt_bq.Columns.Add("Total", typeof(string));

// Insert rows for that table
DataRow dr_tot = dt_bq.NewRow();
dr_tot["Date"] = "Issue Qty";
dr_tot["12/8"] = "10";
dr_tot["13/8"] = "20";
dr_tot["14/8"] = "30";
dr_tot["Total"] = "60";
dt_bq.Rows.Add(dr_tot);

dr_tot = dt_bq.NewRow();
dr_tot["Date"] = "Retrun Qty";
dr_tot["12/8"] = "5";
dr_tot["13/8"] = "20";
dr_tot["14/8"] = "30";
dr_tot["Total"] = "55";
dt_bq.Rows.Add(dr_tot);

dr_tot = dt_bq.NewRow();
dr_tot["Date"] = "Balance";
dr_tot["12/8"] = "5";
dr_tot["13/8"] = "0";
dr_tot["14/8"] = "0";
dr_tot["Total"] = "5";
dt_bq.Rows.Add(dr_tot);

You can add rows with column's name as index to the row. Then insert that row to the table.

Jawad
  • 11,028
  • 3
  • 24
  • 37