top button
Flag Notify
    Connect to us
      Site Registration

Site Registration

How to transfer selected rows from one GridView to another GridView in ASP.NET?

0 votes
1,947 views
How to transfer selected rows from one GridView to another GridView in ASP.NET?
posted Feb 15, 2016 by Jayshree

Share this question
Facebook Share Button Twitter Share Button LinkedIn Share Button

1 Answer

+1 vote
 
Best answer

Multiple selected rows from a GridView control can be transferred to another GridView control very easily.

There can be different ways to transfer multiple selected records of a GridView to another GridView. One way can be to create a copy of DataTable or DataSet which you are binding to first GridView. When you need to bind selected records to second GridView then you can get the ID of the record and bind those selected records to GridView from the copy of DataTable.

The other way is to create a new DataTable and add same number of columns to this DataTable and then add each selected row to DataTable for every column. I am going to use second way in this article to transfer selected rows from one GridView control to another GridView control.
Let’s see how we can do it. You can download source code.

1.Open Visual Studio 2010
2.File > New > Web Site
3.Visual C# or Visual Basic > ASP.NET Empty Web Site > Click Ok
4.Website > Add New Item > Web Form > Click Add
5.Now Drag and Drop two GridView, SqlDataSource and Button in Default aspx page.
6.Populate this GridView with data you want. I have populated it with “Products” table of “NORTHWIND” database.
7.The code in Default.aspx page will be like below.

<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False"
    DataKeyNames="ProductID" DataSourceID="SqlDataSource1">
    <Columns>
        <asp:TemplateField HeaderText="Select">
            <ItemTemplate>
                <asp:CheckBox ID="CheckBox1" runat="server" />
            </ItemTemplate>
         </asp:TemplateField>

        <asp:BoundField DataField="ProductID" HeaderText="ProductID"
             InsertVisible="False" ReadOnly="True" SortExpression="ProductID" />
        <asp:BoundField DataField="ProductName" HeaderText="ProductName"
             SortExpression="ProductName" />
        <asp:BoundField DataField="CategoryID" HeaderText="CategoryID"
             SortExpression="CategoryID" />
        <asp:BoundField DataField="UnitPrice" HeaderText="UnitPrice"
             SortExpression="UnitPrice" />
    </Columns>
</asp:GridView>
<asp:SqlDataSource ID="SqlDataSource1" runat="server"
    ConnectionString="<%$ ConnectionStrings:C:\SQL SERVER 2000 SAMPLE DATABASES\NORTHWND.MDFConnectionString %>"
    SelectCommand="SELECT [ProductID], [ProductName], [CategoryID], [UnitPrice] FROM [Products] WHERE ([UnitPrice] &lt;= @UnitPrice)">
    <SelectParameters>
        <asp:Parameter DefaultValue="10" Name="UnitPrice" Type="Decimal" />
     </SelectParameters>
</asp:SqlDataSource>
<br />
<asp:Button ID="Button1" runat="server" Text="Show selected product below"
    onclick="Button1_Click" />
<br />
<br />
<asp:GridView ID="GridView2" runat="server">
</asp:GridView>

8.Include System.Data namespace in your code file for DataTable and DataRow classes.
9.Write code below in button click event

C#

protected void Button1_Click(object sender, EventArgs e)
{
    DataTable dt = new DataTable();

    dt.Columns.Add("ProductID", typeof(int));
    dt.Columns.Add("ProductName", typeof(string));
    dt.Columns.Add("CategoryID", typeof(int));
    dt.Columns.Add("UnitPrice", typeof(decimal));

    foreach (GridViewRow gvRow in GridView1.Rows)
    {
        CheckBox checkbox = (CheckBox)gvRow.Cells[0].FindControl("CheckBox1");
        if (checkbox.Checked)
        {
            DataRow row = dt.NewRow();
            row["ProductID"] = gvRow.Cells[1].Text;
            row["ProductName"] = gvRow.Cells[2].Text;
            row["CategoryID"] = gvRow.Cells[3].Text;
            row["UnitPrice"] = gvRow.Cells[4].Text;
            dt.Rows.Add(row);
        }
    }
    GridView2.DataSource = dt;
    GridView2.DataBind();
}

VB.NET

Protected Sub Button1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button1.Click
    Dim dt As New DataTable()

    dt.Columns.Add("ProductID", GetType(Integer))
    dt.Columns.Add("ProductName", GetType(String))
    dt.Columns.Add("CategoryID", GetType(Integer))
    dt.Columns.Add("UnitPrice", GetType(Decimal))

    For Each gvRow As GridViewRow In GridView1.Rows
        Dim checkbox As CheckBox = DirectCast(gvRow.Cells(0).FindControl("CheckBox1"), CheckBox)
        If checkbox.Checked Then
            Dim row As DataRow = dt.NewRow()
            row("ProductID") = gvRow.Cells(1).Text
            row("ProductName") = gvRow.Cells(2).Text
            row("CategoryID") = gvRow.Cells(3).Text
            row("UnitPrice") = gvRow.Cells(4).Text
            dt.Rows.Add(row)
        End If
    Next
    GridView2.DataSource = dt
    GridView2.DataBind()
End Sub

I have created a DataTable to add and also added four columns to DataTable. I have used foreach loop to iterate through GridView. I have used FindControl() method to find CheckBox control. When CheckBox is checked, I have created a DataRow, added GridView cell values of this row and added the row to DataTable. This foreach loop will continue for every selected row. I have bound this DataTable of selected records to second GridView control.

10.Press F5 and see website in browser.

answer Feb 15, 2016 by Shivaranjini
Nice sample simple and easy A+++
...