Thursday, 24 May 2012

Merge GridView Header column in ASP.NET


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

<%@ Register Assembly="Obout.Ajax.UI"Namespace="Obout.Ajax.UI.TreeView"TagPrefix="obout"%>
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> gridview merge column headers title>
    <script type="text/javascript">
     functionFilter(Obj)
     {
        vargrid=document.getElementById('GridView');
        varterms = Obj.value;
        varcellNr = "0";//grid colunm cellindex in which you want to search
        varele;
        for (var r = 1; r < grid.rows.length; r++)
        {
          ele = grid.rows[r].cells[cellNr].innerHTML.replace(/<[^>]+>/g, "");
            if(ele.toLowerCase().indexOf(terms) >= 0)
                grid.rows[r].style.display = '';
            elsegrid.rows[r].style.display = 'none';
        }
     };   
  
    script>

head>
<body>
    <form id="form1" runat="server">   
<asp:GridView ID="gridView"runat="server" OnRowCreated="gridView_RowCreated">asp:GridView>   
    form>
body>
html>


using System;
usingSystem.Collections;
usingSystem.Configuration;
usingSystem.Data;
usingSystem.Linq;
usingSystem.Web;
usingSystem.Web.Security;
usingSystem.Web.UI;
usingSystem.Web.UI.HtmlControls;
usingSystem.Web.UI.WebControls;
usingSystem.Web.UI.WebControls.WebParts;
usingSystem.Xml.Linq;
usingObout.Ajax.UI.TreeView;
usingSystem.Data.SqlClient;
usingSystem.Net.Mail;
using System.IO;
usingSystem.Collections.Generic;


public partial class TestwebPage : System.Web.UI.Page
{
    public enum AnalysisType{ sss, ssss };
    protected void Page_Load(objectsender, EventArgs e)
    {
        if(!IsPostBack)
        {
            BindGridView();
        }
    }

    protected void gridView_PreRender(objectsender, EventArgs e)
    {
        MergeRows(gridView);
    } 

    voidBindGridView()
    {
        DataSetdataSet = new DataSet();
        gridView.DataSource = dataSet;
        gridView.DataBind();
    }

   protected void gridView_ RowCreated(objectsender, GridViewRowEventArgs e)
    {
        if(e.Row.RowType == DataControlRowType.Header)
        {
            //custom header.
GridView gvHeader = (GridView)sender;
GridViewRow gvHeaderRow = new GridViewRow(0, 0,    DataControlRowType.Header,DataControlRowState.Insert);
TableCell tcell = newTableCell();

// First column
tcell.Text = "First Column1";
tcell.ColumnSpan = 2;
tcell.BackColor = System.Drawing.Color.DarkGray;
gvHeaderRow.Cells.Add(tcell);

// Second  column
tcell = new TableCell();
tcell.Text = "Second Column2";
tcell.ColumnSpan = 2;
gvHeaderRow.Cells.Add(tcell);

gvHeader.Controls[0].Controls.AddAt(0, gvHeaderRow);

        }
    }}




0 comments:

Post a Comment

Twitter Delicious Facebook Digg Stumbleupon Favorites More

 
Powered by Blogger