RSS

Category Archives: Gridview

Gridview Paging & Sorting

Custom Gridview Paging and Sorting with Vb.net (Styling)


Asp File

<asp:ScriptManager ID=”ScriptManager1″ runat=”server”>
</asp:ScriptManager>
<asp:UpdatePanel ID=”UpdatePanel1″ runat=”server”>
<ContentTemplate>

<asp:GridView ID=”grdList” runat=”server” CellPadding=”2″ CellSpacing=”0″ BorderWidth=”0px”
AutoGenerateColumns=”False” GridLines=”None”
OnPageIndexChanging=”grdList_PageIndexChanging”  Width=”100%”
CssClass=”listing-table”  AllowSorting=”true” AllowPaging=”true”
PageSize=”3″   ShowFooter=”True” onrowcreated=”grdList_RowCreated” >
<Columns>
<asp:BoundField DataField=”Title” HeaderText=”Name” SortExpression=”Name” />
<asp:BoundField DataField=”Class” HeaderText=”Class” SortExpression=”Class” />
<asp:BoundField DataField=”RollNo” HeaderText=”RollNo” SortExpression=”RollNo”  />

</Columns>
<PagerTemplate>
<div>
<div id=”pagn”>
Goto Page
<asp:DropDownList ID=”ddlPageSelector” runat=”server” AutoPostBack=”true”>
</asp:DropDownList>
of
<%=grdList.PageCount%>

<asp:Label ID=”lblNumber” runat=”server”></asp:Label>
<span>
<asp:Button Text=”First” CommandName=”Page” CommandArgument=”First” runat=”server”
ID=”btnFirst” />
<asp:Button Text=”Previous” CommandName=”Page” CommandArgument=”Prev” runat=”server”
ID=”btnPrevious” />
</span>
<%– <span>|</span>–%>
<%–<span>|</span>–%>
<span>
<asp:Button Text=”Next” CommandName=”Page” CommandArgument=”Next” runat=”server”
ID=”btnNext” />
<asp:Button Text=”Last” CommandName=”Page” CommandArgument=”Last” runat=”server”
ID=”btnLast” />
<div style=”float: right;padding-right:15px”>
Record Per Page:
<asp:TextBox ID=”txtPageSize” runat=”server” Width=”25px” EnableViewState=”true”></asp:TextBox>
<asp:LinkButton ID=”lnkSavePageSize” runat=”server” ValidationGroup=”grpList” OnClick=”lnkSavePageSize_Click”><strong>Save</strong></asp:LinkButton>

</div>
</span>
</div>
</div>

<asp:CompareValidator ValidationGroup=”grpList” Display=”None” ID=”CompareValidator1″
ControlToValidate=”txtPageSize” Type=”Integer” Operator=”DataTypeCheck” runat=”server”
ErrorMessage=”Enter numeric value.”></asp:CompareValidator>
<asp:ValidationSummary ValidationGroup=”grpList” ShowSummary=”false” ShowMessageBox=”true”
ID=”ValidationSummary1″ runat=”server” />
</PagerTemplate>
</asp:GridView>

</ContentTemplate>
</asp:UpdatePanel>

Code Behid

private void BindData()
{
grdList.DataSource = (Pass Dataset or datatable Here)
grdList.DataBind();
}

protected void grdList_PageIndexChanging(object sender, GridViewPageEventArgs e)
{
grdList.PageIndex = e.NewPageIndex;
BindData();

}
protected void grdList_Sorting(object sender, GridViewSortEventArgs e)
{

string sortExpression = e.SortExpression;

ViewState[“SortExpression”] = sortExpression;

if (GridViewSortDirection == SortDirection.Ascending)
{
GridViewSortDirection = SortDirection.Descending;
SortGridView(sortExpression, “DESC”);
}
else
{
GridViewSortDirection = SortDirection.Ascending;
SortGridView(sortExpression, “ASC”);
}

}

private SortDirection GridViewSortDirection
{

get
{

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

ViewState[“sortDirection”] = SortDirection.Ascending;

return (SortDirection)ViewState[“sortDirection”];

}

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

}

private void SortGridView(string sortExpression, string direction)
{

DataTable dataTable = new DataTable();
dataTable = getData();
DataView dv = new DataView();
dv = dataTable.DefaultView;
dv.Sort = sortExpression + ” ” + direction;

grdList.DataSource = dv;
grdList.DataBind();

}

protected void grdList_RowCreated(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.Pager)
{
SetPagerButtonStates(grdList, e.Row, this);
}
}
public void SetPagerButtonStates(GridView gridView, GridViewRow gvPagerRow, Page page)
{

int pageIndex = gridView.PageIndex;
int pageCount = gridView.PageCount;
Button btnFirst = (Button)gvPagerRow.FindControl(“btnFirst”);
Button btnPrevious = (Button)gvPagerRow.FindControl(“btnPrevious”);
Button btnNext = (Button)gvPagerRow.FindControl(“btnNext”);
Button btnLast = (Button)gvPagerRow.FindControl(“btnLast”);
TextBox txtPageSize = (TextBox)gvPagerRow.FindControl(“txtPageSize”);
btnFirst.Enabled = btnPrevious.Enabled = (pageIndex != 0);
btnNext.Enabled = btnLast.Enabled = (pageIndex < (pageCount – 1));
DropDownList ddlPageSelector = (DropDownList)gvPagerRow.FindControl(“ddlPageSelector”);
ddlPageSelector.Items.Clear();
for (int i = 1; i <= gridView.PageCount; i++)
{
ddlPageSelector.Items.Add(i.ToString());
}
ddlPageSelector.SelectedIndex = pageIndex;
txtPageSize.Text = gridView.PageSize.ToString();
//Anonymous method (see another way to do this at the bottom)
ddlPageSelector.SelectedIndexChanged += delegate
{
grdList.PageIndex = ddlPageSelector.SelectedIndex;
BindData();
};
//for vb.net( VB.net 2.0 does not support anonymous methods)

//protected void ddlPageSelector_SelectedIndexChanged(object sender, EventArgs e)
//{
//GridView1.PageIndex = ((DropDownList)sender).SelectedIndex;
//GridView1.DataBind();
//}
}

protected void lnkSavePageSize_Click(object sender, EventArgs e)
{
GridViewRow pagerRow = grdList.BottomPagerRow;
TextBox temp1 = (TextBox)pagerRow.FindControl(“txtPageSize”);
if (temp1.Text != “”)
{
grdList.PageSize = Convert.ToInt32(temp1.Text);
}
}

CSS for Paging

/*Amazon*/
div.header {
border-top: 1px solid #d0eaf8;
border-bottom: 1px solid #d0eaf8;
width: 100%;
margin: 4px 0;
}

div.resultCount {
width: 29%;
}

div.pagn {
text-align: left;
width: 100%;
}

div.pagnBtm {
text-align: center;
width: 100%;
}

div.pagn .pagnCur, div.pagnBtm .pagnCur
{
font-weight: bold;
padding: 0 5px;
}

div.pagn .pagnDisabled, div.pagnBtm .pagnDisabled {
color: #999;
padding: 0px 5px;
white-space: nowrap;
}

div.pagn .pagnMore, div.pagn .pagnSep, div.pagnBtm .pagnMore, div.pagnBtm div.pagnSep {
padding: 0 2px;
}

div.pagn .pagnLead, div.pagnBtm .pagnLead {
font-weight: bold;
padding: 0 5px 0 2px;
}

div.pagn input, div.pagn input:visited, div.pagnBtm input, div.pagnBtm input:visited {
text-decoration: none;
padding: 6px;
color: #055d90;
white-space: nowrap;
}

div.pagn input:hover, div.pagn input:active, div.pagnBtm input:hover, div.pagnBtm input:active
{
text-decoration: none;
padding: 6px;
color:#050;
white-space: nowrap;

}

div.headerPaging {
background: url(“../images/tile-blue-bg._V45465059_.gif”) repeat-x;
}

 

Image for CSS

Advertisements
 
1 Comment

Posted by on June 4, 2011 in Gridview, Vb.Net

 

Tags: ,

Gridview Sorting with VB Net with image


Gridview Sorting  with VB.Net

<asp:GridView ID=”grd” runat=”server” Width=”100%” BorderWidth=”0″ BackColor=”#ffffff”

CellPadding=”0″ CellSpacing=”0″ GridLines=”None” ShowFooter=”true” AutoGenerateColumns=”False”

PageSize=”50″ AllowPaging=”true” AllowSorting=”true”>

<PagerSettings Visible=”False” />

<Columns>

<asp:TemplateField>

<HeaderTemplate>

<table border=”0″ cellpadding=”0″ cellspacing=”0″ width=”940px”>

<tr>

<td>

<asp:LinkButton OnClick=”Page_Click” ID=”lnkPageSort” runat=”server”>Article Title</asp:LinkButton>&nbsp;

<asp:Image ID=”imgTitle” runat=”server” ImageAlign=”AbsMiddle” ImageUrl=”images/icons/sortIcon_asc.gif” />

</td>

<td>

<asp:LinkButton OnClick=”Author_Click” ID=”lnkAuthor” runat=”server”>Author</asp:LinkButton>&nbsp;

<asp:Image ID=”imgAuthor” runat=”server” ImageAlign=”AbsMiddle” ImageUrl=”images/icons/sortIcon_asc.gif” />

</td>

<td>

<asp:LinkButton OnClick=”Published_Click” ID=”lnkPublished” runat=”server”>Published</asp:LinkButton>&nbsp;

<asp:Image ID=”imgPublished” runat=”server” ImageAlign=”AbsMiddle” ImageUrl=”images/icons/sortIcon_asc.gif” />

</td>

<td>

<asp:LinkButton OnClick=”Approved_Click” ID=”lnkApproved” runat=”server”>Approved</asp:LinkButton>&nbsp;

<asp:Image ID=”imgApproved” runat=”server” ImageAlign=”AbsMiddle” ImageUrl=”images/icons/sortIcon_asc.gif” />

</td>

<td align=”center”>

<asp:LinkButton OnClick=”DateCreated_Click” ID=”lnkDateCread” runat=”server”>Created</asp:LinkButton>&nbsp;

<asp:Image ID=”imgDateCreated” runat=”server” ImageAlign=”AbsMiddle” ImageUrl=”images/icons/sortIcon_asc.gif” />

</td>

<td align=”center”>

<asp:LinkButton OnClick=”dateModified_Click” ID=”lnkdateModified” runat=”server”>Modified</asp:LinkButton>&nbsp;

<asp:Image ID=”imgdateModified” runat=”server” ImageAlign=”AbsMiddle” ImageUrl=”images/icons/sortIcon_asc.gif” />

</td>

<td>

Actions

</td>

</tr>

</HeaderTemplate>

<ItemTemplate>

<tr>

<td>

<img src=”images/page1.gif” alt=”Edit Page” style=”vertical-align: text-bottom” />&nbsp;<a

href=”newsdetail.aspx?id=<%#Eval(“articleID”)%>”><%#Eval(“articleTitle”)%></a>

</td>

<td>

<%#Eval(“FullName”)%>

</td>

<td>

<%#Eval(“IsPublished”)%>

</td>

<td>

<%#Eval(“IsApproved”)%>

</td>

<td>

<%# DateTimeFormat.formateDate(DataBinder.Eval(Container.DataItem, “dateCreated”))%>

</td>

<td>

<%# DateTimeFormat.formateDate(DataBinder.Eval(Container.DataItem, “dateModified”))%>

</td>

<td>

<a href=”newsdetail.aspx?id=<%#Eval(“articleID”)%>”><strong>Edit</strong></a>&nbsp;&nbsp;|&nbsp;&nbsp;<asp:LinkButton

ID=”lnkDelete” OnClientClick=”return confirm(‘Are you sure you want to delete the record?’);”

runat=”server” CommandName=”cmdDelete” CommandArgument='<%#Eval(“articleID”)%>’><strong>Delete</strong></asp:LinkButton>

</td>

</tr>

</ItemTemplate>

<FooterTemplate>

<tr>

<td colspan=”7″>

&nbsp;

</td>

</tr>

</FooterTemplate>

</asp:TemplateField>

</Columns>

</asp:GridView>

For Gridview Column set Custom Date Formate

First Create class with DateTimeFormat.vb

In App_Code folder

Public Class DateTimeFormat

Public Shared Function formateDate(ByVal obj As Object) As String

Dim dtFormat As String = “”

Try

If Not obj Is Nothing Then

dtFormat = CType(obj, DateTime).ToString(“MM/dd/yy”) & “&nbsp;” & CType(obj, DateTime).ToString(“h:mm tt”)

End If

Catch ex As Exception

End Try

Return dtFormat

End Function

End Class

Code Behind Side

‘//Global Variables

Public Shared sortOrder() As String = {“ASC”}

Public Shared sortLast() As String = {“ASC”, “Title”}

Dim bolRights As Boolean = False

Public strCMSVersion As String = String.Empty

Public strControlPage As String = String.Empty

Public strSiteURL As String = String.Empty

At Page Load Event

If Not Page.IsPostBack Then

bindData()

End If

1.Function Load Data

Private Sub bindData()

Try

DataInfo = ArticlesBAL.GetArticleAll(Convert.ToInt32(Session(“siteID”)))

bindDataPaging(0)

If DataInfo.Rows.Count < 1 Then

Me.lblNoPage.Text = “There are currently no News to display. You may create a new News now by clicking the “”Add New News”” link above.”

Me.lblNoPage.visible = True

End If

Catch ex As Exception

Throw ex

End Try

End Sub

2.Function DataPage

Private Sub bindDataPaging(ByVal pageIndex As Integer)

Try

If DataInfo.Rows.Count > 0 Then

pnlFooter.Visible = False

Else

pnlFooter.Visible = True

End If

processPages(pageIndex)

Catch ex As Exception

Throw ex

End Try

End Sub

3.ProcessPages

Private Sub processPages(ByVal pageIndex As Integer)

Try

If DataInfo.Rows.Count > grd.PageSize Then

grd.DataSource = DataInfo

grd.DataBind()

Dim pageCount As Integer = 0

pageCount = grd.PageCount

If (pageIndex < 0) Then

grd.PageIndex = 0

ElseIf (pageIndex > pageCount – 1) Then

grd.PageIndex = pageCount – 1

Else

If (pageIndex = 0) Then

btnPrev.Visible = False

imgbtnPrev.Visible = False

btnNext.Visible = True

imgbtnNext.Visible = True

ElseIf (pageIndex = pageCount – 1) Then

btnPrev.Visible = True

imgbtnPrev.Visible = True

btnNext.Visible = False

imgbtnNext.Visible = False

Else

btnPrev.Visible = True

imgbtnPrev.Visible = True

btnNext.Visible = True

imgbtnNext.Visible = True

End If

grd.PageIndex = pageIndex

End If

pnlPaging.Visible = True

lblPageCount.Text = “| ” & (grd.PageIndex + 1).ToString() & ” |” & ” of ” & pageCount.ToString()

End If

grd.DataSource = DataInfo

grd.DataBind()

Catch ex As Exception

Throw ex

End Try

End Sub

4.grid Events

Protected Sub grd_PageIndexChanging(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewPageEventArgs) Handles grd.PageIndexChanging

End Sub

5.For Delete Link Button

Protected Sub grd_RowCommand(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewCommandEventArgs) Handles grd.RowCommand

If e.CommandName = “cmdDelete” Then

Dim DocumentID As Integer = e.CommandArgument

ArticlesBAL.DeleteArticle(DocumentID)

Dim pageIndex As Integer = grd.PageIndex

bindData()

bindDataPaging(pageIndex)

End If

End Sub

6.Grid RowCommand Event for Header Image change

Protected Sub grd_RowDataBound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewRowEventArgs) Handles grd.RowDataBound

If sortInfo <> “” Then

If (e.Row.RowType = DataControlRowType.Header) Then

Dim imgSort As Image = e.Row.FindControl(sortInfo)

If sortLast(0) = “DESC” Then

imgSort.ImageUrl = “images/icons/sortIcon_asc.gif”

ElseIf sortLast(0) = “ASC” Then

imgSort.ImageUrl = “images/icons/sortIcon_desc.gif”

End If

Dim lnkbtn As LinkButton = e.Row.FindControl(“lnkApproved”)

lnkbtn.Text = Resources.Resource.lblGridActive

Dim lnksite As LinkButton = e.Row.FindControl(“lnkPublished”)

lnksite.Text = Resources.Resource.lblGridPublish

End If

Else

If (e.Row.RowType = DataControlRowType.Header) Then

Dim lnkbtn As LinkButton = e.Row.FindControl(“lnkApproved”)

lnkbtn.Text = Resources.Resource.lblGridActive

Dim lnksite As LinkButton = e.Row.FindControl(“lnkPublished”)

lnksite.Text = Resources.Resource.lblGridPublish

End If

End If

End Sub

7.Grid Sorting Event

Protected Sub grd_Sorting(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewSortEventArgs) Handles grd.Sorting

End Sub

8.Grid Header link sorting

Protected Sub Page_Click(ByVal sender As Object, ByVal e As System.EventArgs)

Dim lnkHeader As LinkButton = CType(sender, LinkButton)

sorting(0, “articleTitle”, “imgTitle”)

End Sub

Protected Sub Author_Click(ByVal sender As Object, ByVal e As System.EventArgs)

sorting(0, “FullName”, “imgAuthor”)

End Sub

Protected Sub Published_Click(ByVal sender As Object, ByVal e As System.EventArgs)

sorting(0, “IsPublished”, “imgPublished”)

End Sub

Protected Sub Approved_Click(ByVal sender As Object, ByVal e As System.EventArgs)

sorting(0, “IsApproved”, “imgApproved”)

End Sub

Protected Sub DateCreated_Click(ByVal sender As Object, ByVal e As System.EventArgs)

sorting(0, “dateCreated”, “imgDateCreated”)

End Sub

Protected Sub dateModified_Click(ByVal sender As Object, ByVal e As System.EventArgs)

sorting(0, “dateModified”, “imgdateModified”)

End Sub

8.Sorting Function

Public Sub sorting(ByVal order As Integer, ByVal sortName As String, ByVal imgTitle As String)

Try

Dim dt As Data.DataTable = DataInfo

Dim dtV As New DataView

sortLast(0) = sortOrder(order)

sortLast(1) = sortName

sortInfo = imgTitle

If sortOrder(order) = “DESC” Then

dtV = dt.DefaultView

dtV.Sort = sortName & ” ” & sortOrder(order)

sortOrder(order) = “ASC”

ElseIf sortOrder(order) = “ASC” Then

dtV = dt.DefaultView

dtV.Sort = sortName & ” ” & sortOrder(order)

sortOrder(order) = “DESC”

End If

bindDataPaging(grd.PageIndex)

Catch ex As Exception

Throw ex

End Try

End Sub

9.Next Page Grid

Protected Sub btnNext_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnNext.Click

bindDataPaging(grd.PageIndex + 1)

End Sub

10.Previou Page Grid

Protected Sub btnPrev_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnPrev.Click

bindDataPaging(grd.PageIndex – 1)

End Sub

11.Go To Specific Page Grid

Protected Sub btnGoToPage_Click(ByVal sender As Object, ByVal e As System.Web.UI.ImageClickEventArgs) Handles btnGoToPage.Click

bindDataPaging(Convert.ToInt32(Trim(txtGoToPage.Text) – 1))

End Sub

 
Leave a comment

Posted by on April 6, 2011 in Gridview, Vb.Net

 

Tags: ,

First Letter of string is Capital in Vb.net and C#


if you want to show your string on web in proper case like every first character is captital then you can use this code

Vb.Net

Dim strchar As String=’sajjad ahmed’

Dim strCaptical As String = StrConv(strchar, VbStrConv.ProperCase)

Result :

Sajjad Ahmed

C-Sharp(C#)

string strchar=”sajjad ahmed”

Strings.StrConv(strchar, VbStrConv.ProperCase)
 
2 Comments

Posted by on April 6, 2011 in ASP Dot Net C#, Gridview