By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
459,963 Members | 1,815 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 459,963 IT Pros & Developers. It's quick & easy.

Making a Custom DataGridColumnStyle, problems with Paint/Edit fncs.

P: n/a
In VB .Net I made a custom CheckBox column style (for the Datagrid control) that maps to two DataTable columns , one it uses for the Checked status and the other it uses for the Enabled status. I am having a couple problems so far.
1. with the way the Paint method works: When I scroll to the right, the custom column gets Drawn on top of the left-most "Selecting" column (the one where the green arrows are displayed). How can I possibly modify my Paint Method so the column will be underneath the "Selecting" when under-lapping and not display when scrolled far enough to the right. (My custom column is the first column on the left.

2. When I move from CheckBox to Checkbox the Commit method is not fired. And when I click somewhere else on the datagrid I see abnormality in the commit. Changed checkboxes will go back to their default value.

Any help would be greatly appreciated. Here is my code for the class:

Nate

[code]
Imports System
Imports System.Drawing
Imports System.Windows.Forms
Imports System.Data

Public Class DGBoolCol
Inherits DataGridColumnStyle

Private Const Min_H As Integer = 10
Private Const Pref_H As Integer = 10
Private Const Pref_W As Integer = 50

Protected theCurrencyManager As CurrencyManager = Nothing

Private htCheckBoxes As New Hashtable
Private sourceDataTable As New DataTable

Private Allow_Null As Boolean = False

Public Sub New(ByRef dt As DataTable)
sourceDataTable = dt.Copy
End Sub

Protected Overrides Sub Abort(ByVal rowNum As Integer)
Invalidate()
End Sub

Protected Overrides Function Commit(ByVal dataSource As CurrencyManager, ByVal rowNum As Integer) As Boolean
Dim cb As CheckBox = DirectCast(htCheckBoxes("cbAtRow" & CStr(rowNum)), CheckBox)
SetColumnValueAtRow(dataSource, rowNum, cb.Checked)

theCurrencyManager = Nothing
Invalidate()
Return True
End Function

Protected Overrides Function GetMinimumHeight() As Integer
Return Min_H
End Function

Protected Overrides Function GetPreferredHeight(ByVal g As Graphics, ByVal value As Object) As Integer
Return Pref_H
End Function

Protected Overrides Function GetPreferredSize(ByVal g As Graphics, ByVal value As Object) As System.Drawing.Size
Return New Size(Pref_W, Pref_H)
End Function

Protected Overloads Overrides Sub Edit(ByVal source As CurrencyManager, ByVal rowNum As Integer, _
ByVal bounds As Rectangle, ByVal [readOnly] As Boolean)
Edit(source, rowNum, bounds, [readOnly], Nothing)
End Sub

Protected Overloads Overrides Sub Edit(ByVal source As CurrencyManager, ByVal rowNum As Integer, _
ByVal bounds As Rectangle, ByVal [readOnly] As Boolean, _
ByVal instantText As String)
Edit(source, rowNum, bounds, [readOnly], instantText, True)
End Sub

Protected Overloads Overrides Sub Edit(ByVal source As CurrencyManager, ByVal rowNum As Integer, _
ByVal bounds As Rectangle, ByVal [readOnly] As Boolean, _
ByVal instantText As String, ByVal cellIsVisible As Boolean)

Dim cb As CheckBox = DirectCast(htCheckBoxes("cbAtRow" & CStr(rowNum)), CheckBox) 'access from HashTbl
cb.Checked = CBool(GetColumnValueAtRow(source, rowNum))
ColumnStartedEditing(cb)
theCurrencyManager = source

End Sub
Protected Overloads Overrides Sub Paint(ByVal g As Graphics, ByVal bounds As Rectangle, _
ByVal source As CurrencyManager, ByVal rowNum As Integer)
Paint(g, bounds, source, rowNum, False)
End Sub

Protected Overloads Overrides Sub Paint(ByVal g As Graphics, ByVal bounds As Rectangle, _
ByVal source As CurrencyManager, ByVal rowNum As Integer, _
ByVal alignToRight As Boolean)
Dim drawBrush As SolidBrush = Nothing
drawBrush = New SolidBrush(DataGridTableStyle.BackColor)
g.FillRectangle(drawBrush, bounds)
'drawBrush = New SolidBrush(DataGridTableStyle.ForeColor)

Dim drv As DataRowView = [source].List.Item(rowNum) 'drv.Item(0) The Enabled column of the DT
Dim cb As CheckBox = DirectCast(htCheckBoxes("cbAtRow" & CStr(rowNum)), CheckBox)
cb.Checked = GetColumnValueAtRow(source, rowNum)
cb.Enabled = CBool(drv.Item(0))
cb.Bounds = bounds
cb.Visible = True

AddHandler cb.Leave, AddressOf CBLeave
cb.Focus()
End Sub
Protected Overrides Sub SetDataGridInColumn(ByVal value As DataGrid)
If htCheckBoxes.Count = 0 Then
If Not (value Is Nothing) Then
For i As Integer = 0 To sourceDataTable.Rows.Count - 1
Dim cb As New CheckBox
cb.Name = "cbAtRow" & CStr(i) 'give the checkbox a name for referencing
cb.Visible = False
cb.Enabled = Allow_Null 'set the enabled value depending on the Enable DB field

htCheckBoxes.Add("cbAtRow" & CStr(i), cb) 'add the checkbox to the hashtable
value.Controls.Add(cb) 'add the checkbox to the datagrid controls
Next
End If
End If
End Sub

'Leave Event for calling the commit possibly?
Public Sub CBLeave(ByVal sender As Object, ByVal e As System.EventArgs)

End Sub

Public Property AllowNull() As Boolean
Get
Return (Allow_Null)
End Get
Set(ByVal Value As Boolean)
Allow_Null = Value
'call an UpdateHashTable()
End Set
End Property

End Class

[\code]
Nov 21 '05 #1
Share this Question
Share on Google+
1 Reply


P: n/a
Should I try adding the checkboxes to the individual rows or maybe even the
cells the checkboxes will exist in? Does anyone know how to add a checkbox
control to a Cell or Row of a datagrid?
Nov 21 '05 #2

This discussion thread is closed

Replies have been disabled for this discussion.