RSS

Gridview Sorting with VB Net with image

06 Apr

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

Advertisements
 
Leave a comment

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

 

Tags: ,

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

 
%d bloggers like this: