In this article I will explain with an example, how to add (insert) rows in DataTable using For Each loop using C# and VB.Net.
This article will illustrate how to loop through GridView rows using For Each loop and add (insert) rows to DataTable using C# and VB.Net.
Database
I have made use of the following table Customers with the schema as follows.
I have already inserted few records in the table.
Note: You can download the database table SQL by clicking the download link below.
HTML Markup
The HTML Markup consists of an ASP.Net GridView which will be populated from database and a Button.
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="false">
<Columns>
<asp:BoundField DataField="CustomerId" HeaderText="CustomerId" ItemStyle-Width="30" />
<asp:BoundField DataField="Name" HeaderText="Name" ItemStyle-Width="150" />
<asp:BoundField DataField="Country" HeaderText="Country" ItemStyle-Width="150" />
</Columns>
</asp:GridView>
<br />
<br />
<asp:Button runat="server" ID="btnCopy" OnClick="CopyToDataTable" Text="Copy to Datatable" />
Namespaces
You will need to import the following namespaces.
C#
using System.Data;
using System.Configuration;
using System.Data.SqlClient;
VB.Net
Imports System.Data
Imports System.Configuration
Imports System.Data.SqlClient
Binding the GridView
Inside the Page Load event of the page, the GridView is populated with the records of the Customers Table.
C#
protected void Page_Load(object sender, EventArgs e)
{
if (!this.IsPostBack)
{
string constr = ConfigurationManager.ConnectionStrings["constr"].ConnectionString;
using (SqlConnection con = new SqlConnection(constr))
{
using (SqlCommand cmd = new SqlCommand("SELECT CustomerId, Name, Country FROM Customers"))
{
cmd.Connection = con;
using (SqlDataAdapter sda = new SqlDataAdapter(cmd))
{
DataTable dt = new DataTable();
sda.Fill(dt);
GridView1.DataSource = dt;
GridView1.DataBind();
}
}
}
}
}
VB.Net
Protected Sub Page_Load(sender As Object, e As EventArgs) Handles Me.Load
If Not Me.IsPostBack Then
Dim constr As String = ConfigurationManager.ConnectionStrings("constr").ConnectionString
Using con As New SqlConnection(constr)
Using cmd As New SqlCommand("SELECT CustomerId, Name, Country FROM Customers")
cmd.Connection = con
Using sda As New SqlDataAdapter(cmd)
Dim dt As New DataTable()
sda.Fill(dt)
GridView1.DataSource = dt
GridView1.DataBind()
End Using
End Using
End Using
End If
End Sub
Add (Insert) rows in DataTable using For Each loop using C# and VB.Net
When the Copy Button is clicked, a new DataTable is created with columns same as that of the GridView. Then a loop is executed and one by one each GridView Row is copied / added / transferred to the DataTable.
C#
protected void CopyToDataTable(object sender, EventArgs e)
{
//Create a new DataTable.
DataTable dtCustomers = new DataTable("Customers");
//Add columns to DataTable.
foreach (TableCell cell in GridView1.HeaderRow.Cells)
{
dtCustomers.Columns.Add(cell.Text);
}
//Loop through the GridView and copy rows.
foreach (GridViewRow row in GridView1.Rows)
{
dtCustomers.Rows.Add();
for (int i = 0; i < row.Cells.Count; i++)
{
dtCustomers.Rows[row.RowIndex][i] = row.Cells[i].Text;
}
}
}
VB.Net
Protected Sub CopyToDataTable(sender As Object, e As EventArgs)
'Create a new DataTable.
Dim dtCustomers As New DataTable("Customers")
'Add columns to DataTable.
For Each cell As TableCell In GridView1.HeaderRow.Cells
dtCustomers.Columns.Add(cell.Text)
Next
'Loop through the GridView and copy rows.
For Each row As GridViewRow In GridView1.Rows
dtCustomers.Rows.Add()
For i As Integer = 0 To row.Cells.Count - 1
dtCustomers.Rows(row.RowIndex)(i) = row.Cells(i).Text
Next
Next
End Sub
Screenshots
GridView populated from Database
DataTable containing the rows copied / added / transferred from the GridView
Downloads