(不好意思,刚才有人要横竖都有,我就改了一下,可以都出现横向和竖向滚动,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
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 "親クラスから継承された表示プロパティ"
Public Overrides Property AutoGenerateColumns() As Boolean
Get
Return MyBase.AutoGenerateColumns
End Get
Set(ByVal Value As Boolean)
MyBase.AutoGenerateColumns = False
End Set
End Property
Public Shadows Property DataMember() As String
Get
Return MyBase.DataMember
End Get
Set(ByVal Value As String)
MyBase.DataMember = Value
End Set
End Property
Public Overrides Property AllowCustomPaging() As Boolean
Get
Return MyBase.AllowCustomPaging
End Get
Set(ByVal Value As Boolean)
MyBase.AllowCustomPaging = Value
End Set
End Property
Public Overrides Property AllowPaging() As Boolean
Get
Return MyBase.AllowPaging
End Get
Set(ByVal Value As Boolean)
MyBase.AllowPaging = Value
End Set
End Property
Public Overrides Property AllowSorting() As Boolean
Get
Return MyBase.AllowSorting
End Get
Set(ByVal Value As Boolean)
MyBase.AllowSorting = Value
End Set
End Property
Public Overrides ReadOnly Property AlternatingItemStyle() As TableItemStyle
Get
Return MyBase.AlternatingItemStyle
End Get
End Property
Public Overrides Property BackColor() As Color
Get
Return MyBase.BackColor
End Get
Set(ByVal Value As Color)
MyBase.BackColor = Value
End Set
End Property
Public Overrides Property BackImageUrl() As String
Get
Return MyBase.BackImageUrl
End Get
Set(ByVal Value As String)
MyBase.BackImageUrl = Value
End Set
End Property
Public Overrides Property BorderColor() As Color
Get
Return MyBase.BorderColor
End Get
Set(ByVal Value As Color)
MyBase.BorderColor = Value
End Set
End Property
Public Overrides Property BorderStyle() As BorderStyle
Get
Return MyBase.BorderStyle
End Get
Set(ByVal Value As BorderStyle)
MyBase.BorderStyle = Value
End Set
End Property
Public Overrides Property BorderWidth() As Unit
Get
Return MyBase.BorderWidth
End Get
Set(ByVal Value As Unit)
MyBase.BorderWidth = Value
End Set
End Property
Public Overrides Property CellPadding() As Integer
Get
Return MyBase.CellPadding
End Get
Set(ByVal Value As Integer)
MyBase.CellPadding = Value
End Set
End Property
Public Overrides Property CellSpacing() As Integer
Get
Return MyBase.CellSpacing
End Get
Set(ByVal Value As Integer)
MyBase.CellSpacing = Value
End Set
End Property
Public Overrides ReadOnly Property Columns() As DataGridColumnCollection
Get
Return MyBase.Columns
End Get
End Property
Public Overrides Property EditItemIndex() As Integer
Get
Return MyBase.EditItemIndex
End Get
Set(ByVal Value As Integer)
MyBase.EditItemIndex = Value
End Set
End Property
Public Overrides ReadOnly Property EditItemStyle() As TableItemStyle
Get
Return MyBase.EditItemStyle
End Get
End Property
Public Overrides Property Enabled() As Boolean
Get
Return MyBase.Enabled
End Get
Set(ByVal Value As Boolean)
MyBase.Enabled = Value
End Set
End Property
Public Overrides ReadOnly Property FooterStyle() As TableItemStyle
Get
Return MyBase.FooterStyle
End Get
End Property
Public Overrides Property ForeColor() As Color
Get
Return MyBase.ForeColor
End Get
Set(ByVal Value As Color)
MyBase.ForeColor = Value
End Set
End Property
Public Overrides Property GridLines() As GridLines
Get
Return MyBase.GridLines
End Get
Set(ByVal Value As GridLines)
MyBase.GridLines = Value
End Set
End Property
Public Overrides ReadOnly Property HeaderStyle() As TableItemStyle
Get
Return MyBase.HeaderStyle
End Get
End Property
Public Overrides Property Height() As Unit
Get
Return MyBase.Height
End Get
Set(ByVal Value As Unit)
MyBase.Height = Value
End Set
End Property
Public Overrides Property HorizontalAlign() As HorizontalAlign
Get
Return MyBase.HorizontalAlign
End Get
Set(ByVal Value As HorizontalAlign)
MyBase.HorizontalAlign = Value
End Set
End Property
Public Overrides ReadOnly Property ItemStyle() As TableItemStyle
Get
Return MyBase.ItemStyle
End Get
End Property
Public Overrides ReadOnly Property PagerStyle() As DataGridPagerStyle
Get
Return MyBase.PagerStyle
End Get
End Property
Public Overrides Property PageSize() As Integer
Get
Return MyBase.PageSize
End Get
Set(ByVal Value As Integer)
MyBase.PageSize = Value
End Set
End Property
Public Overrides Property ShowFooter() As Boolean
Get
Return MyBase.ShowFooter
End Get
Set(ByVal Value As Boolean)
MyBase.ShowFooter = Value
End Set
End Property
Public Overrides Property ShowHeader() As Boolean
Get
Return MyBase.ShowHeader
End Get
Set(ByVal Value As Boolean)
MyBase.ShowHeader = Value
End Set
End Property
Public Overrides Property Visible() As Boolean
Get
Return MyBase.Visible
End Get
Set(ByVal Value As Boolean)
MyBase.Visible = Value
End Set
End Property
Public Overrides Property Width() As Unit
Get
Return MyBase.Width
End Get
Set(ByVal Value As Unit)
MyBase.Width = Value
End Set
End Property
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
Public Property YScrollBarHeight() As Unit
Get
Return ymScrollWidth
End Get
Set(ByVal Value As Unit)
ymScrollWidth = Value
End Set
End Property
Public Property XScrollBarWidth() As Unit
Get
'設定値を戻す
Return mScrollWidth
End Get
Set(ByVal Value As Unit)
'設定値
mScrollWidth = Value
End Set
End Property
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
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(" FixedColumnDataGrid(writer)
writer.Write("
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, _ output.Write("
ByVal heightOne As String, ByVal heightTwo As String, _
ByVal visiableColumn As Boolean(), _
ByVal output As System.Web.UI.HtmlTextWriter)
output.Write("")
")
If ShowScrollMode = ShowScrollDataMode.None Then
output.Write("") ")
If Not Me.Columns.Count <= mFixedColumns Then
Call ShowLeft("", output)
End If
ElseIf ShowScrollMode = ShowScrollDataMode.XScroll Then
output.Write("") ")
Call ShowLeft(widthOne, output)
If Not Me.Columns.Count <= mFixedColumns Then
Call ShowRight(widthTwo, output)
End If
output.Write("
ElseIf ShowScrollMode = ShowScrollDataMode.YScroll Then
output.Write("") ")
ShowTop(heightOne, output)
output.Write("
If Not Me.Columns.Count <= mFixedColumns Then
output.Write("") ")
Call ShowBottom(heightTwo, output)
output.Write("
End If
ElseIf ShowScrollMode = ShowScrollDataMode.Both Then
output.Write("") ")
ShowTop(heightOne, output)
output.Write("
If mFixedColumns = 0 Then
output.Write("") ")
Call BothScroll(heightTwo, output)
output.Write("
End If
End If
For i As Integer = 0 To Me.Columns.Count - 1
Me.Columns.Item(i).Visible = visiableColumn(i)
Next
output.Write("
End Sub
Private Sub BothScroll(ByVal width As String, ByVal output As System.Web.UI.HtmlTextWriter)
Dim divStyle As String
output.Write("
divStyle = " For i As Integer = 0 To Me.Items.Count - 1 For i As Integer = 0 To Me.Items.Count - 1 Me.Style("BORDER-STYLE") = "none" output.Write("
output.Write(divStyle)
Me.Items(i).Visible = True
Next
Me.Items(i).Attributes("id") = Me.ID & "TRD" & i
Next
Me.ShowHeader = False
MyBase.Render(output)
Me.ShowHeader = True
output.Write("
Private Sub ShowLeft(ByVal width As String, ByVal output As System.Web.UI.HtmlTextWriter)
Dim divStyle As String
output.Write("
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("
Private Sub ShowRight(ByVal width As String, ByVal output As System.Web.UI.HtmlTextWriter)
Dim divStyle As String
output.Write("
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("
Private Sub ShowTop(ByVal height As String, ByVal output As System.Web.UI.HtmlTextWriter)
Dim divStyle As String
output.Write("
divStyle = " For i As Integer = 0 To Me.Items.Count - 1
output.Write(divStyle)
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("
output.Write("
Private Sub ShowBottom(ByVal height As String, ByVal output As System.Web.UI.HtmlTextWriter)
Dim divStyle As String
output.Write("
divStyle = " For i As Integer = 0 To Me.Items.Count - 1 For i As Integer = 0 To Me.Items.Count - 1 Me.Style("BORDER-STYLE") = "none" output.Write("
output.Write(divStyle)
Me.Items(i).Visible = True
Next
Me.Items(i).Attributes("id") = Me.ID & "TRD" & i
Next
Me.ShowHeader = False
MyBase.Render(output)
Me.ShowHeader = True
output.Write("