| |||||||||
![]() |
|
|
«
Previous Thread
|
Next Thread
»
|
Thread Tools | Search this Thread | Rate Thread | Display Modes |
|
#1
|
||||
|
||||
|
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
|
|
#2
|
||||
|
||||
|
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. |
![]() |
| Viewing: ASP Free Forums > Programming > Code Bank > ASP.NET 2.0 GridView Class |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|
|
|