473,587 Members | 2,490 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Synchronising two datagrids together.

I want to obtain the appearance of frozen columns as in an excel sheet so
thet the first two columns of the table remain in view at all times.

Example - I have a table of products - listed by Item and Line.
I want the Item and Line columns to remain visible whilst being able to
scroll the rest of the grid across to reveal other columns.
Ialso need the information to be present and synchronised with the rows
when I scroll down.
All the data is held in one data table - and all I need to do is freeze the
first two columns of the datagrid, but I am informed that the datagrid does
not have this functionality.

Someone came up with the ides of having two datagrids - side by side - and
showing the first two columns (of the data table) in the first grid, with
the rest of the columns ( in the data table) being shown in the second grid.
This idea would be fine, but I cannot get the two grids to scroll down
together as if bound together.

Can anyone please come up with a means of facilitating this idea - or
suggest another means.

I am using VB.NET 2002 Standard edition.

Thanks in advance

Terry


Nov 21 '05 #1
3 1326
Hi,

Here is a column style that locks a column on the datagrid. It is
still under development. basically if you set the locked property to true
it places a panel over the datagrid that has the locked column drawn on it.
Currently it only will lock the first column.

Imports System.Drawing. Drawing2D

Public Class ColoredGridColu mn

Inherits DataGridTextBox Column

Dim mForeColor As Color = Color.Black

Dim mBackColor As Color = Color.White

Dim WithEvents mctrl As DblBufferPanel

Dim WithEvents dg As DataGrid

Dim pt As New Point

Dim bPanelOnly As Boolean = False

Dim cm As CurrencyManager

Dim ar As Boolean

Private Class DblBufferPanel

Inherits Panel

Public Sub New()

Me.SetStyle(Con trolStyles.Doub leBuffer, True)

End Sub

End Class

Public Property ForeColor() As Color

Get

Return mForeColor

End Get

Set(ByVal Value As Color)

mForeColor = Value

End Set

End Property

Public Property BackColor() As Color

Get

Return mBackColor

End Get

Set(ByVal Value As Color)

mBackColor = Value

End Set

End Property

Public Property Locked() As Boolean

Get

Return Not mctrl Is Nothing

End Get

Set(ByVal Value As Boolean)

If Value = False Then

mctrl = Nothing

Else

mctrl = New DblBufferPanel

End If

End Set

End Property

Protected Overloads Overrides Sub Paint(ByVal g As System.Drawing. Graphics,
ByVal bounds As System.Drawing. Rectangle, ByVal source As
System.Windows. Forms.CurrencyM anager, ByVal rowNum As Integer, ByVal
backBrush As System.Drawing. Brush, ByVal foreBrush As System.Drawing. Brush,
ByVal alignToRight As Boolean)

Dim brFore As Brush

Dim brBack As Brush

Dim cFore As Color

Dim cBack As Color

Static bPainted As Boolean = False

If Not bPainted And Not (mctrl Is Nothing) Then

dg = Me.DataGridTabl eStyle.DataGrid

dg.Parent.Contr ols.Add(mctrl)

MovePanel()

mctrl.BringToFr ont()

pt = dg.GetCellBound s(0, 0).Location

If TypeOf dg.DataSource Is DataTable Then

AddHandler DirectCast(dg.D ataSource, DataTable).Defa ultView.ListCha nged,
AddressOf dv_ListChanged

ElseIf TypeOf dg.DataSource Is DataView Then

AddHandler DirectCast(dg.D ataSource, DataView).ListC hanged, AddressOf
dv_ListChanged

End If

End If

cm = source

ar = alignToRight

bPainted = True

If Me.DataGridTabl eStyle.DataGrid .IsSelected(row Num) Then

cFore = Me.DataGridTabl eStyle.Selectio nForeColor

cBack = Me.DataGridTabl eStyle.Selectio nBackColor

Else

cFore = ForeColor

cBack = BackColor

End If

brBack = New LinearGradientB rush(bounds, cBack, Color.White, 90, False)

brFore = New SolidBrush(cFor e)

Dim bl As New Blend

bl.Factors = New Single() {0.0F, 0.1F, 0.5F, 0.7F, 0.7F, 0.5F, 0.3F, 0.2F,
0}

bl.Positions = New Single() {0, 0.1F, 0.2F, 0.5F, 0.6F, 0.7F, 0.8F, 0.9F,
1.0F}

DirectCast(brBa ck, LinearGradientB rush).Blend = bl

If Not bPanelOnly Then

MyBase.Paint(g, bounds, source, rowNum, brBack, brFore, alignToRight)

End If

If Not (mctrl Is Nothing) Then

' if there is another control to draw on move the bounds to the right edge
if htere is not then it will ignore that directive right? yesgot oitf the
control

'mctrl.Backgrou ndImage = bm

PaintRow(mctrl. CreateGraphics, bounds, rowNum)

End If

If Me.GetColumnVal ueAtRow(source, rowNum).ToStrin g = "Davolio" Then

Me.DataGridTabl eStyle.DataGrid .Select(rowNum)

End If

End Sub

Public Shadows Sub BeginUpdate()

MyBase.BeginUpd ate()

End Sub

Public Shadows Sub EndUpdate()

MyBase.EndUpdat e()

End Sub

Protected Overloads Overrides Sub Edit(ByVal source As
System.Windows. Forms.CurrencyM anager, ByVal rowNum As Integer, ByVal bounds
As System.Drawing. Rectangle, ByVal [readOnly] As Boolean, ByVal instantText
As String, ByVal cellIsVisible As Boolean)

MyBase.Edit(sou rce, rowNum, bounds, [readOnly], instantText, cellIsVisible)

MyBase.TextBox. ForeColor = ForeColor()

MyBase.TextBox. BackColor = BackColor

End Sub

Public Sub New()

End Sub

Private Sub dg_Scroll(ByVal sender As Object, ByVal e As System.EventArg s)
Handles dg.Scroll

Trace.WriteLine ("Scroll")

RedrawPanel()

End Sub

Private Sub MovePanel()

mctrl.Location = New Point(dg.Left + dg.RowHeaderWid th + 2, dg.Top + 21)

Dim intFactor As Integer = SystemInformati on.HorizontalSc rollBarHeight

For Each ctrl As Control In Me.DataGridTabl eStyle.DataGrid .Controls

If TypeOf ctrl Is HScrollBar Then

If Not DirectCast(ctrl , HScrollBar).Vis ible Then intFactor = 0

End If

Next

mctrl.Height = dg.Height - 21 - intFactor - 2

End Sub

Private Sub dg_Move(ByVal sender As Object, ByVal e As System.EventArg s)
Handles dg.Move

MovePanel()

End Sub

Private Sub PreparePanel()

Dim sf As New StringFormat

sf.LineAlignmen t = StringAlignment .Center

Dim rDraw As New RectangleF(0, 0, Me.Width, 20)

Dim g As Graphics = mctrl.CreateGra phics

g.Clear(mctrl.B ackColor)

Try

g.DrawString(Me .HeaderText, dg.Font, Brushes.Black, rDraw, sf)

ControlPaint.Dr awBorder3D(g, _

New Rectangle(0, 0, Me.Width, 19), Border3DStyle.R aisedInner)

Catch

End Try

End Sub

Private Sub dv_ListChanged( ByVal sender As Object, ByVal e As
System.Componen tModel.ListChan gedEventArgs)

'PreparePanel()

RedrawPanel()

End Sub

Private Sub dg_VisibleChang ed(ByVal sender As Object, ByVal e As
System.EventArg s) Handles dg.VisibleChang ed

Try

mctrl.Visible = dg.Visible

Catch ex As Exception

End Try

End Sub

Private Sub dg_SizeChanged( ByVal sender As Object, ByVal e As
System.EventArg s) Handles dg.SizeChanged

MovePanel()

RedrawPanel()

End Sub

Private Sub RedrawPanel()

Static oldTop As Integer = 0

If Not mctrl Is Nothing And dg.VisibleRowCo unt > 0 Then

Application.DoE vents()

Dim hti As DataGrid.HitTes tInfo = dg.HitTest(pt)

Dim newRow As Integer = hti.Row

Trace.WriteLine (String.Format( "First Row {0} Visible rows {1}", hti.Row,
dg.VisibleRowCo unt))

oldTop = newRow

bPanelOnly = True

For x As Integer = oldTop To oldTop + dg.VisibleRowCo unt - 1

If x < cm.Count Then

Trace.WriteLine (String.Format( "Drawing Row {0} {1}", x, dg.GetCellBound s(x,
dg.FirstVisible Column)))

Paint(Nothing, dg.GetCellBound s(x, dg.FirstVisible Column), _

cm, x, Nothing, Nothing, ar)

End If

Next

bPanelOnly = False

End If

End Sub

Private Sub dg_Resize(ByVal sender As Object, ByVal e As System.EventArg s)
Handles dg.Resize

MovePanel()

End Sub

Private Sub mctrl_Paint(ByV al sender As Object, ByVal e As
System.Windows. Forms.PaintEven tArgs) Handles mctrl.Paint

Dim sf As New StringFormat

sf.LineAlignmen t = StringAlignment .Center

Dim rDraw As New RectangleF(0, 0, Me.Width, 20)

Dim g As Graphics = e.Graphics

g.Clear(mctrl.B ackColor)

Try

g.DrawString(Me .HeaderText, dg.Font, Brushes.Black, rDraw, sf)

ControlPaint.Dr awBorder3D(g, _

New Rectangle(0, 0, Me.Width, 19), Border3DStyle.R aisedInner)

Debug.WriteLine ("Panel Paint")

Dim hti As DataGrid.HitTes tInfo = dg.HitTest(pt)

Dim newRow As Integer = hti.Row

Dim oldTop As Integer

Trace.WriteLine (String.Format( "First Row {0} Visible rows {1}", hti.Row,
dg.VisibleRowCo unt))

oldTop = newRow

bPanelOnly = True

For x As Integer = oldTop To oldTop + dg.VisibleRowCo unt - 1

If x < cm.Count Then

Trace.WriteLine (String.Format( "Drawing Row {0} {1}", x, dg.GetCellBound s(x,
dg.FirstVisible Column)))

Paint(Nothing, dg.GetCellBound s(x, dg.FirstVisible Column), _

cm, x, Nothing, Nothing, ar)

End If

Next

Catch

End Try

End Sub

Private Sub PaintRow(ByVal g As Graphics, ByVal bounds As Rectangle, ByVal
rownum As Integer)

Dim brFore As Brush

Dim brBack As Brush

Dim cFore As Color

Dim cBack As Color

Dim bounds2 As New Rectangle(0, bounds.Y - 21, bounds.Width, bounds.Height)

If Me.DataGridTabl eStyle.DataGrid .IsSelected(row num) Then

cFore = Me.DataGridTabl eStyle.Selectio nForeColor

cBack = Me.DataGridTabl eStyle.Selectio nBackColor

Else

cFore = ForeColor

cBack = BackColor

End If

brBack = New LinearGradientB rush(bounds, cBack, Color.White, 90, False)

brFore = New SolidBrush(cFor e)

Dim bl As New Blend

bl.Factors = New Single() {0.0F, 0.1F, 0.5F, 0.7F, 0.7F, 0.5F, 0.3F, 0.2F,
0}

bl.Positions = New Single() {0, 0.1F, 0.2F, 0.5F, 0.6F, 0.7F, 0.8F, 0.9F,
1.0F}

DirectCast(brBa ck, LinearGradientB rush).Blend = bl

If mctrl.Width <> Me.Width Then mctrl.Width = Me.Width

MyBase.Paint(g, bounds2, cm, rownum, _

brBack, brFore, ar)

If rownum = cm.Count - 1 Then

Dim br As New SolidBrush(Me.D ataGridTableSty le.DataGrid.Bac kgroundColor)

g.FillRectangle (br, 0, bounds2.Bottom + 1, mctrl.Width, _

mctrl.Height - bounds2.Bottom - 1)

End If

End Sub

End Class

Ken

-------------------------------
"anon" <ng*@tdrd.frees erve.co.uk> wrote in message
news:d1******** **@newsg3.svr.p ol.co.uk...
I want to obtain the appearance of frozen columns as in an excel sheet so
thet the first two columns of the table remain in view at all times.

Example - I have a table of products - listed by Item and Line.
I want the Item and Line columns to remain visible whilst being able to
scroll the rest of the grid across to reveal other columns.
Ialso need the information to be present and synchronised with the rows
when I scroll down.
All the data is held in one data table - and all I need to do is freeze the
first two columns of the datagrid, but I am informed that the datagrid does
not have this functionality.

Someone came up with the ides of having two datagrids - side by side - and
showing the first two columns (of the data table) in the first grid, with
the rest of the columns ( in the data table) being shown in the second grid.
This idea would be fine, but I cannot get the two grids to scroll down
together as if bound together.

Can anyone please come up with a means of facilitating this idea - or
suggest another means.

I am using VB.NET 2002 Standard edition.

Thanks in advance

Terry

Nov 21 '05 #2
Hi,

Here is a column style that locks a column on the datagrid. It is
still under development. basically if you set the locked property to true
it places a panel over the datagrid that has the locked column drawn on it.
Currently it only will lock the first column.

Imports System.Drawing. Drawing2D

Public Class ColoredGridColu mn

Inherits DataGridTextBox Column

Dim mForeColor As Color = Color.Black

Dim mBackColor As Color = Color.White

Dim WithEvents mctrl As DblBufferPanel

Dim WithEvents dg As DataGrid

Dim pt As New Point

Dim bPanelOnly As Boolean = False

Dim cm As CurrencyManager

Dim ar As Boolean

Private Class DblBufferPanel

Inherits Panel

Public Sub New()

Me.SetStyle(Con trolStyles.Doub leBuffer, True)

End Sub

End Class

Public Property ForeColor() As Color

Get

Return mForeColor

End Get

Set(ByVal Value As Color)

mForeColor = Value

End Set

End Property

Public Property BackColor() As Color

Get

Return mBackColor

End Get

Set(ByVal Value As Color)

mBackColor = Value

End Set

End Property

Public Property Locked() As Boolean

Get

Return Not mctrl Is Nothing

End Get

Set(ByVal Value As Boolean)

If Value = False Then

mctrl = Nothing

Else

mctrl = New DblBufferPanel

End If

End Set

End Property

Protected Overloads Overrides Sub Paint(ByVal g As System.Drawing. Graphics,
ByVal bounds As System.Drawing. Rectangle, ByVal source As
System.Windows. Forms.CurrencyM anager, ByVal rowNum As Integer, ByVal
backBrush As System.Drawing. Brush, ByVal foreBrush As System.Drawing. Brush,
ByVal alignToRight As Boolean)

Dim brFore As Brush

Dim brBack As Brush

Dim cFore As Color

Dim cBack As Color

Static bPainted As Boolean = False

If Not bPainted And Not (mctrl Is Nothing) Then

dg = Me.DataGridTabl eStyle.DataGrid

dg.Parent.Contr ols.Add(mctrl)

MovePanel()

mctrl.BringToFr ont()

pt = dg.GetCellBound s(0, 0).Location

If TypeOf dg.DataSource Is DataTable Then

AddHandler DirectCast(dg.D ataSource, DataTable).Defa ultView.ListCha nged,
AddressOf dv_ListChanged

ElseIf TypeOf dg.DataSource Is DataView Then

AddHandler DirectCast(dg.D ataSource, DataView).ListC hanged, AddressOf
dv_ListChanged

End If

End If

cm = source

ar = alignToRight

bPainted = True

If Me.DataGridTabl eStyle.DataGrid .IsSelected(row Num) Then

cFore = Me.DataGridTabl eStyle.Selectio nForeColor

cBack = Me.DataGridTabl eStyle.Selectio nBackColor

Else

cFore = ForeColor

cBack = BackColor

End If

brBack = New LinearGradientB rush(bounds, cBack, Color.White, 90, False)

brFore = New SolidBrush(cFor e)

Dim bl As New Blend

bl.Factors = New Single() {0.0F, 0.1F, 0.5F, 0.7F, 0.7F, 0.5F, 0.3F, 0.2F,
0}

bl.Positions = New Single() {0, 0.1F, 0.2F, 0.5F, 0.6F, 0.7F, 0.8F, 0.9F,
1.0F}

DirectCast(brBa ck, LinearGradientB rush).Blend = bl

If Not bPanelOnly Then

MyBase.Paint(g, bounds, source, rowNum, brBack, brFore, alignToRight)

End If

If Not (mctrl Is Nothing) Then

' if there is another control to draw on move the bounds to the right edge
if htere is not then it will ignore that directive right? yesgot oitf the
control

'mctrl.Backgrou ndImage = bm

PaintRow(mctrl. CreateGraphics, bounds, rowNum)

End If

If Me.GetColumnVal ueAtRow(source, rowNum).ToStrin g = "Davolio" Then

Me.DataGridTabl eStyle.DataGrid .Select(rowNum)

End If

End Sub

Public Shadows Sub BeginUpdate()

MyBase.BeginUpd ate()

End Sub

Public Shadows Sub EndUpdate()

MyBase.EndUpdat e()

End Sub

Protected Overloads Overrides Sub Edit(ByVal source As
System.Windows. Forms.CurrencyM anager, ByVal rowNum As Integer, ByVal bounds
As System.Drawing. Rectangle, ByVal [readOnly] As Boolean, ByVal instantText
As String, ByVal cellIsVisible As Boolean)

MyBase.Edit(sou rce, rowNum, bounds, [readOnly], instantText, cellIsVisible)

MyBase.TextBox. ForeColor = ForeColor()

MyBase.TextBox. BackColor = BackColor

End Sub

Public Sub New()

End Sub

Private Sub dg_Scroll(ByVal sender As Object, ByVal e As System.EventArg s)
Handles dg.Scroll

Trace.WriteLine ("Scroll")

RedrawPanel()

End Sub

Private Sub MovePanel()

mctrl.Location = New Point(dg.Left + dg.RowHeaderWid th + 2, dg.Top + 21)

Dim intFactor As Integer = SystemInformati on.HorizontalSc rollBarHeight

For Each ctrl As Control In Me.DataGridTabl eStyle.DataGrid .Controls

If TypeOf ctrl Is HScrollBar Then

If Not DirectCast(ctrl , HScrollBar).Vis ible Then intFactor = 0

End If

Next

mctrl.Height = dg.Height - 21 - intFactor - 2

End Sub

Private Sub dg_Move(ByVal sender As Object, ByVal e As System.EventArg s)
Handles dg.Move

MovePanel()

End Sub

Private Sub PreparePanel()

Dim sf As New StringFormat

sf.LineAlignmen t = StringAlignment .Center

Dim rDraw As New RectangleF(0, 0, Me.Width, 20)

Dim g As Graphics = mctrl.CreateGra phics

g.Clear(mctrl.B ackColor)

Try

g.DrawString(Me .HeaderText, dg.Font, Brushes.Black, rDraw, sf)

ControlPaint.Dr awBorder3D(g, _

New Rectangle(0, 0, Me.Width, 19), Border3DStyle.R aisedInner)

Catch

End Try

End Sub

Private Sub dv_ListChanged( ByVal sender As Object, ByVal e As
System.Componen tModel.ListChan gedEventArgs)

'PreparePanel()

RedrawPanel()

End Sub

Private Sub dg_VisibleChang ed(ByVal sender As Object, ByVal e As
System.EventArg s) Handles dg.VisibleChang ed

Try

mctrl.Visible = dg.Visible

Catch ex As Exception

End Try

End Sub

Private Sub dg_SizeChanged( ByVal sender As Object, ByVal e As
System.EventArg s) Handles dg.SizeChanged

MovePanel()

RedrawPanel()

End Sub

Private Sub RedrawPanel()

Static oldTop As Integer = 0

If Not mctrl Is Nothing And dg.VisibleRowCo unt > 0 Then

Application.DoE vents()

Dim hti As DataGrid.HitTes tInfo = dg.HitTest(pt)

Dim newRow As Integer = hti.Row

Trace.WriteLine (String.Format( "First Row {0} Visible rows {1}", hti.Row,
dg.VisibleRowCo unt))

oldTop = newRow

bPanelOnly = True

For x As Integer = oldTop To oldTop + dg.VisibleRowCo unt - 1

If x < cm.Count Then

Trace.WriteLine (String.Format( "Drawing Row {0} {1}", x, dg.GetCellBound s(x,
dg.FirstVisible Column)))

Paint(Nothing, dg.GetCellBound s(x, dg.FirstVisible Column), _

cm, x, Nothing, Nothing, ar)

End If

Next

bPanelOnly = False

End If

End Sub

Private Sub dg_Resize(ByVal sender As Object, ByVal e As System.EventArg s)
Handles dg.Resize

MovePanel()

End Sub

Private Sub mctrl_Paint(ByV al sender As Object, ByVal e As
System.Windows. Forms.PaintEven tArgs) Handles mctrl.Paint

Dim sf As New StringFormat

sf.LineAlignmen t = StringAlignment .Center

Dim rDraw As New RectangleF(0, 0, Me.Width, 20)

Dim g As Graphics = e.Graphics

g.Clear(mctrl.B ackColor)

Try

g.DrawString(Me .HeaderText, dg.Font, Brushes.Black, rDraw, sf)

ControlPaint.Dr awBorder3D(g, _

New Rectangle(0, 0, Me.Width, 19), Border3DStyle.R aisedInner)

Debug.WriteLine ("Panel Paint")

Dim hti As DataGrid.HitTes tInfo = dg.HitTest(pt)

Dim newRow As Integer = hti.Row

Dim oldTop As Integer

Trace.WriteLine (String.Format( "First Row {0} Visible rows {1}", hti.Row,
dg.VisibleRowCo unt))

oldTop = newRow

bPanelOnly = True

For x As Integer = oldTop To oldTop + dg.VisibleRowCo unt - 1

If x < cm.Count Then

Trace.WriteLine (String.Format( "Drawing Row {0} {1}", x, dg.GetCellBound s(x,
dg.FirstVisible Column)))

Paint(Nothing, dg.GetCellBound s(x, dg.FirstVisible Column), _

cm, x, Nothing, Nothing, ar)

End If

Next

Catch

End Try

End Sub

Private Sub PaintRow(ByVal g As Graphics, ByVal bounds As Rectangle, ByVal
rownum As Integer)

Dim brFore As Brush

Dim brBack As Brush

Dim cFore As Color

Dim cBack As Color

Dim bounds2 As New Rectangle(0, bounds.Y - 21, bounds.Width, bounds.Height)

If Me.DataGridTabl eStyle.DataGrid .IsSelected(row num) Then

cFore = Me.DataGridTabl eStyle.Selectio nForeColor

cBack = Me.DataGridTabl eStyle.Selectio nBackColor

Else

cFore = ForeColor

cBack = BackColor

End If

brBack = New LinearGradientB rush(bounds, cBack, Color.White, 90, False)

brFore = New SolidBrush(cFor e)

Dim bl As New Blend

bl.Factors = New Single() {0.0F, 0.1F, 0.5F, 0.7F, 0.7F, 0.5F, 0.3F, 0.2F,
0}

bl.Positions = New Single() {0, 0.1F, 0.2F, 0.5F, 0.6F, 0.7F, 0.8F, 0.9F,
1.0F}

DirectCast(brBa ck, LinearGradientB rush).Blend = bl

If mctrl.Width <> Me.Width Then mctrl.Width = Me.Width

MyBase.Paint(g, bounds2, cm, rownum, _

brBack, brFore, ar)

If rownum = cm.Count - 1 Then

Dim br As New SolidBrush(Me.D ataGridTableSty le.DataGrid.Bac kgroundColor)

g.FillRectangle (br, 0, bounds2.Bottom + 1, mctrl.Width, _

mctrl.Height - bounds2.Bottom - 1)

End If

End Sub

End Class

Ken

-------------------------------
"anon" <ng*@tdrd.frees erve.co.uk> wrote in message
news:d1******** **@newsg3.svr.p ol.co.uk...
I want to obtain the appearance of frozen columns as in an excel sheet so
thet the first two columns of the table remain in view at all times.

Example - I have a table of products - listed by Item and Line.
I want the Item and Line columns to remain visible whilst being able to
scroll the rest of the grid across to reveal other columns.
Ialso need the information to be present and synchronised with the rows
when I scroll down.
All the data is held in one data table - and all I need to do is freeze the
first two columns of the datagrid, but I am informed that the datagrid does
not have this functionality.

Someone came up with the ides of having two datagrids - side by side - and
showing the first two columns (of the data table) in the first grid, with
the rest of the columns ( in the data table) being shown in the second grid.
This idea would be fine, but I cannot get the two grids to scroll down
together as if bound together.

Can anyone please come up with a means of facilitating this idea - or
suggest another means.

I am using VB.NET 2002 Standard edition.

Thanks in advance

Terry

Nov 21 '05 #3
thanks for that feedback - it's a starting point.
Terry


"Ken Tucker [MVP]" <vb***@bellsout h.net> wrote in message
news:u2******** ******@tk2msftn gp13.phx.gbl...
Hi,

Here is a column style that locks a column on the datagrid. It is
still under development. basically if you set the locked property to true
it places a panel over the datagrid that has the locked column drawn on it. Currently it only will lock the first column.

Imports System.Drawing. Drawing2D

Public Class ColoredGridColu mn

Inherits DataGridTextBox Column

Dim mForeColor As Color = Color.Black

Dim mBackColor As Color = Color.White

Dim WithEvents mctrl As DblBufferPanel

Dim WithEvents dg As DataGrid

Dim pt As New Point

Dim bPanelOnly As Boolean = False

Dim cm As CurrencyManager

Dim ar As Boolean

Private Class DblBufferPanel

Inherits Panel

Public Sub New()

Me.SetStyle(Con trolStyles.Doub leBuffer, True)

End Sub

End Class

Public Property ForeColor() As Color

Get

Return mForeColor

End Get

Set(ByVal Value As Color)

mForeColor = Value

End Set

End Property

Public Property BackColor() As Color

Get

Return mBackColor

End Get

Set(ByVal Value As Color)

mBackColor = Value

End Set

End Property

Public Property Locked() As Boolean

Get

Return Not mctrl Is Nothing

End Get

Set(ByVal Value As Boolean)

If Value = False Then

mctrl = Nothing

Else

mctrl = New DblBufferPanel

End If

End Set

End Property

Protected Overloads Overrides Sub Paint(ByVal g As System.Drawing. Graphics, ByVal bounds As System.Drawing. Rectangle, ByVal source As
System.Windows. Forms.CurrencyM anager, ByVal rowNum As Integer, ByVal
backBrush As System.Drawing. Brush, ByVal foreBrush As System.Drawing. Brush, ByVal alignToRight As Boolean)

Dim brFore As Brush

Dim brBack As Brush

Dim cFore As Color

Dim cBack As Color

Static bPainted As Boolean = False

If Not bPainted And Not (mctrl Is Nothing) Then

dg = Me.DataGridTabl eStyle.DataGrid

dg.Parent.Contr ols.Add(mctrl)

MovePanel()

mctrl.BringToFr ont()

pt = dg.GetCellBound s(0, 0).Location

If TypeOf dg.DataSource Is DataTable Then

AddHandler DirectCast(dg.D ataSource, DataTable).Defa ultView.ListCha nged,
AddressOf dv_ListChanged

ElseIf TypeOf dg.DataSource Is DataView Then

AddHandler DirectCast(dg.D ataSource, DataView).ListC hanged, AddressOf
dv_ListChanged

End If

End If

cm = source

ar = alignToRight

bPainted = True

If Me.DataGridTabl eStyle.DataGrid .IsSelected(row Num) Then

cFore = Me.DataGridTabl eStyle.Selectio nForeColor

cBack = Me.DataGridTabl eStyle.Selectio nBackColor

Else

cFore = ForeColor

cBack = BackColor

End If

brBack = New LinearGradientB rush(bounds, cBack, Color.White, 90, False)

brFore = New SolidBrush(cFor e)

Dim bl As New Blend

bl.Factors = New Single() {0.0F, 0.1F, 0.5F, 0.7F, 0.7F, 0.5F, 0.3F, 0.2F,
0}

bl.Positions = New Single() {0, 0.1F, 0.2F, 0.5F, 0.6F, 0.7F, 0.8F, 0.9F,
1.0F}

DirectCast(brBa ck, LinearGradientB rush).Blend = bl

If Not bPanelOnly Then

MyBase.Paint(g, bounds, source, rowNum, brBack, brFore, alignToRight)

End If

If Not (mctrl Is Nothing) Then

' if there is another control to draw on move the bounds to the right edge
if htere is not then it will ignore that directive right? yesgot oitf the
control

'mctrl.Backgrou ndImage = bm

PaintRow(mctrl. CreateGraphics, bounds, rowNum)

End If

If Me.GetColumnVal ueAtRow(source, rowNum).ToStrin g = "Davolio" Then

Me.DataGridTabl eStyle.DataGrid .Select(rowNum)

End If

End Sub

Public Shadows Sub BeginUpdate()

MyBase.BeginUpd ate()

End Sub

Public Shadows Sub EndUpdate()

MyBase.EndUpdat e()

End Sub

Protected Overloads Overrides Sub Edit(ByVal source As
System.Windows. Forms.CurrencyM anager, ByVal rowNum As Integer, ByVal bounds As System.Drawing. Rectangle, ByVal [readOnly] As Boolean, ByVal instantText As String, ByVal cellIsVisible As Boolean)

MyBase.Edit(sou rce, rowNum, bounds, [readOnly], instantText, cellIsVisible)
MyBase.TextBox. ForeColor = ForeColor()

MyBase.TextBox. BackColor = BackColor

End Sub

Public Sub New()

End Sub

Private Sub dg_Scroll(ByVal sender As Object, ByVal e As System.EventArg s)
Handles dg.Scroll

Trace.WriteLine ("Scroll")

RedrawPanel()

End Sub

Private Sub MovePanel()

mctrl.Location = New Point(dg.Left + dg.RowHeaderWid th + 2, dg.Top + 21)

Dim intFactor As Integer = SystemInformati on.HorizontalSc rollBarHeight

For Each ctrl As Control In Me.DataGridTabl eStyle.DataGrid .Controls

If TypeOf ctrl Is HScrollBar Then

If Not DirectCast(ctrl , HScrollBar).Vis ible Then intFactor = 0

End If

Next

mctrl.Height = dg.Height - 21 - intFactor - 2

End Sub

Private Sub dg_Move(ByVal sender As Object, ByVal e As System.EventArg s)
Handles dg.Move

MovePanel()

End Sub

Private Sub PreparePanel()

Dim sf As New StringFormat

sf.LineAlignmen t = StringAlignment .Center

Dim rDraw As New RectangleF(0, 0, Me.Width, 20)

Dim g As Graphics = mctrl.CreateGra phics

g.Clear(mctrl.B ackColor)

Try

g.DrawString(Me .HeaderText, dg.Font, Brushes.Black, rDraw, sf)

ControlPaint.Dr awBorder3D(g, _

New Rectangle(0, 0, Me.Width, 19), Border3DStyle.R aisedInner)

Catch

End Try

End Sub

Private Sub dv_ListChanged( ByVal sender As Object, ByVal e As
System.Componen tModel.ListChan gedEventArgs)

'PreparePanel()

RedrawPanel()

End Sub

Private Sub dg_VisibleChang ed(ByVal sender As Object, ByVal e As
System.EventArg s) Handles dg.VisibleChang ed

Try

mctrl.Visible = dg.Visible

Catch ex As Exception

End Try

End Sub

Private Sub dg_SizeChanged( ByVal sender As Object, ByVal e As
System.EventArg s) Handles dg.SizeChanged

MovePanel()

RedrawPanel()

End Sub

Private Sub RedrawPanel()

Static oldTop As Integer = 0

If Not mctrl Is Nothing And dg.VisibleRowCo unt > 0 Then

Application.DoE vents()

Dim hti As DataGrid.HitTes tInfo = dg.HitTest(pt)

Dim newRow As Integer = hti.Row

Trace.WriteLine (String.Format( "First Row {0} Visible rows {1}", hti.Row,
dg.VisibleRowCo unt))

oldTop = newRow

bPanelOnly = True

For x As Integer = oldTop To oldTop + dg.VisibleRowCo unt - 1

If x < cm.Count Then

Trace.WriteLine (String.Format( "Drawing Row {0} {1}", x, dg.GetCellBound s(x, dg.FirstVisible Column)))

Paint(Nothing, dg.GetCellBound s(x, dg.FirstVisible Column), _

cm, x, Nothing, Nothing, ar)

End If

Next

bPanelOnly = False

End If

End Sub

Private Sub dg_Resize(ByVal sender As Object, ByVal e As System.EventArg s)
Handles dg.Resize

MovePanel()

End Sub

Private Sub mctrl_Paint(ByV al sender As Object, ByVal e As
System.Windows. Forms.PaintEven tArgs) Handles mctrl.Paint

Dim sf As New StringFormat

sf.LineAlignmen t = StringAlignment .Center

Dim rDraw As New RectangleF(0, 0, Me.Width, 20)

Dim g As Graphics = e.Graphics

g.Clear(mctrl.B ackColor)

Try

g.DrawString(Me .HeaderText, dg.Font, Brushes.Black, rDraw, sf)

ControlPaint.Dr awBorder3D(g, _

New Rectangle(0, 0, Me.Width, 19), Border3DStyle.R aisedInner)

Debug.WriteLine ("Panel Paint")

Dim hti As DataGrid.HitTes tInfo = dg.HitTest(pt)

Dim newRow As Integer = hti.Row

Dim oldTop As Integer

Trace.WriteLine (String.Format( "First Row {0} Visible rows {1}", hti.Row,
dg.VisibleRowCo unt))

oldTop = newRow

bPanelOnly = True

For x As Integer = oldTop To oldTop + dg.VisibleRowCo unt - 1

If x < cm.Count Then

Trace.WriteLine (String.Format( "Drawing Row {0} {1}", x, dg.GetCellBound s(x, dg.FirstVisible Column)))

Paint(Nothing, dg.GetCellBound s(x, dg.FirstVisible Column), _

cm, x, Nothing, Nothing, ar)

End If

Next

Catch

End Try

End Sub

Private Sub PaintRow(ByVal g As Graphics, ByVal bounds As Rectangle, ByVal
rownum As Integer)

Dim brFore As Brush

Dim brBack As Brush

Dim cFore As Color

Dim cBack As Color

Dim bounds2 As New Rectangle(0, bounds.Y - 21, bounds.Width, bounds.Height)
If Me.DataGridTabl eStyle.DataGrid .IsSelected(row num) Then

cFore = Me.DataGridTabl eStyle.Selectio nForeColor

cBack = Me.DataGridTabl eStyle.Selectio nBackColor

Else

cFore = ForeColor

cBack = BackColor

End If

brBack = New LinearGradientB rush(bounds, cBack, Color.White, 90, False)

brFore = New SolidBrush(cFor e)

Dim bl As New Blend

bl.Factors = New Single() {0.0F, 0.1F, 0.5F, 0.7F, 0.7F, 0.5F, 0.3F, 0.2F,
0}

bl.Positions = New Single() {0, 0.1F, 0.2F, 0.5F, 0.6F, 0.7F, 0.8F, 0.9F,
1.0F}

DirectCast(brBa ck, LinearGradientB rush).Blend = bl

If mctrl.Width <> Me.Width Then mctrl.Width = Me.Width

MyBase.Paint(g, bounds2, cm, rownum, _

brBack, brFore, ar)

If rownum = cm.Count - 1 Then

Dim br As New SolidBrush(Me.D ataGridTableSty le.DataGrid.Bac kgroundColor)

g.FillRectangle (br, 0, bounds2.Bottom + 1, mctrl.Width, _

mctrl.Height - bounds2.Bottom - 1)

End If

End Sub

End Class

Ken

-------------------------------
"anon" <ng*@tdrd.frees erve.co.uk> wrote in message
news:d1******** **@newsg3.svr.p ol.co.uk...
I want to obtain the appearance of frozen columns as in an excel sheet so
thet the first two columns of the table remain in view at all times.

Example - I have a table of products - listed by Item and Line.
I want the Item and Line columns to remain visible whilst being able to scroll the rest of the grid across to reveal other columns.
Ialso need the information to be present and synchronised with the rows when I scroll down.
All the data is held in one data table - and all I need to do is freeze the first two columns of the datagrid, but I am informed that the datagrid does not have this functionality.

Someone came up with the ides of having two datagrids - side by side - and
showing the first two columns (of the data table) in the first grid, with
the rest of the columns ( in the data table) being shown in the second grid. This idea would be fine, but I cannot get the two grids to scroll down
together as if bound together.

Can anyone please come up with a means of facilitating this idea - or
suggest another means.

I am using VB.NET 2002 Standard edition.

Thanks in advance

Terry


Nov 21 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
1738
by: justme | last post by:
Dears I have created the following page to display my data <head> <style type="text/css"> body, td, th, h1, h2 {font-family: sans-serif;} body, td, th {font-size: 100%;} a:link { font-weight:bold; color:E00000; text-decoration:none } a:visited { font-weight:bold; color:E00000; text-decoration:none } a:hover { font-weight:bold;...
6
1705
by: paulus4605 | last post by:
Dears I have the following problem I’m using a query to get all the data from my database from the past year the second query is displaying the results by month. How can I match the second query with the first query in order to get the results from the same store on the same row?
1
2583
by: Dominic Marks | last post by:
Hi, (I apologise if this is the wrong list, I haven't posted to a postgresql.org mailing list before, general seemed like a good catch-all) I am trying to implement a centralised authentication system using PostgreSQL as the central repository for user and password information. I want to keep a table which contains core user details...
1
1552
by: judy | last post by:
I have a number of Acess databases on a web site and copies of these databases on my local PC. The databases can be independently updated on the web and also on my local PC. Is there some way of synchronising my local copies of the dbs with the ones on my web site? I had thought this would be a common problem but I can't seem to find a...
1
1411
by: Michael Thomas | last post by:
Hi everyone Having a slight problem synchronising tables in MS Access 2002. I have two tables: Products (StockCode*, Description, CostPrice, SellingPrice) and
1
1372
by: Rajesh Sharma | last post by:
Dear all, please help me in Combo Box Synchronising. I have three levels. Category -> Type -> Subtype Depending on the selected Category, the Type appears in the second combo box and depending on the Type the Subtype appears.
0
1251
by: Scott Meddows | last post by:
I'm having trouble scrolling some datagrids so they sync up... I have two identical datagrids on a form, filled with eh same dataset. I want a user to be able to scroll on the datasets and the other dataset then scrolls to the same place on the other dataset (bare with me). This is the code that I have so far... Private Sub...
2
1921
by: rn5a | last post by:
In a shopping cart app, a ASPX page retrieves the order details & personal details of a user from a MS-Access database table depending upon the username of the user. The order details of a particular order (like ProductID, Name, Description, Quantity etc.) are displayed in one DataGrid where as the personal details of the buyer...
3
1573
by: chromis | last post by:
Hi, I've just setup a development server for testing websites at my office and wish to synchronise it with the sites folder on our live server, does anyone know of any good programs for this? I've got total file commander which is pretty good for synchronising directories but it apparently when downloading it changes the timestamp to the...
0
7852
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language...
1
7974
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
0
8221
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
0
6629
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
1
5719
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes...
0
5395
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
0
3845
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in...
1
2364
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
1
1455
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.