DataGrid custom control

xiaoxiao2021-03-06  142

Imports system.componentmodelimports system.Web.ui.WebControlsimports system.drawing

Namespace CustomDataGrids

")> 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 INTERDATASET End Get Set (Byval Value As Dataset) INTERDATASET = VALUE End SET End Property

Public property gridtable () AS STRING 'DEFAULT TABLE NAME (OPTIONAL) GET RETURN INTERTABLENAME End Get Set (Byval Value As String) InternalTableName = Value End End Property

Public Readonly Property ErrorMessage () AS String 'We Want An Error Message Get Return Attributes ("ErrorMessage") End Get 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 = " 3 " DIM RIGHTARROW AS STRING = " 4 "Pagersty.prevpageText = Leftarrow Pagersty.nextPageText = RightArrowPagerstyLle.pageButtonCount = 10 Pagersty.horizontalalign = Horizontalalign.right

AllowPaging = true pagepaging = 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. Horizontalarign = horizontalalign.center'Settings for footer showfooter = true footerstyle.ForeColor = Color.Fromargb (74, 60, 140) Footerstyle.BackColor = Color.Fromargb (181, 199, 222)

'Handle The onpageIndexchange 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? created?

If ITEMTYPE = ListiteMTYPE.FOOTER TEN '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.LeftDim 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 (labelreturnrow) E.Item.cells.add (newcell) endiff

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 Litralcontrol ("Page:") 'Leeten 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 = " Labelrror "Labelrror.Forecolor = color.red labelerror.text = attributes (" errorMessage ")

Cellpager.controls.addat (2, textpage) Cellpager.Controls.addat (3, New Litral Control ("& NBSP")) Cellpager.controls.addat (4, Button) 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 the 'do we should the named or default ta ble? 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 Classend Namespace

转载请注明原文地址:https://www.9cbs.com/read-127594.html

New Post(0)