.NET Tutorials, Forums, Interview Questions And Answers
Welcome :Guest
Sign In
Win Surprise Gifts!!!

Top 5 Contributors of the Month
Melody Anderson

Home >> Code Snippets >> ASP.NET Controls >> Post New Resource Bookmark and Share   

 Subscribe to Code Snippets

Merge Cells Columns in Row of GridView

Posted By:satyapriyanayak       Posted Date: May 02, 2013    Points: 40    Category: ASP.NET Controls    URL: http://www.dotnetspark.com  

Here we will be able to merge gridview cells ,columns in gridview rows. Here in DataBound Event of the gridview control we count total number of rows and then checking each cells value against value of same cell in previous row and then setting the RowSpan of cells.

Default.aspx code

<%@ Page Language="VB" AutoEventWireup="false" CodeFile="Default.aspx.vb" Inherits="_Default" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">

<head runat="server">
    <title>Untitled Page</title>

    <form id="form1" runat="server">
    <asp:GridView ID="GridView1" runat="server"
    BorderStyle="None" BorderWidth="1px" CellPadding="4" ForeColor="Black"
    OnDataBound="GridView1_DataBound1" BackColor="White">
            <asp:BoundField DataField="Country"
                            SortExpression="Country" />
            <asp:BoundField DataField="State"
                            SortExpression="State" />
            <asp:BoundField DataField="City"
                            SortExpression="City" />
            <HeaderStyle BackColor="#FF8040" />
            <EditRowStyle BackColor="#FFCC00" />
            <AlternatingRowStyle BackColor="White" ForeColor="Black" />

Default.aspx.vb code

Imports System.Data
Imports System.Data.SqlClient
Partial Class _Default
    Inherits System.Web.UI.Page

    Dim strConnString As String = System.Configuration.ConfigurationManager.ConnectionStrings.Item("ConnectionString").ToString()
    Dim con As New SqlConnection(strConnString)
    Dim str As String
    Dim com As SqlCommand
    Dim sqlda As SqlDataAdapter
    Dim ds As DataSet

Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load

        str = "select country,state,city from location order by state"
        com = New SqlCommand(str, con)
        Dim reader As SqlDataReader
        reader = com.ExecuteReader()
        GridView1.DataSource = reader
    End Sub

End Class

    Protected Sub GridView1_DataBound1(ByVal sender As Object, ByVal e As System.EventArgs) Handles GridView1.DataBound

        For rowIndex As Integer = GridView1.Rows.Count - 2 To 0 Step -1
            Dim gviewRow As GridViewRow = GridView1.Rows(rowIndex)
            Dim gviewPreviousRow As GridViewRow = GridView1.Rows(rowIndex + 1)
            For cellCount As Integer = 0 To gviewRow.Cells.Count - 1
                If gviewRow.Cells(cellCount).Text = gviewPreviousRow.Cells(cellCount).Text Then
                    If gviewPreviousRow.Cells(cellCount).RowSpan < 2 Then
                        gviewRow.Cells(cellCount).RowSpan = 2
                        gviewRow.Cells(cellCount).RowSpan = gviewPreviousRow.Cells(cellCount).RowSpan + 1
                    End If
                    gviewPreviousRow.Cells(cellCount).Visible = False
                End If

    End Sub


Further Readings:


No response found. Be the first to respond this post

Post Comment

You must Sign In To post reply
Find More code samples in C#, ASP.Net, Vb.Net and more Here

Hall of Fame    Twitter   Terms of Service    Privacy Policy    Contact Us    Archives   Tell A Friend