Merge ASP.Net GridView Cells or Columns in Row with CheckBox using C# and VB.Net

Last Reply 6 months ago By pandeyism

Posted 6 months ago

I have a grid view that showing the data from a view in sql server.

now I want to show the data of grid view like below pic in my asp.net web form:

http://uupload.ir/files/x28q_22.png

I want to display the record with the same Course Id in a row with one checkbox.

but I get this result and just the first row has a checkbox.

http://uupload.ir/files/f5ko_333.png

how can I do this?

Posted 6 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>

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  ContactName, Country, City FROM Customers 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);
            }
        }
        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;
                }
            }
        }
    }
}

VB.Net

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

Private Function GetData(query As String) As DataTable
    Dim dt As New DataTable()
    Dim constr As String = ConfigurationManager.ConnectionStrings("constr").ConnectionString
    Using con As New SqlConnection(constr)
        Using cmd As New SqlCommand(query)
            Using sda As New SqlDataAdapter()
                cmd.CommandType = CommandType.Text
                cmd.Connection = con
                sda.SelectCommand = cmd
                sda.Fill(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

Screenshot