Transfer (Pass) Merged and checked (selected) GridView Rows to another GridView in ASP.Net

Last Reply 2 months ago By pandeyism

Posted 2 months ago

I have a gridview that has some rows in it.and some of gridview's row was merged with colspan. now I want to transfer the selected row of my first gridview to the second gridview.

for example, if 3rows merged I want to transfer that 3 rows, to the second gridview. 

          <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" OnDataBound="OnDataBound" >
              <Columns>
                 <asp:BoundField DataField="TRoom" HeaderText="نام اتاق" HeaderStyle-CssClass="header-center" />
                        <asp:BoundField DataField="DayWeek" HeaderText="روز هفته" HeaderStyle-CssClass="header-center" />
                        <asp:BoundField DataField="ET" HeaderText="ساعت پایان کلاس" HeaderStyle-CssClass="header-center" />
                        <asp:BoundField DataField="ST" HeaderText="ساعت شروع کلاس" HeaderStyle-CssClass="header-center" />
                        <asp:BoundField DataField="LNProfessor" HeaderText="نام خانوادگی استاد" HeaderStyle-CssClass="header-center" />
                        <asp:BoundField DataField="NProfessor" HeaderText="نام استاد" HeaderStyle-CssClass="header-center" />
                        <asp:BoundField DataField="Code" HeaderText="کد درس" HeaderStyle-CssClass="header-center" />
                        <asp:BoundField DataField="TLesson" HeaderText="نام درس" HeaderStyle-CssClass="header-center" />
                        <asp:BoundField DataField="CourseId" HeaderText="آی دی" HeaderStyle-CssClass="header-center" />
                    </Columns>
                    <HeaderStyle BackColor="#3AC0F2" ForeColor="White"></HeaderStyle>
                </asp:GridView>

the code that merges rows with colspan: 

        protected void GridView1_DataBound(object sender, EventArgs e)
        {
            for (int i = GridView1.Rows.Count - 1; i > 0; i--)
            {
                GridViewRow row = GridView1.Rows[i];
                GridViewRow previousRow = GridView1.Rows[i - 1];
                CheckBox chk = row.FindControl("chkSelect") as CheckBox;
                int count = row.Cells.Count - 1;
                for (int j = 8; j >5; j--)
                {
                    if (row.Cells[j].Text == previousRow.Cells[j].Text)
                    {
                        if (previousRow.Cells[j].RowSpan == 0)
                        {
                            if (row.Cells[j].RowSpan == 0)
                            {
                                previousRow.Cells[j].RowSpan += 2;
                                previousRow.Cells[count].RowSpan = 2;
                            }
                            else
                            {
                                previousRow.Cells[j].RowSpan = row.Cells[j].RowSpan + 1;
                                previousRow.Cells[count].RowSpan = row.Cells[j].RowSpan + 1;
                            }
                            chk.Visible = false;
                            row.Cells[j + 1].Visible = false;
                            row.Cells[j].Visible = false;
                        }
                    }
                }
            }

the code for getting selected record to transfer:

        protected void GetSelectedRecords(object sender, EventArgs e)
        {
            DataTable dt1 = new DataTable();
            int s = 0;
            for (int i = GridView1.Rows.Count - 1; i > 0; i--)
            {
                GridViewRow row = GridView1.Rows[i];
                GridViewRow previousRow = GridView1.Rows[i - 1];
                CheckBox chk = row.FindControl("chkSelect") as CheckBox;
                int count = row.Cells.Count - 1;
                for (int j = 8; j > 5; j--)
                {
                    if (row.Cells[j].Text == previousRow.Cells[j].Text)
                    {
                        s++;
                    }
                }
                foreach (GridViewRow row1 in GridView1.Rows)
                {                   
                        for (int i1 = 0; i1 < s; i1++)
                        {
                            CheckBox chkRow = (row.Cells[i1].FindControl("chkselect") as CheckBox);
                            if (chkRow.Checked)
                            {
                                string TRoom = row.Cells[0].Text;
                                string DayWeek = row.Cells[1].Text;
                                string StartTime = row.Cells[2].Text;
                                string EndTime = row.Cells[3].Text;
                                string Family = row.Cells[4].Text;
                                string Name = row.Cells[5].Text;
                                string LessonCode = row.Cells[6].Text;
                                string LessonName = row.Cells[7].Text;
                                string CourseId = row.Cells[8].Text;
                                dt1.Rows.Add(TRoom, DayWeek, EndTime, StartTime, Family, Name, LessonCode, LessonName, CourseId);
                            }
                        }                   
                }
                gvSelected.DataSource = dt1;
                gvSelected.DataBind();
            }
        }

I tried it in above code but it doesn't work correctly. how can I do that?!

Posted 2 months ago

Hey dorsa,

Please refer below sample.

HTML

<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" OnDataBound="OnDataBound">
    <Columns>
        <asp:BoundField DataField="ContactName" HeaderText="ContactName" ItemStyle-Width="150"
            SortExpression="ContactName">
            <HeaderStyle CssClass="header-center"></HeaderStyle>
            <ItemStyle Width="150px"></ItemStyle>
        </asp:BoundField>
        <asp:BoundField DataField="City" HeaderText="City" ItemStyle-Width="150" SortExpression="City">
            <HeaderStyle CssClass="header-center"></HeaderStyle>
            <ItemStyle Width="150px"></ItemStyle>
        </asp:BoundField>
        <asp:BoundField DataField="Country" HeaderText="Country" ItemStyle-Width="150" SortExpression="Country">
            <HeaderStyle CssClass="header-center"></HeaderStyle>
            <ItemStyle Width="150px"></ItemStyle>
        </asp:BoundField>
        <asp:TemplateField HeaderText="select">
            <ItemTemplate>
                <asp:CheckBox ID="chkSelect" runat="server" AutoPostBack="true" />
            </ItemTemplate>
        </asp:TemplateField>
    </Columns>
    <HeaderStyle BackColor="#3AC0F2" ForeColor="White"></HeaderStyle>
</asp:GridView>
<asp:Button Text="Transfer" runat="server" OnClick="Transfer" />
<br />
<asp:GridView runat="server" ID="GridView2" AutoGenerateColumns="false">
    <Columns>
        <asp:BoundField DataField="ContactName" HeaderText="ContactName" />
        <asp:BoundField DataField="Country" HeaderText="Country" />
        <asp:BoundField DataField="City" HeaderText="City" />
    </Columns>
</asp:GridView>

Namespaces

C#

using System.Data;
using System.Data.SqlClient;
using System.Configuration;

VB.Net

Imports System.Data.SqlClient
Imports System.Data

Code

C#

protected void Page_Load(object sender, EventArgs e)
{
    if (!this.IsPostBack)
    {
        DataTable dt = new DataTable();
        GridView1.DataSource = GetData("SELECT TOP 5 ContactName, Country, City FROM Customers WHERE Country IS NOT NULL GROUP BY Country, City, ContactName");
        GridView1.DataBind();
    }
}

private DataTable GetData(string query)
{
    DataTable dt = new DataTable();
    string constr = ConfigurationManager.ConnectionStrings["constr"].ConnectionString;
    using (SqlConnection con = new SqlConnection(constr))
    {
        using (SqlCommand cmd = new SqlCommand(query))
        {
            using (SqlDataAdapter sda = new SqlDataAdapter())
            {
                cmd.CommandType = CommandType.Text;
                cmd.Connection = con;
                sda.SelectCommand = cmd;
                sda.Fill(dt);
                ViewState["dt"] = dt;
            }
        }
        return dt;
    }
}

protected void OnDataBound(object sender, EventArgs e)
{
    for (int i = GridView1.Rows.Count - 1; i > 0; i--)
    {
        GridViewRow row = GridView1.Rows[i];
        GridViewRow previousRow = GridView1.Rows[i - 1];
        CheckBox chk = row.FindControl("chkSelect") as CheckBox;
        int count = row.Cells.Count - 1;
        for (int j = 0; j < row.Cells.Count - 1; j++)
        {
            if (row.Cells[j].Text == previousRow.Cells[j].Text)
            {
                if (previousRow.Cells[j].RowSpan == 0)
                {
                    if (row.Cells[j].RowSpan == 0)
                    {
                        previousRow.Cells[j].RowSpan += 2;
                        previousRow.Cells[count].RowSpan = 2;
                    }
                    else
                    {
                        previousRow.Cells[j].RowSpan = row.Cells[j].RowSpan + 1;
                        previousRow.Cells[count].RowSpan = row.Cells[j].RowSpan + 1;
                    }
                    chk.Visible = false;
                    row.Cells[j].Visible = false;
                    row.Cells[j + 1].Visible = false;
                }
            }
        }
    }
}

protected void Transfer(object sender, EventArgs e)
{
    DataTable dt = ViewState["dt"] as DataTable;
    DataTable dtChecked = dt.Clone();
    foreach (GridViewRow row in GridView1.Rows)
    {
        CheckBox chk = row.FindControl("chkSelect") as CheckBox;
        if (chk.Checked)
        {
            DataRow[] dr = dt.Select("Country='" + row.Cells[2].Text + "'");
            dtChecked.Merge(dr.CopyToDataTable());
        }
    }
    GridView2.DataSource = dtChecked;
    GridView2.DataBind();
}

VB.Net

Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs) Handles Me.Load
    If Not Me.IsPostBack Then
        Dim dt As DataTable = New DataTable()
        GridView1.DataSource = GetData("SELECT TOP 5 ContactName, Country, City FROM Customers WHERE Country IS NOT NULL GROUP BY Country, City, ContactName")
        GridView1.DataBind()
    End If
End Sub

Private Function GetData(ByVal query As String) As DataTable
    Dim dt As DataTable = New DataTable()
    Dim constr As String = ConfigurationManager.ConnectionStrings("constr").ConnectionString
    Using con As SqlConnection = New SqlConnection(constr)
        Using cmd As SqlCommand = New SqlCommand(query)
            Using sda As SqlDataAdapter = New SqlDataAdapter()
                cmd.CommandType = CommandType.Text
                cmd.Connection = con
                sda.SelectCommand = cmd
                sda.Fill(dt)
                ViewState("dt") = dt
            End Using
        End Using

        Return dt
    End Using
End Function

Protected Sub OnDataBound(sender As Object, e As EventArgs)
    For i As Integer = GridView1.Rows.Count - 1 To 1 Step -1
        Dim row As GridViewRow = GridView1.Rows(i)
        Dim chk As CheckBox = TryCast(row.FindControl("chkSelect"), CheckBox)
        Dim count As Integer = row.Cells.Count - 1
        Dim previousRow As GridViewRow = GridView1.Rows(i - 1)
        For j As Integer = 0 To row.Cells.Count - 2
            If row.Cells(j).Text = previousRow.Cells(j).Text Then
                If previousRow.Cells(j).RowSpan = 0 Then
                    If row.Cells(j).RowSpan = 0 Then
                        previousRow.Cells(j).RowSpan += 2
                        previousRow.Cells(count).RowSpan = 2
                    Else
                        previousRow.Cells(j).RowSpan = row.Cells(j).RowSpan + 1
                        previousRow.Cells(count).RowSpan = row.Cells(count).RowSpan + 1
                    End If
                    chk.Visible = False
                    row.Cells(j).Visible = False
                    row.Cells(count).Visible = False
                End If
            End If
        Next
    Next
End Sub

Protected Sub Transfer(ByVal sender As Object, ByVal e As EventArgs)
    Dim dt As DataTable = TryCast(ViewState("dt"), DataTable)
    Dim dtChecked As DataTable = dt.Clone()

    For Each row As GridViewRow In GridView1.Rows
        Dim chk As CheckBox = TryCast(row.FindControl("chkSelect"), CheckBox)

        If chk.Checked Then
            Dim dr As DataRow() = dt.Select("Country='" & row.Cells(2).Text & "'")
            dtChecked.Merge(dr.CopyToDataTable())
        End If
    Next

    GridView2.DataSource = dtChecked
    GridView2.DataBind()
End Sub

Screenshot