Aspdotnet-Suresh

aspdotnet-suresh offers C#.net articles and tutorials,csharp dot net,asp.net articles and tutorials,VB.NET Articles,Gridview articles,code examples of asp.net 2.0 /3.5,AJAX,SQL Server Articles,examples of .net technologies

Transfer (Copy) Rows From Gridview Data to Datatable in Asp.net using C#, VB.NET

Jul 15, 2015
Introduction

Here I will explain how to copy
gridview rows data to datatable in asp.net using c#, vb.net with example or transfer or add rows from asp.net gridview to datatable using c#, vb.net with example.


Before implement this example first design one table productinfo in your database as shown below

Column Name
Data Type
Allow Nulls
productid
Int(IDENTITY=TRUE)
Yes
productname
varchar(50)
Yes
price
varchar(50)
Yes
Once table created in database enter some dummy data to test application now open your aspx page and write the code like as shown below


<html xmlns="http://www.w3.org/1999/xhtml">
<head id="Head1" runat="server">
<title>Transfer Gridview Rows to Datatable in asp.net using c#, vb.net</title>
<style type="text/css">
.GridviewDiv {font-size: 100%; font-family: 'Lucida Grande', 'Lucida Sans Unicode', Verdana, Arial, Helevetica, sans-serif; color: #303933;}
.headerstyle
{
color:#FFFFFF;border-right-color:#abb079;border-bottom-color:#abb079;background-color: #df5015;padding:0.5em 0.5em 0.5em 0.5em;text-align:center;
}
</style>
</head>
<body>
<form id="form1" runat="server">
<div class="GridviewDiv">
<asp:GridView runat="server" ID="gvDetails" AllowPaging="true" PageSize="10" AutoGenerateColumns="false" Width="420px" OnPageIndexChanging="gvDetails_PageIndexChanging">
<HeaderStyle CssClass="headerstyle" />
<Columns>
<asp:BoundField DataField="productid" HeaderText="Product Id" />
<asp:BoundField DataField="productname" HeaderText="Product Name" />
<asp:BoundField DataField="price" HeaderText="Price" />
</Columns>
</asp:GridView>
</div>
<br />
<asp:Button ID="btnTransfer" runat="server" Text="Gridview Rows to Datatable" Font-Bold="true" onclick="btnTransfer_Click" /><br />
</form>
</body>
</html>

After completion of aspx page add following namespaces in codebehind

C# Code


using System;
using System.Web.UI.WebControls;
using System.Data;
using System.Data.SqlClient;

After completion of adding namespaces you need to write the code like as shown below


protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
BindGridview();
}
}
protected void BindGridview()
{
DataSet ds = new DataSet();
using (SqlConnection con = new SqlConnection("Data Source=Suresh;Integrated Security=true;Initial Catalog=MySampleDB"))
{
con.Open();
SqlCommand cmd = new SqlCommand("select * from productinfo", con);
SqlDataAdapter da = new SqlDataAdapter(cmd);
DataTable dt = new DataTable();
da.Fill(dt);
con.Close();
gvDetails.DataSource = dt;
gvDetails.DataBind();
}
}
protected void gvDetails_PageIndexChanging(object sender, GridViewPageEventArgs e)
{
gvDetails.PageIndex = e.NewPageIndex;
BindGridview();
}
// Transfer Gridview Rows to datatable
protected void btnTransfer_Click(object sender, EventArgs e)
{
String strConnection = "Data Source=Suresh;Initial Catalog=MySampleDB;Integrated Security=True";
DataTable dt = new DataTable("productinfo");
dt.Columns.Add("productid", typeof(int));
dt.Columns.Add("productname", typeof(string));
dt.Columns.Add("price", typeof(int));
foreach (GridViewRow row in gvDetails.Rows)
{
int userid = int.Parse(row.Cells[0].Text);
string productname = row.Cells[1].Text;
int price = int.Parse(row.Cells[2].Text);
dt.Rows.Add(userid, productname, price);
}
}

VB.NET Code


Imports System.Web.UI.WebControls
Imports System.Data
Imports System.Data.SqlClient

Partial Class VBCode
Inherits System.Web.UI.Page
Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs) Handles Me.Load
If Not IsPostBack Then
BindGridview()
End If
End Sub
Protected Sub BindGridview()
Dim ds As New DataSet()
Using con As New SqlConnection("Data Source=Suresh;Integrated Security=true;Initial Catalog=MySampleDB")
con.Open()
Dim cmd As New SqlCommand("select * from productinfo", con)
Dim da As New SqlDataAdapter(cmd)
Dim dt As New DataTable()
da.Fill(dt)
con.Close()
gvDetails.DataSource = dt
gvDetails.DataBind()
End Using
End Sub
Protected Sub gvDetails_PageIndexChanging(ByVal sender As Object, ByVal e As GridViewPageEventArgs)
gvDetails.PageIndex = e.NewPageIndex
BindGridview()
End Sub
' Transfer Gridview Rows to datatable
Protected Sub btnTransfer_Click(ByVal sender As Object, ByVal e As EventArgs)
Dim strConnection As [String] = "Data Source=Suresh;Initial Catalog=MySampleDB;Integrated Security=True"
Dim dt As New DataTable("productinfo")
dt.Columns.Add("productid", GetType(Integer))
dt.Columns.Add("productname", GetType(String))
dt.Columns.Add("price", GetType(Integer))
For Each row As GridViewRow In gvDetails.Rows
Dim userid As Integer = Integer.Parse(row.Cells(0).Text)
Dim productname As String = row.Cells(1).Text
Dim price As Integer = Integer.Parse(row.Cells(2).Text)
dt.Rows.Add(userid, productname, price)
Next
End Sub
End Class

Demo

Transfer (Copy) Rows From Gridview Data to Datatable in Asp.net using C#, VB.NET

Download Sample Code Attached


If you enjoyed this post, please support the blog below. It's FREE!

Get the latest Asp.net, C#.net, VB.NET, jQuery, Plugins & Code Snippets for FREE by subscribing to our Facebook, Twitter, RSS feed, or by email.

subscribe by rss Subscribe by RSS subscribe by email Subscribe by Email

0 comments :

Give your Valuable Comments

Other Related Posts

© 2015 Aspdotnet-Suresh.com. All Rights Reserved.
The content is copyrighted to Suresh Dasari and may not be reproduced on other websites without permission from the owner.