Code Bank
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
 
 
User Name:
Password:
Remember me
Go Back   ASP Free ForumsProgrammingCode Bank

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread ASP Free Forums Sponsor:
  #1  
Old June 1st, 2006, 01:08 PM
Memnoch's Avatar
Memnoch Memnoch is offline
Unholy Moderator
ASP Free God 14th Plane (11500 - 11999 posts)
 
Join Date: Oct 2003
Location: In hell, where did you think?
Posts: 11,770 Memnoch User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)Memnoch User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)Memnoch User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)Memnoch User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)Memnoch User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)Memnoch User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)Memnoch User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)Memnoch User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)Memnoch User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)Memnoch User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)Memnoch User rank is Lieutenant Colonel (40000 - 50000 Reputation Level) 
Time spent in forums: 3 Weeks 5 Days 6 h 58 m 22 sec
Reputation Power: 469
ASP.NET 2.0 GridView Class

I've written a GridView class that allows developers to "shortcut" commonly used functionality in the GridView control.
Examples of it's usage is in the following post.

Class Procedures:
1. BindGridView - Will bind the control do a datatable object and provide the "records returned" text to a label of literal control, if one is passed.

2. GridViewPageIndexChanged - Manages the paging of the GridView control.

3. CheckAndFixPageIndex - Manages issues that might arise with resetting the page index, such as when you are on page 9, and then run another search that only returns 1 page.

4. GridViewSortCommand - Manages the Sorting functionality of the GridView.

5. GetSingleRowPrimaryKey - Enables you to extract the Primary Key for the selected row. In 1.x we could use the following code to get the primary key of the row, but this isn't possible in 2.0.
Code:
Dim strEmployeeID As String

strEmployeeID = DataGrid1.DataKeys(e.Item.ItemIndex).ToString()


Code:
Option Strict On

Imports System.Web.UI.WebControls
Imports System.Data

Public Class clsWebGridView

#Region "   BindGridView Sub..."

	Public Shared Sub BindGridView(ByRef dtGrid As DataTable, ByRef gvGrid As System.Web.UI.WebControls.GridView, Optional ByRef litRecordCount As System.Web.UI.WebControls.Literal = Nothing, Optional ByRef lblRecordCountLabel As System.Web.UI.WebControls.Label = Nothing)
		Dim strSort As String = String.Empty
		Dim intTotalRecords As Integer = 0
		Dim strText As String = String.Empty

		If dtGrid Is Nothing OrElse gvGrid Is Nothing Then
			Exit Sub
		End If

		intTotalRecords = dtGrid.Rows.Count
		strSort = gvGrid.Attributes("SortExpression")
		dtGrid.Sort = strSort
		dtGrid.DataView.RowFilter = gvGrid.Attributes("Filter")
		gvGrid.DataSource = dtGrid.DataView

		If Not litRecordCount Is Nothing OrElse Not lblRecordCountLabel Is Nothing Then
			If intTotalRecords = dtGrid.DataView.Count Then
				If intTotalRecords = 1 Then
					strText = "1 matching record returned"
				Else
					strText = intTotalRecords & " matching records returned"
				End If
			Else
				If dtGrid.DataView.Count = 1 Then
					strText = "1 matching record (of " & intTotalRecords.ToString() & ") returned"
				Else
					strText = dtGrid.DataView.Count & " matching records (of " & intTotalRecords.ToString() & ") returned"
				End If
			End If
		End If

		If Not litRecordCount Is Nothing Then
			litRecordCount.Text = strText
		End If

		If Not lblRecordCountLabel Is Nothing Then
			lblRecordCountLabel.Text = strText
		End If

		If gvGrid.AllowPaging = True Then
			If gvGrid.PageSize = 0 OrElse dtGrid.Rows.Count >= gvGrid.PageSize Then
				gvGrid.PagerSettings.Visible = True
			Else
				gvGrid.PagerSettings.Visible = False
			End If
		End If

		Call CheckAndFixPageIndex(gvGrid, gvGrid.PageIndex)

		gvGrid.DataBind()
	End Sub

#End Region

#Region "   GridViewPageIndexChanged Sub..."

	Public Shared Sub GridViewPageIndexChanged(ByRef source As Object, ByRef e As System.Web.UI.WebControls.GridViewPageEventArgs)
		Dim gvGrid As GridView

		gvGrid = DirectCast(source, System.Web.UI.WebControls.GridView)

		Call CheckAndFixPageIndex(gvGrid, e.NewPageIndex)
	End Sub

#End Region

#Region "   CheckAndFixPageIndex Sub..."

	Private Shared Sub CheckAndFixPageIndex(ByRef gvGrid As System.Web.UI.WebControls.GridView, ByVal NewPageIndex As Integer)
		Dim intRows As Integer = -1

		If gvGrid.AllowPaging = True Then
			If Not gvGrid.DataSource Is Nothing Then
				If TypeOf gvGrid.DataSource Is System.Data.DataView Then
					intRows = DirectCast(gvGrid.DataSource, System.Data.DataView).Count
				ElseIf TypeOf gvGrid.DataSource Is System.Data.DataTable Then
					intRows = DirectCast(gvGrid.DataSource, System.Data.DataTable).Rows.Count
				ElseIf TypeOf gvGrid.DataSource Is ICollection Then
					intRows = DirectCast(gvGrid.DataSource, ICollection).Count
				Else
					intRows = -1
				End If

				If ((intRows - 1) \ gvGrid.PageSize) >= NewPageIndex Then
					gvGrid.PageIndex = NewPageIndex
				Else
					gvGrid.PageIndex = 0
				End If
			Else
				If (gvGrid.PageCount - 1) < NewPageIndex Then
					gvGrid.PageIndex = 0
				Else
					gvGrid.PageIndex = NewPageIndex
				End If
			End If
		End If
	End Sub

#End Region

#Region "   GridViewSortCommand Sub..."

	Public Shared Sub GridViewSortCommand(ByRef source As Object, ByRef e As System.Web.UI.WebControls.GridViewSortEventArgs)
		Dim gvGrid As GridView
		Dim strOldSort As String = String.Empty
		Dim strNewSort As String = String.Empty

		gvGrid = DirectCast(source, System.Web.UI.WebControls.GridView)
		strOldSort = gvGrid.Attributes("SortExpression")
		strNewSort = e.SortExpression.ToString

		If Not strOldSort Is Nothing AndAlso strOldSort = strNewSort Then
			strNewSort &= " DESC"
		End If

		gvGrid.Attributes("SortExpression") = strNewSort
	End Sub

#End Region

#Region "GetSingleRowPrimaryKey Function..."

	Public Shared Function GetSingleRowPrimaryKey(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewCommandEventArgs  ) As String
		Dim gvGrid As GridView
		Dim gvRow As GridViewRow
		Dim strReturnID As String

		gvGrid = DirectCast(sender, GridView)
		gvRow = DirectCast(DirectCast(e.CommandSource, Control).NamingContainer, GridViewRow)
		strReturnID = gvGrid.DataKeys(gvRow.RowIndex).Value.ToString

		Return strReturnID
	End Function

#End Region

End Class

Reply With Quote
  #2  
Old June 1st, 2006, 01:19 PM
Memnoch's Avatar
Memnoch Memnoch is offline
Unholy Moderator
ASP Free God 14th Plane (11500 - 11999 posts)
 
Join Date: Oct 2003
Location: In hell, where did you think?
Posts: 11,770 Memnoch User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)Memnoch User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)Memnoch User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)Memnoch User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)Memnoch User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)Memnoch User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)Memnoch User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)Memnoch User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)Memnoch User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)Memnoch User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)Memnoch User rank is Lieutenant Colonel (40000 - 50000 Reputation Level) 
Time spent in forums: 3 Weeks 5 Days 6 h 58 m 22 sec
Reputation Power: 469
Examples of usage:

Examples of usage:

BindGridView
Code:
Private Sub LoadRecords()
   Dim dtData As DataTable

   dtData = Query to get records

   If dtData.Rows.Count > 0 Then
     PrimaryKeyID is the primary key for the data, such as CompanyID, CustomerID, etc...
     gvDepartments.DataKeyNames = New String() {"PrimaryKeyID"}
     clsWebGridView.BindGridView(dt, GridView1)
     GridView1.Visible = True
   Else
     GridView1.Visible = False
   End If
End Sub


GridViewPageIndexChanged
Code:
Protected Sub GridView1_PageIndexChanging(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewPageEventArgs) Handles gvDepartments.PageIndexChanging
  clsWebGridView.GridViewPageIndexChanged(sender, e)
  LoadRecords()
End Sub


GridViewSortCommand
Code:
Protected Sub gvDepartments_Sorting(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewSortEventArgs) Handles gvDepartments.Sorting
  clsWebGridView.GridViewSortCommand(sender, e)
  LoadRecords()
End Sub


Assumes you have a button or imagebutton for each row to do "edit" and "delete" events where the CommandName property is assigned.

GetSingleRowPrimaryKey
Code:
Protected Sub GridView1_RowCommand(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewCommandEventArgs  ) Handles gvEmployees.RowCommand
  Select Case e.CommandName
    Case "EditRecord"
      Response.Redirect("SomePage.aspx?RecordID=" & clsWebGridView.GetSingleRowPrimaryKey(sender, e))
    Case "DeleteRecord"
	  A delete method you pass the primary key to, to delete the record
      mclsDML.dt_DELETE_Record(clsWebGridView.GetSingleR  owPrimaryKey(sender, e))
	  LoadRecords()
    Case Else
  End Select
End Sub

Last edited by Memnoch : June 1st, 2006 at 05:39 PM.

Reply With Quote
Reply

Viewing: ASP Free ForumsProgrammingCode Bank > ASP.NET 2.0 GridView Class


Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump


Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 





© 2003-2008 by Developer Shed. All rights reserved. DS Cluster 3 hosted by Hostway
Stay green...Green IT