In this article I will explain how to copy / add / transfer rows (data) from
GridView to
DataTable in
ASP.Net 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 following
HTML Markup consists of:
GridView – For displaying data.
Columns
The
GridView consists of three
BoundField column.
Button – For copy the DataTable.
<asp:GridView ID="gvCustomers" 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 handler, the connection string is read from the
Web.Config file.
Then, the records are fetched from the
Customers Table of
SQL Server database using
SqlDataAdapter and copied to
DataTable object using
Fill method.
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);
gvCustomers.DataSource = dt;
gvCustomers.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)
gvCustomers.DataSource = dt
gvCustomers.DataBind()
End Using
End Using
End Using
End If
End Sub
Copy (Add) Rows from GridView to DataTable in ASP.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 gvCustomers.HeaderRow.Cells)
{
dtCustomers.Columns.Add(cell.Text);
}
//Loop through the GridView and copy rows.
foreach (GridViewRow row in gvCustomers.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 gvCustomers.HeaderRow.Cells
dtCustomers.Columns.Add(cell.Text)
Next
'Loop through the GridView and copy rows.
For Each row As GridViewRow In gvCustomers.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