Ok, there is additional code that can be added to make this and feel more effiecient, but this gets the job done.
In your customize datagrid control override the horizontal scrollbar events with a class like this:
Protected Overrides Sub GridHScrolled(ByVal sender As Object, ByVal se As System.Windows.Forms.ScrollEventArgs)
Select Case se.Type
Case ScrollEventType.SmallIncrement, ScrollEventType.LargeIncrement
Dim SumWidth = 0
For i As Integer = _ColumnNumber + 1 To MyBase.TableStyles(0).GridColumnStyles.Count - 1
If MyBase.TableStyles(0).GridColumnStyles(i).Width > 0 Then
MyBase.TableStyles(0).GridColumnStyles(i).Width = 0
Exit For
End If
SumWidth += MyBase.TableStyles(0).GridColumnStyles(i).Width
Next
MyBase.TableStyles(0).GridColumnStyles(MyBase.TableStyles(0).GridColumnStyles.Count - 1).Width() += MyBase.TableStyles(0).DataGrid().Width - SumWidth
Case ScrollEventType.SmallDecrement, ScrollEventType.LargeDecrement
For i As Integer = MyBase.TableStyles(0).GridColumnStyles.Count - 1 To 1 Step -1
If MyBase.TableStyles(0).GridColumnStyles(i).Width = 0 Then
MyBase.TableStyles(0).GridColumnStyles(i).Width = 100
Exit For
End If
Next
Case ScrollEventType.EndScroll, ScrollEventType.First, ScrollEventType.Last, ScrollEventType.ThumbPosition, ScrollEventType.ThumbTrack
MyBase.GridHScrolled(sender, se)
End Select
Else
MyBase.GridHScrolled(sender, se)
End If
End Sub
3 comments:
Hi,
I wanted to implement the code for freezing columns in a datagrid. It doesn't work. It compiles but doesn't do anything.
What is the variable _ColumnNumber used for. If I assign '0', the code compiles and theerr are no runtime errors but doesn't freeze the column though. If I use any other value, I get 'index out of range exception'
The _ColumnNumber is a variable:
Private _ColumnNumber As Integer = 0
Which I use to set what column I want to use to freeze by using a property:
Public Property FirstColumn() As Integer
Get
Return _ColumnNumber
End Get
Set(ByVal Value As Integer)
_ColumnNumber = Value
End Set
End Property
In my situation, I didn't always want to freeze the very first column since sometimes I have dummy columns. Hope that helps, sorry for the delay.
Hi Andrus,
This is Viv. I also have a requirement where I want to frezze first 3-4 columns of the GRID. I am using VS 2003 and VB.net. I read your code and comment but frankly I am not sure on how to implement this. If you could send me your custom GRID code or some hints on using the code, I will really appreciate it. Thanks.
Post a Comment