RSS

Category Archives: Uncategorized

Merge GridView Cells Or Columns in Row ASP.NET C# VB.NET


In this example i am going to describe how to merge GridView cells or Columns in gridview rows containing same data or content using C# and VB.NET in ASP.NET

For this i m using DataBound Event of gridview, counting total rows and then checking each cells value against value of same cell in previous row and then setting the RowSpan of cells.

For this i have created a table containing Counties ,states and respective cities and country and state cells / columns are merged in rows having same country or states.

Html source of the page look like this

<asp:GridView ID="GridView1" runat="server"
    AutoGenerateColumns="False"
    BorderStyle="None" BorderWidth="1px" CellPadding="4"
    GridLines="Horizontal" ForeColor="Black"
    Height="119px" DataSourceID="SqlDataSource1"
    OnDataBound="GridView1_DataBound1">
            <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>
</asp:GridView>
<asp:SqlDataSource ID="SqlDataSource1" runat="server"
ConnectionString="<%$ ConnectionStrings:ConnectionString %>"
SelectCommand="SELECT [Country], [State], [City]
               FROM [Details] ORDER BY [State]">
</asp:SqlDataSource>
C# code behind
protected void GridView1_DataBound1(object sender, EventArgs e)
{
  for (int rowIndex = GridView1.Rows.Count - 2;
                                     rowIndex >= 0; rowIndex--)
  {
    GridViewRow gvRow = GridView1.Rows[rowIndex];
    GridViewRow gvPreviousRow = GridView1.Rows[rowIndex + 1];
    for (int cellCount = 0; cellCount < gvRow.Cells.Count;
                                                  cellCount++)
    {
     if (gvRow.Cells[cellCount].Text ==
                            gvPreviousRow.Cells[cellCount].Text)
     {
       if (gvPreviousRow.Cells[cellCount].RowSpan < 2)
       {
         gvRow.Cells[cellCount].RowSpan = 2;
       }
       else
       {
        gvRow.Cells[cellCount].RowSpan =
            gvPreviousRow.Cells[cellCount].RowSpan + 1;
       }
       gvPreviousRow.Cells[cellCount].Visible = false;
    }
   }
 }
}

VB.NET code behind 
Protected Sub GridView1_DataBound1
           (ByVal sender As Object, ByVal e As EventArgs)

For rowIndex As Integer = GridView1.Rows.Count - 2 To 0 Step -1
    Dim gvRow As GridViewRow = GridView1.Rows(rowIndex)
    Dim gvPreviousRow As GridViewRow = GridView1.Rows(rowIndex + 1)
    For cellCount As Integer = 0 To gvRow.Cells.Count - 1
    If gvRow.Cells(cellCount).Text =
                         gvPreviousRow.Cells(cellCount).Text Then
    If gvPreviousRow.Cells(cellCount).RowSpan < 2 Then
    gvRow.Cells(cellCount).RowSpan = 2
    Else
    gvRow.Cells(cellCount).RowSpan =
                       gvPreviousRow.Cells(cellCount).RowSpan + 1
    End If
    gvPreviousRow.Cells(cellCount).Visible = False
    End If
    Next
  Next
End Sub
 
Leave a comment

Posted by on June 26, 2010 in Uncategorized

 

Custom Paging of GridView,DataListView and Repeater Control of Asp.net With C#


Custom Paging Img

Custom Paging of GridView or DataListview or Repeater

Code-Behind Code

//Page Load Event

protected void Page_Load(object sender, EventArgs e)

{

if (!IsPostBack)

{

BindMngRegistration();

}

}

//Load or Bind the Data to Dataset with gridview or Datalist

public void BindMngRegistration()

{

DataSet ds = BLLRegisrationOnline.ViewManageRegistration();

_PageDataSource.DataSource = ds.Tables[0].DefaultView;

_PageDataSource.AllowPaging = true;

_PageDataSource.PageSize = Convert.ToInt32(GVRegView.PageSize);

_PageDataSource.CurrentPageIndex = CurrentPage;

ViewState[“TotalPages”] = _PageDataSource.PageCount;

this.lblPageInfo.Text = “Page ” + (CurrentPage + 1) + ” of ” + _PageDataSource.PageCount;

this.lbtnPrevious.Enabled = !_PageDataSource.IsFirstPage;

this.lbtnNext.Enabled = !_PageDataSource.IsLastPage;

this.lbtnFirst.Enabled = !_PageDataSource.IsFirstPage;

this.lbtnLast.Enabled = !_PageDataSource.IsLastPage;

//Bind With Control

this.GVRegView.DataSource = _PageDataSource;

this.GVRegView.DataBind();

this.doPaging();

}

//Write This Code For the Number of Record to display at Lable

protected void dlPaging_ItemDataBound(object sender, DataListItemEventArgs e)

{

if (e.Item.ItemType != ListItemType.Separator)

{

LinkButton lnkbtnPage = (LinkButton)e.Item.FindControl(“lnkbtnPaging”);

if (lnkbtnPage.CommandArgument.ToString() == CurrentPage.ToString())

{

lnkbtnPage.Enabled = false;

lnkbtnPage.Style.Add(“fone-size”, “14px”);

lnkbtnPage.Font.Bold = true;

}

}

}

//display the current page of the number of page like (5 of 15)

protected void dlPaging_ItemCommand(object source, DataListCommandEventArgs e)

{

if (e.CommandName.Equals(“Paging”))

{

CurrentPage = Convert.ToInt16(e.CommandArgument.ToString());

this.BindMngRegistration();

}

}

// Page Datasource Object for pagging

#region PagedDataSource

PagedDataSource _PageDataSource = new PagedDataSource();

#endregion

//Properties of Paging for next,first ,last,previous

#region Private Properties

private int CurrentPage

{

get

{

object objPage = ViewState[“_CurrentPage”];

int _CurrentPage = 0;

if (objPage == null)

{

_CurrentPage = 0;

}

else

{

_CurrentPage = (int)objPage;

}

return _CurrentPage;

}

set { ViewState[“_CurrentPage”] = value; }

}

private int fistIndex

{

get

{

int _FirstIndex = 0;

if (ViewState[“_FirstIndex”] == null)

{

_FirstIndex = 0;

}

else

{

_FirstIndex = Convert.ToInt32(ViewState[“_FirstIndex”]);

}

return _FirstIndex;

}

set { ViewState[“_FirstIndex”] = value; }

}

private int lastIndex

{

get

{

int _LastIndex = 0;

if (ViewState[“_LastIndex”] == null)

{

_LastIndex = 0;

}

else

{

_LastIndex = Convert.ToInt32(ViewState[“_LastIndex”]);

}

return _LastIndex;

}

set { ViewState[“_LastIndex”] = value; }

}

#endregion

//Events of Paging

#region “Custom Paging”

protected void lbtnFirst_Click(object sender, EventArgs e)

{

CurrentPage = 0;

this.BindMngRegistration();

}

protected void lbtnPrevious_Click(object sender, EventArgs e)

{

CurrentPage -= 1;

this.BindMngRegistration();

}

protected void lbtnNext_Click(object sender, EventArgs e)

{

CurrentPage += 1;

this.BindMngRegistration();

}

protected void lbtnLast_Click(object sender, EventArgs e)

{

CurrentPage = (Convert.ToInt32(ViewState[“TotalPages”]) – 1);

this.BindMngRegistration();

}

#endregion

//total Pages

private void doPaging()

{

DataTable dt = new DataTable();

dt.Columns.Add(“PageIndex”);

dt.Columns.Add(“PageText”);

fistIndex = CurrentPage – 5;

if (CurrentPage > 5)

{

lastIndex = CurrentPage + 5;

}

else

{

lastIndex = 10;

}

if (lastIndex > Convert.ToInt32(ViewState[“TotalPages”]))

{

lastIndex = Convert.ToInt32(ViewState[“TotalPages”]);

fistIndex = lastIndex – 10;

}

if (fistIndex < 0)

{

fistIndex = 0;

}

for (int i = fistIndex; i < lastIndex; i++)

{

DataRow dr = dt.NewRow();

dr[0] = i;

dr[1] = i + 1;

dt.Rows.Add(dr);

}

this.dlPaging.DataSource = dt;

this.dlPaging.DataBind();

}

Client- Side Code

<div style=”float:right;”>

<table cellpadding=”0″ border=”0″>

<tr><td>

<asp:Label ID=”lblPageInfo” runat=”server”></asp:Label>

</td><td>&nbsp;&nbsp;</td>

<td align=”right”>

<asp:LinkButton ID=”lbtnFirst” runat=”server”

CausesValidation=”false” OnClick=”lbtnFirst_Click” CssClass=”paging”>First</asp:LinkButton>

&nbsp;</td>

<td align=”right”>

<asp:LinkButton ID=”lbtnPrevious” CssClass=”paging” runat=”server”

CausesValidation=”false” OnClick=”lbtnPrevious_Click”>Previous</asp:LinkButton>&nbsp;&nbsp;</td>

<td align=”center” valign=”middle”>

<asp:DataList ID=”dlPaging” runat=”server” RepeatDirection=”Horizontal” OnItemCommand=”dlPaging_ItemCommand”

OnItemDataBound=”dlPaging_ItemDataBound”>

<ItemTemplate>

<asp:LinkButton CssClass=”paging” ID=”lnkbtnPaging” runat=”server” CommandArgument='<%# Eval(“PageIndex”) %>’

CommandName=”Paging” Text='<%# Eval(“PageText”) %>’></asp:LinkButton>&nbsp;

</ItemTemplate>

<SeparatorTemplate>&nbsp;|&nbsp;</SeparatorTemplate>

</asp:DataList>

</td>

<td align=”left”>

&nbsp;&nbsp;<asp:LinkButton CssClass=”paging” ID=”lbtnNext” runat=”server” CausesValidation=”false”

OnClick=”lbtnNext_Click”>Next</asp:LinkButton></td>

<td align=”left”>

&nbsp;

<asp:LinkButton ID=”lbtnLast” runat=”server” CausesValidation=”false” OnClick=”lbtnLast_Click” CssClass=”paging”>Last</asp:LinkButton></td>

<td style=”padding-left:5px;”>&nbsp;</td>

</tr>

<tr>

<td colspan=”7″ align=”center” style=”display:none;” valign=”middle”>

&nbsp;</td>

</tr>

</table></div>

 
2 Comments

Posted by on June 25, 2010 in ASP Dot Net C#, Uncategorized

 

Hello world!


Welcome to WordPress.com. This is your first post. Edit or delete it and start blogging!

 
Leave a comment

Posted by on June 25, 2010 in Uncategorized