473,325 Members | 2,480 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,325 software developers and data experts.

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

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
1 3565
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: Bill C. | last post by:
Hello, I know this has been discussed a lot already because I've been searching around for information the last few weeks. I'm trying to implement a DataGridComboBoxColumn class. I've found...
1
by: The_Rave | last post by:
Hi everyone, I'm trying to add my own template columns to the property builder of ..NET. E.g. a checkbox column, or an image column. But I can't find the sources of the wizard, or a way to add...
3
by: Darryn Ross | last post by:
Hi, I am having a few problems with my datagrid, the data in my database isn't coming through right. for example 100.00 in the table is coming through as 100 and 01/01/2004 in the table is...
0
by: Richard | last post by:
Does anyone have a best practices object model for the properties and methods that would typically be involved with the GDI painting, rendering, etc. aspect of this control. Classes surely to be...
2
by: nate axtell | last post by:
I'm trying to create a custom DataGridBoolColumn. I inherit DataGridColumnStyle and create a public CheckBox variable. This columnType will be mapped to a dataTable boolean column. What are the...
1
by: Richard | last post by:
Does anyone have a best practices object model for the properties and methods that would typically be involved with the GDI painting, rendering, etc. aspect of this control. Classes surely to be...
9
by: Duncan Barnes-Ceeney | last post by:
I’m having problems with a custom Combo box. The main problem is that I want to modify the look of the combo which includes the size of the button. To do this I have inherited from the standard...
3
by: Richard Ryerson | last post by:
I have a general DataGridComboBoxColumn that I built using the Example in the .NET 2003 Combined Collection help file (that was a data time picker). I am able to assign a data source and display...
2
by: Hamed | last post by:
What does instantText mean in Edit method of DataGridColumnStyle? Regards Hamed
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 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 former...

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.