CSDN博客

img rickjelly2004

*控件*有横向和竖向的滚动条的DataGrid(横向支持从第几列滚动)

发表于2004/12/14 9:11:00  3744人阅读

分类: asp.net

(不好意思,刚才有人要横竖都有,我就改了一下,可以都出现横向和竖向滚动,q其实我觉得没必要了,要不然,微软干嘛要用PAGESIZE来控制呀)

目前在论坛有很多的人问有滚动条的DATAGRID,所以我就急急忙忙的做了一个,当然了,功能就是滚动条,不过我做好了发现分页好象不行了,没办法呀,有时间我还要给它做一个分页控件,一下是我的代码,有兴趣的人可以考回去,自己完善一下,给它加一个分页(我的说的分页不行,好象是分页到上面去了),由于我不多考虑,时间很紧,上面问题也很多,望大家改改吧。
用法:FixedColumns:你要滚动横向,从哪一列开始

ShowScrollMode:你要横向滚动,还是竖项滚动

XScrollBarWidth:横滚动条的宽度
YScrollBarWidth:竖滚动条的高度

记住,你先设置ShowScrollMode,然后ScrollBarWidth的宽度,

你可能设置FixedColumns的时候回发现前面的几列不见了,由于你没有给前面几列没设置宽度

所以你在添加列的时候还要给没一列加一个宽度,会的吧,属性编辑器中加
注:你有横滚动时,允许分页会出现两个分页符,你可以设置横滚动的高度挡住它,呵呵,没多时间去改了,我要做一个很大的项目了,望见谅!

Imports System.ComponentModel
Imports System.Web.UI
Imports System.Drawing
Imports System.Web
Imports System.Web.UI.WebControls

<DefaultProperty("Text"), ToolboxData("<{0}:XYScrollDataGrid runat=server></{0}:XYScrollDataGrid>")> _
Public Class XYScrollDataGrid
    Inherits System.Web.UI.WebControls.DataGrid

    Private mScrollWidth As Unit
    Private mDataMode As DataModeType
    Private mFixedColumns As Integer
    Private mShowScrollDataMode As ShowScrollDataMode
    Private ymScrollWidth As Unit

    Public Enum ShowScrollDataMode
        XScroll
        YScroll
        Both
        None
    End Enum

    Public Enum DataModeType
        可変行Scroll
        固定行Scroll
    End Enum

#Region "プロパティ"
#Region "親クラスから継承された表示プロパティ"

    <NotifyParentProperty(True), Browsable(False), _
    EditorBrowsable(EditorBrowsableState.Advanced)> _
    Public Overrides Property AutoGenerateColumns() As Boolean
        Get
            Return MyBase.AutoGenerateColumns
        End Get
        Set(ByVal Value As Boolean)
            MyBase.AutoGenerateColumns = False
        End Set
    End Property

    <NotifyParentProperty(True), Browsable(False), _
    EditorBrowsable(EditorBrowsableState.Advanced)> _
    Public Shadows Property DataMember() As String
        Get
            Return MyBase.DataMember
        End Get
        Set(ByVal Value As String)
            MyBase.DataMember = Value
        End Set
    End Property

    <NotifyParentProperty(True)> _
     Public Overrides Property AllowCustomPaging() As Boolean
        Get
            Return MyBase.AllowCustomPaging
        End Get
        Set(ByVal Value As Boolean)
            MyBase.AllowCustomPaging = Value
        End Set
    End Property

    <NotifyParentProperty(True)> _
    Public Overrides Property AllowPaging() As Boolean
        Get
            Return MyBase.AllowPaging
        End Get
        Set(ByVal Value As Boolean)
            MyBase.AllowPaging = Value
        End Set
    End Property

    <NotifyParentProperty(True), _
    Browsable(False), EditorBrowsable(EditorBrowsableState.Advanced)> _
    Public Overrides Property AllowSorting() As Boolean
        Get
            Return MyBase.AllowSorting
        End Get
        Set(ByVal Value As Boolean)
            MyBase.AllowSorting = Value
        End Set
    End Property

    <NotifyParentProperty(True)> _
    Public Overrides ReadOnly Property AlternatingItemStyle() As TableItemStyle
        Get
            Return MyBase.AlternatingItemStyle
        End Get
    End Property

    <NotifyParentProperty(True)> _
    Public Overrides Property BackColor() As Color
        Get
            Return MyBase.BackColor
        End Get
        Set(ByVal Value As Color)
            MyBase.BackColor = Value
        End Set
    End Property

    <NotifyParentProperty(True)> _
        Public Overrides Property BackImageUrl() As String
        Get
            Return MyBase.BackImageUrl
        End Get
        Set(ByVal Value As String)
            MyBase.BackImageUrl = Value
        End Set
    End Property

    <NotifyParentProperty(True)> _
    Public Overrides Property BorderColor() As Color
        Get
            Return MyBase.BorderColor
        End Get
        Set(ByVal Value As Color)
            MyBase.BorderColor = Value
        End Set
    End Property

    <NotifyParentProperty(True)> _
    Public Overrides Property BorderStyle() As BorderStyle
        Get
            Return MyBase.BorderStyle
        End Get
        Set(ByVal Value As BorderStyle)
            MyBase.BorderStyle = Value
        End Set
    End Property

    <NotifyParentProperty(True)> _
    Public Overrides Property BorderWidth() As Unit
        Get
            Return MyBase.BorderWidth
        End Get
        Set(ByVal Value As Unit)
            MyBase.BorderWidth = Value
        End Set
    End Property

    <NotifyParentProperty(True)> _
       Public Overrides Property CellPadding() As Integer
        Get
            Return MyBase.CellPadding
        End Get
        Set(ByVal Value As Integer)
            MyBase.CellPadding = Value
        End Set
    End Property

    <NotifyParentProperty(True)> _
    Public Overrides Property CellSpacing() As Integer
        Get
            Return MyBase.CellSpacing
        End Get
        Set(ByVal Value As Integer)
            MyBase.CellSpacing = Value
        End Set
    End Property

    <NotifyParentProperty(True)> _
    Public Overrides ReadOnly Property Columns() As DataGridColumnCollection
        Get
            Return MyBase.Columns
        End Get
    End Property

    <NotifyParentProperty(True)> _
    Public Overrides Property EditItemIndex() As Integer
        Get
            Return MyBase.EditItemIndex
        End Get
        Set(ByVal Value As Integer)
            MyBase.EditItemIndex = Value
        End Set
    End Property

    <NotifyParentProperty(True)> _
    Public Overrides ReadOnly Property EditItemStyle() As TableItemStyle
        Get
            Return MyBase.EditItemStyle
        End Get
    End Property

    <NotifyParentProperty(True)> _
    Public Overrides Property Enabled() As Boolean
        Get
            Return MyBase.Enabled
        End Get
        Set(ByVal Value As Boolean)
            MyBase.Enabled = Value
        End Set
    End Property

    <NotifyParentProperty(True)> _
    Public Overrides ReadOnly Property FooterStyle() As TableItemStyle
        Get
            Return MyBase.FooterStyle
        End Get
    End Property

    <NotifyParentProperty(True)> _
    Public Overrides Property ForeColor() As Color
        Get
            Return MyBase.ForeColor
        End Get
        Set(ByVal Value As Color)
            MyBase.ForeColor = Value
        End Set
    End Property

    <NotifyParentProperty(True)> _
    Public Overrides Property GridLines() As GridLines
        Get
            Return MyBase.GridLines
        End Get
        Set(ByVal Value As GridLines)
            MyBase.GridLines = Value
        End Set
    End Property

    <NotifyParentProperty(True)> _
    Public Overrides ReadOnly Property HeaderStyle() As TableItemStyle
        Get
            Return MyBase.HeaderStyle
        End Get
    End Property

    <NotifyParentProperty(True)> _
    Public Overrides Property Height() As Unit
        Get
            Return MyBase.Height
        End Get
        Set(ByVal Value As Unit)
            MyBase.Height = Value
        End Set
    End Property

    <NotifyParentProperty(True)> _
    Public Overrides Property HorizontalAlign() As HorizontalAlign
        Get
            Return MyBase.HorizontalAlign
        End Get
        Set(ByVal Value As HorizontalAlign)
            MyBase.HorizontalAlign = Value
        End Set
    End Property

    <NotifyParentProperty(True)> _
    Public Overrides ReadOnly Property ItemStyle() As TableItemStyle
        Get
            Return MyBase.ItemStyle
        End Get
    End Property

    <NotifyParentProperty(True)> _
    Public Overrides ReadOnly Property PagerStyle() As DataGridPagerStyle
        Get
            Return MyBase.PagerStyle
        End Get
    End Property

    <NotifyParentProperty(True)> _
    Public Overrides Property PageSize() As Integer
        Get
            Return MyBase.PageSize
        End Get
        Set(ByVal Value As Integer)
            MyBase.PageSize = Value
        End Set
    End Property

    <NotifyParentProperty(True)> _
       Public Overrides Property ShowFooter() As Boolean
        Get
            Return MyBase.ShowFooter
        End Get
        Set(ByVal Value As Boolean)
            MyBase.ShowFooter = Value
        End Set
    End Property

    <NotifyParentProperty(True)> _
           Public Overrides Property ShowHeader() As Boolean
        Get
            Return MyBase.ShowHeader
        End Get
        Set(ByVal Value As Boolean)
            MyBase.ShowHeader = Value
        End Set
    End Property

    <NotifyParentProperty(True)> _
    Public Overrides Property Visible() As Boolean
        Get
            Return MyBase.Visible
        End Get
        Set(ByVal Value As Boolean)
            MyBase.Visible = Value
        End Set
    End Property

    <NotifyParentProperty(True)> _
    Public Overrides Property Width() As Unit
        Get
            Return MyBase.Width
        End Get
        Set(ByVal Value As Unit)
            MyBase.Width = Value
        End Set
    End Property

    <NotifyParentProperty(True)> _
       Public Overrides Property CssClass() As String
        Get
            Return MyBase.CssClass
        End Get
        Set(ByVal Value As String)
            MyBase.CssClass = Value
        End Set
    End Property
#End Region

    <Category("モード選択"), DefaultValue(GetType(Unit), "0px"), _
       Description("スクロールの幅")> _
    Public Property YScrollBarHeight() As Unit
        Get
            Return ymScrollWidth
        End Get

        Set(ByVal Value As Unit)
            ymScrollWidth = Value
        End Set
    End Property

    <Category("モード選択"), DefaultValue(GetType(Unit), "0px"), _
       Description("スクロールの幅")> _
    Public Property XScrollBarWidth() As Unit
        Get
            '設定値を戻す
            Return mScrollWidth
        End Get

        Set(ByVal Value As Unit)
            '設定値
            mScrollWidth = Value
        End Set
    End Property

    <Category("モード選択"), DefaultValue(0), _
       Description("固定行の個数")> _
       Public Property FixedColumns() As Integer
        Get
            If mShowScrollDataMode = ShowScrollDataMode.XScroll Then
                Return mFixedColumns
            End If
        End Get

        Set(ByVal Value As Integer)
            If mShowScrollDataMode = ShowScrollDataMode.XScroll Then
                If Me.Columns.Count < Value And Not Me.Columns.Count = 0 Then
                    mFixedColumns = Me.Columns.Count
                ElseIf Value < 0 Then
                    mFixedColumns = 0
                Else
                    mFixedColumns = Value
                End If
            End If
        End Set
    End Property

    <Category("モード選択"), DefaultValue(GetType(ShowScrollDataMode), "XScroll"), _
     Description("XScroll")> _
        Public Property ShowScrollMode() As ShowScrollDataMode
        Get
            Return mShowScrollDataMode
        End Get
        Set(ByVal Value As ShowScrollDataMode)
            mShowScrollDataMode = Value
        End Set
    End Property
#End Region

    Protected Overrides Sub Render(ByVal writer As System.Web.UI.HtmlTextWriter)
        Dim tmpArrayList As Hashtable

        tmpArrayList = New Hashtable
        tmpArrayList.Add("Z-INDEX", Style.Item("Z-INDEX"))
        tmpArrayList.Add("LEFT", Style.Item("LEFT"))
        tmpArrayList.Add("POSITION", Style.Item("POSITION"))
        tmpArrayList.Add("TOP", Style.Item("TOP"))

        Me.Style.Remove("Z-INDEX")
        Me.Style.Remove("LEFT")
        Me.Style.Remove("POSITION")
        Me.Style.Remove("TOP")

        writer.Write("<div" & " id='" & Me.ID & "_All' style='")
        writer.Write("Z-INDEX: " & tmpArrayList("Z-INDEX") & ";")
        writer.Write("LEFT: " & tmpArrayList("LEFT") & ";")
        writer.Write("POSITION: " & tmpArrayList("POSITION") & ";")
        writer.Write("TOP: " & tmpArrayList("TOP") & "' ms_positioning = 'FlowLayout'>")

        FixedColumnDataGrid(writer)
        writer.Write("</div>")
    End Sub

    Private Sub FixedColumnDataGrid(ByVal output As System.Web.UI.HtmlTextWriter)
        Dim divStyle As String
        Dim widthOne As String
        Dim widthTwo As String
        Dim heightOne As String
        Dim heightTwo As String
        Dim visiableColumn() As Boolean
        Dim k As Integer

        ReDim visiableColumn(Me.Columns.Count - 1)

        For i As Integer = 0 To Me.Columns.Count - 1
            visiableColumn(i) = Me.Columns(i).Visible
        Next

        k = 0

        For i As Integer = 0 To mFixedColumns - 1

            For j As Integer = k To Me.Columns.Count - 1

                If Me.Columns.Item(j).Visible Then

                    widthOne = CStr((Val(widthOne) + Val(Me.Columns(j).HeaderStyle.Width.ToString)))
                    k = j + 1

                    Exit For
                End If
            Next
        Next
        widthOne = CType(Val(widthOne) + 1 + Val(CellSpacing) * mFixedColumns, String)
        If mFixedColumns = 0 Then
            widthOne = "0"
        End If
        widthTwo = mScrollWidth.ToString
        heightTwo = mScrollWidth.ToString
        OutPutLayout(widthOne, widthTwo, heightOne, heightTwo, visiableColumn, output)
    End Sub

    Private Sub OutPutLayout(ByVal widthOne As String, ByVal widthTwo As String, _
    ByVal heightOne As String, ByVal heightTwo As String, _
    ByVal visiableColumn As Boolean(), _
    ByVal output As System.Web.UI.HtmlTextWriter)
        output.Write("<table cellSpacing='0' cellPadding='0' border='0'>")
        If ShowScrollMode = ShowScrollDataMode.None Then
            output.Write("<tr>")
            If Not Me.Columns.Count <= mFixedColumns Then
                Call ShowLeft("", output)
            End If

            output.Write("</tr>")
        ElseIf ShowScrollMode = ShowScrollDataMode.XScroll Then
            output.Write("<tr>")
            Call ShowLeft(widthOne, output)
            If Not Me.Columns.Count <= mFixedColumns Then
                Call ShowRight(widthTwo, output)
            End If
            output.Write("</tr>")
        ElseIf ShowScrollMode = ShowScrollDataMode.YScroll Then
            output.Write("<tr>")
            ShowTop(heightOne, output)
            output.Write("</tr>")
            If Not Me.Columns.Count <= mFixedColumns Then
                output.Write("<tr>")
                Call ShowBottom(heightTwo, output)
                output.Write("</tr>")
            End If
        ElseIf ShowScrollMode = ShowScrollDataMode.Both Then
            output.Write("<tr>")
            ShowTop(heightOne, output)
            output.Write("</tr>")
            If mFixedColumns = 0 Then
                output.Write("<tr>")
                Call BothScroll(heightTwo, output)
                output.Write("</tr>")
            End If
        End If
        For i As Integer = 0 To Me.Columns.Count - 1
            Me.Columns.Item(i).Visible = visiableColumn(i)
        Next
        output.Write("</table>")
    End Sub

    Private Sub BothScroll(ByVal width As String, ByVal output As System.Web.UI.HtmlTextWriter)
        Dim divStyle As String

        output.Write("<td valign='top'>")

        divStyle = "<DIV id='" & Me.ID & "_DivBottom' style='OVERFLOW-Y: scroll; OVERFLOW-X: scroll;HEIGHT: " & _
                            Me.YScrollBarHeight.ToString & "; WIDTH: " & Me.XScrollBarWidth.ToString & ";" & _
                " BORDER-TOP-STYLE: none; BORDER-RIGHT-STYLE: none; BORDER-LEFT-STYLE: none; " & _
                            "' ms_positioning = 'GridLayout' > "
        output.Write(divStyle)

        For i As Integer = 0 To Me.Items.Count - 1
            Me.Items(i).Visible = True
        Next

        For i As Integer = 0 To Me.Items.Count - 1
            Me.Items(i).Attributes("id") = Me.ID & "TRD" & i
        Next

        Me.Style("BORDER-STYLE") = "none"
        Me.ShowHeader = False
        MyBase.Render(output)
        Me.ShowHeader = True

        output.Write("</DIV>")
        output.Write("</td>")
    End Sub

    Private Sub ShowLeft(ByVal width As String, ByVal output As System.Web.UI.HtmlTextWriter)
        Dim divStyle As String
        output.Write("<td valign='top'>")
        divStyle = "<DIV id='" & Me.ID & "_DivLeft' style='OVERFLOW-X: hidden; WIDTH: " & _
                            width & ";" _
                            & " BORDER-TOP-STYLE: none; BORDER-RIGHT-STYLE: none; BORDER-LEFT-STYLE: none; " & _
                            "' ms_positioning = 'GridLayout' > "
        output.Write(divStyle)

        If Me.mShowScrollDataMode = ShowScrollDataMode.None Then
            For i As Integer = 0 To Me.Items.Count - 1
                Me.Items(i).Attributes("id") = Me.ID & "TRD" & i
            Next

        Else
            For i As Integer = 0 To Me.Items.Count - 1
                Me.Items(i).Attributes("id") = Me.ID & "TRL" & i
            Next
        End If
        Me.Style("BORDER-STYLE") = "none"
        MyBase.Render(output)
        output.Write("</DIV>")
        output.Write("</td>")
    End Sub

    Private Sub ShowRight(ByVal width As String, ByVal output As System.Web.UI.HtmlTextWriter)
        Dim divStyle As String

        output.Write("<td valign='top'>")
        divStyle = "<DIV id='" & Me.ID & "_DivRight' style='OVERFLOW-X: scroll; WIDTH: " & _
                            width & ";" & _
                " BORDER-TOP-STYLE: none; BORDER-RIGHT-STYLE: none; BORDER-LEFT-STYLE: none; " & _
                            "' ms_positioning = 'GridLayout' > "
        output.Write(divStyle)
        If Not ShowScrollMode = ShowScrollDataMode.None Then
            For i As Integer = 0 To mFixedColumns - 1
                For j As Integer = 0 To Me.Columns.Count - 1
                    If Me.Columns.Item(j).Visible Then
                        Me.Columns.Item(j).Visible = False
                        Exit For
                    End If
                Next
            Next
        End If

        For i As Integer = 0 To Me.Items.Count - 1
            Me.Items(i).Attributes("id") = Me.ID & "TRR" & i
        Next
        Me.Style("BORDER-STYLE") = "none"
        MyBase.Render(output)
        output.Write("</DIV>")
        output.Write("</td>")
    End Sub

    Private Sub ShowTop(ByVal height As String, ByVal output As System.Web.UI.HtmlTextWriter)

        Dim divStyle As String

        output.Write("<td valign='bottom'>")

        divStyle = "<DIV id='" & Me.ID & "_DivTop' style='BORDER-TOP-STYLE: none; BORDER-RIGHT-STYLE: none; BORDER-LEFT-STYLE: none; " & _
                            "' ms_positioning = 'GridLayout' > "
        output.Write(divStyle)

        For i As Integer = 0 To Me.Items.Count - 1
            Me.Items(i).Visible = False
        Next
        For i As Integer = 0 To Me.Items.Count - 1
            Me.Items(i).Attributes("id") = Me.ID & "TRD" & i
        Next
        Me.Style("BORDER-STYLE") = "none"
        Me.ShowFooter = False
        MyBase.Render(output)
        output.Write("</DIV>")
        output.Write("</td>")
    End Sub

    Private Sub ShowBottom(ByVal height As String, ByVal output As System.Web.UI.HtmlTextWriter)

        Dim divStyle As String

        output.Write("<td valign='top'>")

        divStyle = "<DIV id='" & Me.ID & "_DivBottom' style='OVERFLOW-Y: scroll; HEIGHT: " & _
                            Me.YScrollBarHeight.ToString & ";" & _
                " BORDER-TOP-STYLE: none; BORDER-RIGHT-STYLE: none; BORDER-LEFT-STYLE: none; " & _
                            "' ms_positioning = 'GridLayout' > "
        output.Write(divStyle)

        For i As Integer = 0 To Me.Items.Count - 1
            Me.Items(i).Visible = True
        Next

        For i As Integer = 0 To Me.Items.Count - 1
            Me.Items(i).Attributes("id") = Me.ID & "TRD" & i
        Next

        Me.Style("BORDER-STYLE") = "none"
        Me.ShowHeader = False
        MyBase.Render(output)
        Me.ShowHeader = True

        output.Write("</DIV>")
        output.Write("</td>")
    End Sub
End Class

阅读全文
0 0

相关文章推荐

img
取 消
img