In this article I will explain with an example, how to create new DataTable from existing DataTable in Windows Forms (WinForms) Application using C# and VB.Net.
Form Design
The Form consists of following controls:
DataGridView – For displaying data.
Button – For copy data from one DataTable to another DataTable.
The Button has been assigned with a Click event handler.
Namespaces
You will need to import the following namespace.
VB.Net
Creating Dummy DataTable in C# and VB.Net
Inside the Form Load event handler, a DataTable object is created and columns are defined using AddRange method of the DataTable.
The AddRange method accepts an Array of objects of type DataColumn.
Then, the name and the optional parameter Data Type i.e. the Type of data of column is specified in the DataColumn object.
Once the schema is ready, some rows are added to the DataTable.
Finally, DataTable is assigned to the DataSource property of the DataGridView and the DataGridView is populated.
C#
private void Form1_Load(object sender, EventArgs e)
{
DataTable dt = new DataTable();
dt.Columns.AddRange(new DataColumn[3] {
new DataColumn("CustomerId", typeof(int)),
new DataColumn("Name", typeof(string)),
new DataColumn("Country", typeof(string)) });
dt.Rows.Add(1, "John Hammond", "United States");
dt.Rows.Add(2, "Mudassar Khan", "India");
dt.Rows.Add(3, "Suzanne Mathews", "France");
dt.Rows.Add(4, "Robert Schidner", "Russia");
dataGridView1.DataSource = dt;
}
VB.Net
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
Dim dt As DataTable = New DataTable()
dt.Columns.AddRange(New DataColumn(2) {
New DataColumn("CustomerId", GetType(Integer)),
New DataColumn("Name", GetType(String)),
New DataColumn("Country", GetType(String))})
dt.Rows.Add(1, "John Hammond", "United States")
dt.Rows.Add(2, "Mudassar Khan", "India")
dt.Rows.Add(3, "Suzanne Mathews", "France")
dt.Rows.Add(4, "Robert Schidner", "Russia")
DataGridView1.DataSource = dt
End Sub
Creating new DataTable from existing DataTable in C# and VB.Net
When the Button is clicked, a DataTable object is created.
A FOR EACH loop is executed over the columns of the DataGridView and added to the newly created DataTable object.
After that, another FOR EACH loop is executed over the rows of the DataGridView and the data is added as rows to the DataTable object.
C#
private void CopyToDataTable(object sender, EventArgs e)
{
DataTable dtCustomer = new DataTable("Customer");
foreach (DataGridViewColumn cell in dataGridView1.Columns)
{
dtCustomer.Columns.Add(cell.HeaderText);
}
foreach (DataGridViewRow row in dataGridView1.Rows)
{
dtCustomer.Rows.Add(row);
for (int i = 0; i < row.Cells.Count; i++)
{
dtCustomer.Rows[row.Index][i] = row.Cells[i].Value;
}
}
}
VB.Net
Private Sub CopyToDataTable(sender As Object, e As EventArgs) Handles btnCopy.Click
Dim dtCustomer As DataTable = New DataTable("Customer")
For Each cell As DataGridViewColumn In DataGridView1.Columns
dtCustomer.Columns.Add(cell.HeaderText)
Next
For Each row As DataGridViewRow In DataGridView1.Rows
dtCustomer.Rows.Add(row)
For i As Integer = 0 To row.Cells.Count - 1
dtCustomer.Rows(row.Index)(i) = row.Cells(i).Value
Next
Next
End Sub
Screenshots
DataGridView
New DataTable
Downloads