Imports System.ComponentModel
Imports System.Web.UI
Imports System.Web.UI.WebControls
Imports System.Drawing
Namespace CustomDataGrids
<DefaultProperty("Text"), ToolboxData("<{0}:myGrid runat=server></{0}:myGrid>")> Public Class myGrid
Inherits System.Web.UI.WebControls.DataGrid
#Region "Private Variables and Objects"
Dim internalDataSet As DataSet 'DataGrid Internal DataSet
Dim internalDataView As DataView 'DataGrid Internal DataView
Dim internalTableName As String 'Default Table Name (optional)
Protected WithEvents textPage As New TextBox 'Define TextBox for current number
Protected WithEvents labelError As New Label 'We want an error label in the footer
#End Region
#Region "Public Grid Properties "
Public Property GridDataSet() As DataSet 'DataGrid DataSet
Get
Return internalDataSet
End Get
Set(ByVal Value As DataSet)
internalDataSet = Value
End Set
End Property
Public Property GridTable() As String 'Default Table name (optional)
Get
Return internalTableName
End Get
Set(ByVal Value As String)
internalTableName = Value
End Set
End Property
Public ReadOnly Property ErrorMEssage() As String 'We want an error message
Get
Return Attributes("ErrorMessage")
End Get
End Property
#End Region
#Region "Grid Constructors"
Public Sub New()
'Set DataGrid Paging
PagerStyle.Mode = PagerMode.NextPrev
PagerStyle.ForeColor = Color.FromArgb(74, 60, 140)
PagerStyle.BackColor = Color.FromArgb(231, 231, 255)
Dim leftArrow As String = "<span style='font-family:webdings;font-size:medium;'>3</span>"
Dim rightArrow As String = "<span style='font-family:webdings;font-size:medium;'>4</span>"
PagerStyle.PrevPageText = leftArrow
PagerStyle.NextPageText = rightArrow
PagerStyle.PageButtonCount = 10
PagerStyle.HorizontalAlign = HorizontalAlign.Right
AllowPaging = True
PageSize = 4
'Visual Settings
AutoGenerateColumns = False
GridLines = GridLines.None
CellSpacing = 0
CellPadding = 3
BorderColor = Color.FromArgb(231, 231, 255)
BorderStyle = BorderStyle.Solid
BorderWidth = Unit.Pixel(1)
ForeColor = Color.Black
Font.Size = FontUnit.XSmall
Font.Name = "Verdana"
'Settings for Normal Rows
ItemStyle.ForeColor = Color.FromArgb(74, 60, 140)
ItemStyle.BackColor = Color.FromArgb(231, 231, 255)
'Settings for Alternating Rows
AlternatingItemStyle.BackColor = Color.FromArgb(247, 247, 247)
'Settings for Selected Rows
SelectedItemStyle.ForeColor = Color.FromArgb(247, 247, 247)
SelectedItemStyle.BackColor = Color.FromArgb(115, 138, 156)
'Settings for Heading
HeaderStyle.Font.Name = "Veranda"
HeaderStyle.Font.Bold = True
HeaderStyle.ForeColor = Color.FromArgb(247, 247, 247)
HeaderStyle.BackColor = Color.FromArgb(74, 60, 140)
HeaderStyle.HorizontalAlign = HorizontalAlign.Center
'Settings for Footer
ShowFooter = True
FooterStyle.ForeColor = Color.FromArgb(74, 60, 140)
FooterStyle.BackColor = Color.FromArgb(181, 199, 222)
'Handle the OnPageIndexChanged Event and Item Created Event internally
AddHandler ItemCreated, AddressOf OnItemCreated
AddHandler PageIndexChanged, AddressOf OnPageIndexChanged
End Sub
#End Region
#Region "Intercept ItemCreated and add features: HEADER for Sorting --> up/down error, PAGER for custom Pageing, FOOTER for summary & Buttons"
Public Shadows Sub OnItemCreated(ByVal sender As Object, ByVal e As DataGridItemEventArgs)
Dim itemType As ListItemType = e.Item.ItemType 'What item whas created?
If itemType = ListItemType.Footer Then 'FOOTER --> Erase custom footer and replace with three cells
Dim footerRow As TableCellCollection = e.Item.Cells
Dim footerCellCount As Integer = footerRow.Count
Dim i As Integer 'Remove existing Cells
For i = footerCellCount - 1 To 0 Step -1
e.Item.Cells.RemoveAt(i)
Next
Dim newCell As New TableCell 'Add just one cell
newCell.ColumnSpan = footerCellCount
newCell.HorizontalAlign = HorizontalAlign.Left
Dim labelReturnRows As New Label
Dim totalRows As Integer
Try
labelReturnRows.Text = "This is Page " + CType(CurrentPageIndex + 1, String) + " of " + CType(PageCount, String) + " Pages."
Catch
End Try
newCell.Controls.Add(labelReturnRows)
e.Item.Cells.Add(newCell)
End If
If itemType = ListItemType.Pager Then 'PAGER --> Add Page and Number to default behavior
Dim cellPager As TableCell = e.Item.Cells(0)
Dim cellControls As ControlCollection = cellPager.Controls
Dim leftArrow As WebControl = CType(cellControls(0), WebControl)
leftArrow.ToolTip = "Previous Record"
Dim rightArrow As WebControl = CType(cellControls(2), WebControl)
rightArrow.ToolTip = "Next Record"
cellPager.HorizontalAlign = HorizontalAlign.Center 'Pager aligns horizontal
cellPager.Controls.AddAt(1, New LiteralControl("Page:")) 'Leteral Controls are faster than labels
textPage.ID = "textPageID" 'Set the textBox for current and selected Page
textPage.Width = Unit.Pixel(30)
textPage.Text = CType((CurrentPageIndex + 1), String)
textPage.ToolTip = "Shows Current Page & allows setting of page number"
Dim buttonGoto As New Button 'Set the Goto Page Button with events
buttonGoto.Text = "Go"
buttonGoto.ID = "buttonGoto"
AddHandler buttonGoto.Click, AddressOf GotoPage_Click
buttonGoto.ToolTip = "Click to goto selected Page"
labelError.ID = "labelError"
labelError.ForeColor = Color.Red
labelError.Text = Attributes("ErrorMessage")
cellPager.Controls.AddAt(2, textPage)
cellPager.Controls.AddAt(3, New LiteralControl(" "))
cellPager.Controls.AddAt(4, buttonGoto)
cellPager.Controls.AddAt(7, labelError)
cellPager.HorizontalAlign = HorizontalAlign.Center
End If
End Sub
#End Region
#Region "Custom Grid Event Handlers - PageIndexChanged"
Public Shadows Sub OnPageIndexChanged(ByVal sender As Object, ByVal e As DataGridPageChangedEventArgs)
CurrentPageIndex = e.NewPageIndex
Attributes("ErrorMessage") = "" 'Make sure the error is reset
Update()
End Sub
#End Region
#Region "OnGotoPage Event Handler"
Private Sub GotoPage_Click(ByVal sender As Object, ByVal e As System.EventArgs)
Dim newPage As Integer
newPage = CInt(textPage.Text) - 1 'Get new page from TextBox
If newPage >= 0 And newPage <= PageCount - 1 Then 'Validate Page
CurrentPageIndex = newPage
Attributes("ErrorMessage") = "" 'Reset the Error Message
Update()
Else
Attributes("ErrorMessage") = " Incorrect Page Entry" 'Error Message
Update()
End If
End Sub
#End Region
#Region "Update() - Internal Data Update"
Public Sub Update()
'Even though the DefaultView would be used by the Grid, we might need to call it from a specific table in the Dataset
Try
If internalTableName = Nothing Then 'Do we choose the named or default table?
internalDataView = internalDataSet.Tables(0).DefaultView
Else
internalDataView = internalDataSet.Tables(internalTableName).DefaultView
End If
DataSource = internalDataView 'Update Internal DataView
DataBind()
Catch
DataBind() 'Calling DataBind will avoid and error and return an empty grid
End Try
End Sub
#End Region
End Class
End Namespace