Introduction:
Here I will explain how to move or transfer selected checkbox gridview rows to another gridview in asp.net using c#, vb.net or copy one gridview row to another gridview in asp.net using c#, vb.net.
Here I will explain how to move or transfer selected checkbox gridview rows to another gridview in asp.net using c#, vb.net or copy one gridview row to another gridview in asp.net using c#, vb.net.
Description:
In previous posts I explained Display images in gridview from database in asp.net, Enable/Disable checkbox in gridview based on condition, asp.net gridview examples and bind data to textbox control in gridview, Bind data to dropdownlist in gridview and many articles relating to asp.net, c#, vb.net, c#, vb.net. Now I will explain how to move selected rows from one gridview to another gridview in asp.net using c#, vb.net.
In previous posts I explained Display images in gridview from database in asp.net, Enable/Disable checkbox in gridview based on condition, asp.net gridview examples and bind data to textbox control in gridview, Bind data to dropdownlist in gridview and many articles relating to asp.net, c#, vb.net, c#, vb.net. Now I will explain how to move selected rows from one gridview to another gridview in asp.net using c#, vb.net.
To implement this first we need to
write the code in aspx page like this
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>Tranfer selected gridview rows to another gridview in
Asp.net</title>
</head>
<body>
<form id="form1"
runat="server">
<div>
<asp:GridView ID="gvDetails"
AutoGenerateColumns="false"
CellPadding="5"
runat="server">
<Columns>
<asp:TemplateField>
<ItemTemplate>
<asp:CheckBox ID="chkSelect"
runat="server"
AutoPostBack="true"
OnCheckedChanged="chkSelect_CheckChanged"
/>
</ItemTemplate>
</asp:TemplateField>
<asp:BoundField HeaderText="UserId" DataField="UserId" />
<asp:BoundField HeaderText="UserName" DataField="UserName" />
<asp:BoundField HeaderText="Education" DataField="Education" />
<asp:BoundField HeaderText="Location" DataField="Location" />
</Columns>
<HeaderStyle BackColor="#df5015" Font-Bold="true" ForeColor="White" />
</asp:GridView>
<br />
<b>Second Gridview Data</b>
<asp:GridView ID="gvTranferRows"
AutoGenerateColumns="false"
CellPadding="5"
runat="server"
EmptyDataText="No
Records Found">
<Columns>
<asp:BoundField HeaderText="UserId" DataField="UserId" />
<asp:BoundField HeaderText="UserName" DataField="UserName" />
<asp:BoundField HeaderText="Education" DataField="Education" />
<asp:BoundField HeaderText="Location" DataField="Location" />
</Columns>
<HeaderStyle BackColor="#df5015" Font-Bold="true" ForeColor="White" />
</asp:GridView>
</div>
</form>
</body>
</html>
|
Now
add following namespaces in codebehind
C# Code
using System;
using System.Data;
using System.Web.UI.WebControls;
|
After
that add following code in code behind
|
VB.NET Code
Imports System.Data
Imports System.Web.UI.WebControls
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()
BindSecondGrid()
End If
End Sub
Protected Sub
BindGridview()
Dim dt As New DataTable()
dt.Columns.Add("UserId",
GetType(Int32))
dt.Columns.Add("UserName",
GetType(String))
dt.Columns.Add("Education",
GetType(String))
dt.Columns.Add("Location",
GetType(String))
Dim dtrow As DataRow = dt.NewRow()
' Create New Row
dtrow("UserId")
= 1
'Bind Data to Columns
dtrow("UserName")
= "SureshDasari"
dtrow("Education")
= "B.Tech"
dtrow("Location")
= "Chennai"
dt.Rows.Add(dtrow)
dtrow = dt.NewRow()
' Create New Row
dtrow("UserId")
= 2
'Bind Data to Columns
dtrow("UserName")
= "MadhavSai"
dtrow("Education")
= "MBA"
dtrow("Location")
= "Nagpur"
dt.Rows.Add(dtrow)
dtrow = dt.NewRow()
' Create New Row
dtrow("UserId")
= 3
'Bind Data to Columns
dtrow("UserName")
= "MaheshDasari"
dtrow("Education")
= "B.Tech"
dtrow("Location")
= "Nuzividu"
dt.Rows.Add(dtrow)
gvDetails.DataSource = dt
gvDetails.DataBind()
End Sub
Protected Sub
chkSelect_CheckChanged(ByVal sender As Object, ByVal e As
EventArgs)
GetSelectedRows()
BindSecondGrid()
End Sub
Protected Sub
BindSecondGrid()
Dim dt As DataTable = DirectCast(ViewState("GetRecords"),
DataTable)
gvTranferRows.DataSource = dt
gvTranferRows.DataBind()
End Sub
Private Sub
GetSelectedRows()
Dim dt As DataTable
If ViewState("GetRecords")
IsNot Nothing
Then
dt = DirectCast(ViewState("GetRecords"), DataTable)
Else
dt = CreateTable()
End If
For i As Integer = 0 To
gvDetails.Rows.Count - 1
Dim chk As CheckBox = DirectCast(gvDetails.Rows(i).Cells(0).FindControl("chkSelect"), CheckBox)
If chk.Checked Then
dt = AddGridRow(gvDetails.Rows(i), dt)
Else
dt = RemoveRow(gvDetails.Rows(i), dt)
End If
Next
ViewState("GetRecords")
= dt
End Sub
Private Function
CreateTable() As DataTable
Dim dt As New DataTable()
dt.Columns.Add("UserId")
dt.Columns.Add("UserName")
dt.Columns.Add("Education")
dt.Columns.Add("Location")
dt.AcceptChanges()
Return dt
End Function
Private Function
AddGridRow(ByVal gvRow As GridViewRow, ByVal
dt As DataTable) As
DataTable
Dim dr As DataRow() =
dt.[Select]("UserId = '" &
gvRow.Cells(1).Text & "'")
If dr.Length <= 0 Then
dt.Rows.Add()
Dim rowscount As Integer = dt.Rows.Count - 1
dt.Rows(rowscount)("UserId")
= gvRow.Cells(1).Text
dt.Rows(rowscount)("UserName")
= gvRow.Cells(2).Text
dt.Rows(rowscount)("Education")
= gvRow.Cells(3).Text
dt.Rows(rowscount)("Location")
= gvRow.Cells(4).Text
dt.AcceptChanges()
End If
Return dt
End Function
Private Function
RemoveRow(ByVal gvRow As GridViewRow, ByVal dt As DataTable) As
DataTable
Dim dr As DataRow() =
dt.[Select]("UserId = '" &
gvRow.Cells(1).Text & "'")
If dr.Length > 0 Then
dt.Rows.Remove(dr(0))
dt.AcceptChanges()
End If
Return dt
End Function
End Class
|
|
Demo
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. |
|||
|
|||
19 comments :
Very nice ...
Mr suresh.. What are you trying to show me.. What us gvChildGrid.
Please use names properly.. plzzzzzzzzzz
very nice article
I don't think so your code will show the output you are showing in the Demo.gif
protected void gvUserInfo_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
con.Open();
GridView gv = (GridView)e.Row.FindControl("gvChildGrid");
int CountryId = Convert.ToInt32(e.Row.Cells[1].Text);
SqlCommand cmd = new SqlCommand("select * from State where CountryID=" + CountryId, con);
SqlDataAdapter da = new SqlDataAdapter(cmd);
DataSet ds = new DataSet();
da.Fill(ds);
con.Close();
gv.DataSource = ds;
gv.DataBind();
}
}
very nice article
The code you have provided in C# is totally wrong. Don't confuse learners plzz
i have of your certify.
good logic in data collection
hi,
how to add a Header Panel in GridView Asp.Net
Ex:http://examples1.ext.net/#/GridPanel/ArrayGrid/Simple/
Please can any one Help in Developing this type of Grid View
@Comment 2, 4 and 6..
Sorry guys mistakenly i written code. Now I updated the code. Please check it once.
test
very nic article
i tried this it's not working for me
Hello,
I created a page with som textboxes and gridview with checkboxes. what ever the data i am selected in the gridview that data along with the textbox fields should be converted to pdf. Can you help me regarding this.
Nice Artice...
display only serialno in gridview just any one serialno click after serialno datas display in second gridview from sqlserver database
display only serialno in gridview just any one serialno click after serialno datas display in second gridview from sqlserver database using asp.net vblanguage
How will I insert the selected data from the second gridview to database?
hi sir I have used your codings for my projects it is very usefull to me.........
please upload the codings for copy selected rows from datalist to gridview using the checkbox control in c# asp.net
is not working on same client id record
Note: Only a member of this blog may post a comment.