Wednesday, 16 September 2015

how to merge cells in gridview in asp.net using C#


merge cell or coloumn in gridview



Description:

In this example we explain that how to merge cells in gridview in asp.net. Or Merge Cells Columns in Row of GridView. Merge GridView Cells or Columns in a Row in ASP.Net using C#.

Here we use ondatabound event of the gridview to merge the common cells in one cell in gridview in 
asp.net. Here we counting total rows in gridview and then checking each cells value against value of same cell in previous row and then setting the RowSpan of cells.

Many time there is a situation when gridview contain same values for a column in multiple rows.so this repeated items are displayed in one time and merge cells.

Here we demonstrate merge cells in gridview that has same value in 
asp.net or how to merge all rows in a coloumn in a gridview in asp.net



mergecell.aspx:-

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="mergecell.aspx.cs" Inherits="mergecell" %>

<!DOCTYPE html>

<html xmlns="http://www.w3.org/1999/xhtml">
<head id="Head1" runat="server">
    <title>Merge Cells Columns in Row of GridView in Asp.Net using C#</title>
</head>
<body>
    <form id="form1" runat="server">
        <div>
            <asp:GridView ID="grdCountry" runat="server"
                AutoGenerateColumns="False"
                BorderStyle="None" BorderWidth="1px" CellPadding="5" ForeColor="Black"
                Height="119px"
                BackColor="White">
                <Columns>

                    <asp:BoundField DataField="Country"
                        HeaderText="Country"
                        SortExpression="Country" />

                    <asp:BoundField DataField="State"
                        HeaderText="State"
                        SortExpression="State" />

                    <asp:BoundField DataField="City"
                        HeaderText="City"
                        SortExpression="City" />
                </Columns>
                <HeaderStyle BackColor="#FF2080" />
                <EditRowStyle BackColor="#FFCC55" />
                <AlternatingRowStyle BackColor="White" ForeColor="Black" />
            </asp:GridView>
        </div>
    </form>
</body>
</html>


mergecell.aspx.cs:-

using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class mergecell : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
            BindGrid();
    }

    private void BindGrid()
    {
        DataTable dtCountry = new DataTable();
        dtCountry.Columns.Add("Country");
        dtCountry.Columns.Add("State");
        dtCountry.Columns.Add("City");
        DataRow dr1 = dtCountry.NewRow();
        dr1[0] = "India";
        dr1[1] = "Gujarat";
        dr1[2] = "Rajkot";
        dtCountry.Rows.Add(dr1);
        DataRow dr2 = dtCountry.NewRow();
        dr2[0] = "India";
        dr2[1] = "Gujarat";
        dr2[2] = "Ahmedabad";
        dtCountry.Rows.Add(dr2);
        DataRow dr3 = dtCountry.NewRow();
        dr3[0] = "India";
        dr3[1] = "Gujarat";
        dr3[2] = "Surat";
        dtCountry.Rows.Add(dr3);
        DataRow dr4 = dtCountry.NewRow();
        dr4[0] = "Pakistan";
        dr4[1] = "Lahore";
        dr4[2] = "Punjab";
        dtCountry.Rows.Add(dr4);
        DataRow dr5 = dtCountry.NewRow();
        dr5[0] = "Pakistan";
        dr5[1] = "Lahore";
        dr5[2] = "Rawalpindi";
        dtCountry.Rows.Add(dr5);
        DataRow dr6 = dtCountry.NewRow();
        dr6[0] = "USA";
        dr6[1] = "California";
        dr6[2] = "Sendigo";
        dtCountry.Rows.Add(dr6);
        grdCountry.DataSource = dtCountry;
        grdCountry.DataBind();
        MergeCells();
    }
    private void MergeCells()
    {
        int i = grdCountry.Rows.Count - 2;
        while (i >= 0)
        {
            GridViewRow curRow = grdCountry.Rows[i];
            GridViewRow preRow = grdCountry.Rows[i + 1];

            int j = 0;
            while (j < curRow.Cells.Count)
            {
                if (curRow.Cells[j].Text == preRow.Cells[j].Text)
                {
                    if (preRow.Cells[j].RowSpan < 2)
                    {
                        curRow.Cells[j].RowSpan = 2;
                        preRow.Cells[j].Visible = false;
                    }
                    else
                    {
                        curRow.Cells[j].RowSpan = preRow.Cells[j].RowSpan + 1;
                        preRow.Cells[j].Visible = false;
                    }
                }
                j++;
            }
            i--;
        }
    }
}




0 comments:

Post a Comment