Skip to content

Instantly share code, notes, and snippets.

@pepinho24
Created September 26, 2018 13:59
Show Gist options
  • Save pepinho24/71bb8b427c872a219d299d3b31a4a5e5 to your computer and use it in GitHub Desktop.
Save pepinho24/71bb8b427c872a219d299d3b31a4a5e5 to your computer and use it in GitHub Desktop.
Simplified Orders Table from Northwind database created in C# as a DataTable
public DataTable GetDataTableSource()
{
DataTable dataTable = new DataTable();
dataTable.Columns.Add(new DataColumn("OrderID", typeof(int)));
dataTable.Columns.Add(new DataColumn("OrderDate", typeof(DateTime)));
dataTable.Columns.Add(new DataColumn("Freight", typeof(decimal)));
dataTable.Columns.Add(new DataColumn("ShipName", typeof(string)));
dataTable.Columns.Add(new DataColumn("ShipCountry", typeof(string)));
dataTable.PrimaryKey = new DataColumn[] { dataTable.Columns["OrderID"] };
for (int i = 0; i < 30; i++)
{
// or use the following one-liner with the following arguments
// OrderID, OrderDate, Freight, ShipName, ShipCountry
//dataTable.Rows.Add(i + 1, DateTime.Now, (i + 1) + (i + 1) * 0.1 + (i + 1) * 0.01, "Name " + (i + 1), "Country " + (i + 1));
DataRow row = dataTable.NewRow();
row["OrderID"] = i + 1;
row["OrderDate"] = DateTime.Now;
row["Freight"] = (i + 1) + (i + 1) * 0.1 + (i + 1) * 0.01;
row["ShipName"] = "Name " + (i + 1);
row["ShipCountry"] = "Country " + (i + 1);
dataTable.Rows.Add(row);
}
return dataTable;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment