DATAGRID自定义控

原创 2004年08月13日 12:24:00

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("&nbsp"))
                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

Android自定义组合控件

【课程介绍】 开发工具:Android Studo ;如何自定义组合控件
  • 2016年11月13日 21:15

DataGrid自定义列标题

  • 2010年07月02日 08:47
  • 140KB
  • 下载

自定义的 DataGrid样式

  • 2012年04月27日 18:08
  • 558KB
  • 下载

自定义DataGrid显示的办法

 代码代码,依然是代码,下面这个是一个自定义了特定列,和特定列的单元格的DataGrid(当然,这两个不再在一列上):xml version="1.0" encoding="utf-8"?>mx:Ap...
  • tj19832
  • tj19832
  • 2008-01-02 23:46:00
  • 1359

easyUI——DataGrid自定义操作列

项目中遇到一个问题:在easyUI的datagrid中增加一列为操作列。官网的demo中没有给出具体的解决问题的方法。解决的思路就是利用单元格的属性formatter中添加内容。...
  • suneqing
  • suneqing
  • 2015-01-31 15:54:03
  • 5497

DataGrid 自定义字段内容

因为DataGrid Web 控件的AutoGenerateColumn 属性预设为True,表示会自动产生数据源中所有的字段。如果我们想自订DataGrid Web 控制所要显示的字段,只要将Aut...
  • limshirley
  • limshirley
  • 2017-04-17 14:16:43
  • 573

easyUI自定义DataGrid分页栏

@author 雨泽 datagrid内置一个很好特性的分页功能,自定义也相当简单,在这个教程中,我们将创建一个datagrid 和添加一些自定义按钮在分页工具栏. 查看...
  • me4405801
  • me4405801
  • 2016-01-07 10:38:45
  • 457

如何在DataGrid 控件中实现自定义分页操作

   在一般情况下,DataGrid控件每次翻页操作时,都会将数据源中的数据重新调用依次,当数据源中的数据比较多的时候,这样就会浪费系统的资源和降低程序执行的效率,解决的办法就是如同标题所说,依靠自定...
  • stone0419
  • stone0419
  • 2007-10-06 14:39:00
  • 848

【.Net码农】DataGrid 数据绑定使用小结二(自定义列)

http://www.cnblogs.com/tnspop/archive/2012/01/10/2317803.html
  • CrackLibby
  • CrackLibby
  • 2014-10-30 10:27:10
  • 1104

easyui datagrid自定义操作列

通过formatter方法给Jquery easyui 的datagrid 每行增加操作链接我们都知道Jquery的EasyUI的datagrid可以添加并且自定义Toolbar,这样我们选择一行然后...
  • itmyhome
  • itmyhome
  • 2014-04-21 21:48:46
  • 5065
收藏助手
不良信息举报
您举报文章:DATAGRID自定义控
举报原因:
原因补充:

(最多只允许输入30个字)