Groups | Blog | Home
all groups > vb.net controls > september 2004 >

vb.net controls : DataGrid Binding Boolean Property of Custom Objects


ngspamshield NO[at]SPAM yahoo.com
9/8/2004 11:52:23 AM
I am experimenting with binding collections of custom objects to a
DataGrid. I am having problems with the DataGridBoolColumn style that
I am mapping to my boolean properties: I cannot turn off the tri-state
behavior. Setting AllowNulls = false does not work.
I also get an error "Cannot Winen from Target Type to Primitive Type.
Do you want to correct the value?" when leaving a cell in Null state.
(greyed) .I have programmed a workaroud in the mouse_up event, but If
somebody can answer what is going wrong, I'd appreciate it.

Environment : Win2K, VS.net 2003 ,Framework v1.1, Language-VB.Net

Here is the complete code to reproduce this problem:
Start a new windows forms project. Drop a datagrid on the form.
Add this in class form1:

Private colNames As Collection
Private Sub Form1_Load(ByVal sender As Object, ByVal e As
System.EventArgs) Handles MyBase.Load

Dim n As cName
Dim cb As DataGridBoolColumn
Dim ts As New DataGridTableStyle
Dim cs As DataGridTextBoxColumn


colNames = New Collection

n = New cName
n.fName = "Fred"
n.lname = "Smith"
n.IsCurrent = True
colNames.Add(n)

n = New cName
n.fName = "Jane"
n.lName = "Jones"
n.IsCurrent = False
colNames.Add(n)



cs = New DataGridTextBoxColumn
cs.MappingName = "lname"
cs.HeaderText = "Last"
cs.Width = 100
ts.GridColumnStyles.Add(cs)

cs = New DataGridTextBoxColumn
cs.MappingName = "fname"
cs.HeaderText = "First"
cs.Width = 100
ts.GridColumnStyles.Add(cs)


cb = New DataGridBoolColumn
cb.AllowNull = False
cb.MappingName = "IsCurrent"
cb.HeaderText = "Current?"
cb.Width = 100

ts.GridColumnStyles.Add(cb)
ts.MappingName = "colNames"

DataGrid1.TableStyles.Clear()
DataGrid1.TableStyles.Add(ts)
Me.DataGrid1.SetDataBinding(colNames, "")


End Sub



'And this after end class 'Form1

Public Class cName
Private m_fName As String
Private m_lName As String
Private m_IsCurrent As Boolean

Public Property IsCurrent() As Boolean
Get
Return m_IsCurrent
End Get
Set(ByVal Value As Boolean)
m_IsCurrent = Value
End Set
End Property

Public Property fname() As String
Get
Return m_fName
End Get
Set(ByVal Value As String)
m_fName = Value
End Set

End Property

Public Property lname() As String
Get
Return m_lName
End Get
Set(ByVal Value As String)
m_lName = Value
End Set

End Property

End Class

When I do this, I get the object's props mapped in the grid OK, but
the checkbox column is allowing tristate even though AllowNulls=False.


The workaroud I'm using is:
Private Sub DataGrid1_MouseUp(ByVal sender As Object, ByVal e As
System.Windows.Forms.MouseEventArgs) Handles DataGrid1.MouseUp
Dim hti As DataGrid.HitTestInfo
hti = DataGrid1.HitTest(e.X, e.Y)
If hti.Column = 0 Then
DataGrid1.Item(hti.Row, hti.Column) = Not
Convert.ToBoolean(DataGrid1.Item(hti.Row, hti.Column))
End If

End Sub

(Not my idea - Thanks to moorejd on www.WindowsForms.net)


Ken Tucker [MVP]
9/10/2004 8:14:35 AM
Hi,

I wrote a new column that inherits from datagridboolcolumn to
overcome that.


Public Class NoNullBoolColumn

Inherits DataGridBoolColumn

Protected Overrides Function GetColumnValueAtRow(ByVal lm As
System.Windows.Forms.CurrencyManager, ByVal row As Integer) As Object

Dim objNull As Object = Convert.DBNull

If objNull.Equals(MyBase.GetColumnValueAtRow(lm, row)) Then

Return False

Else

Return MyBase.GetColumnValueAtRow(lm, row)

End If

End Function

End Class



Ken

-----------------------

[quoted text, click to view]
I am experimenting with binding collections of custom objects to a
DataGrid. I am having problems with the DataGridBoolColumn style that
I am mapping to my boolean properties: I cannot turn off the tri-state
behavior. Setting AllowNulls = false does not work.
I also get an error "Cannot Winen from Target Type to Primitive Type.
Do you want to correct the value?" when leaving a cell in Null state.
(greyed) .I have programmed a workaroud in the mouse_up event, but If
somebody can answer what is going wrong, I'd appreciate it.

Environment : Win2K, VS.net 2003 ,Framework v1.1, Language-VB.Net

Here is the complete code to reproduce this problem:
Start a new windows forms project. Drop a datagrid on the form.
Add this in class form1:

Private colNames As Collection
Private Sub Form1_Load(ByVal sender As Object, ByVal e As
System.EventArgs) Handles MyBase.Load

Dim n As cName
Dim cb As DataGridBoolColumn
Dim ts As New DataGridTableStyle
Dim cs As DataGridTextBoxColumn


colNames = New Collection

n = New cName
n.fName = "Fred"
n.lname = "Smith"
n.IsCurrent = True
colNames.Add(n)

n = New cName
n.fName = "Jane"
n.lName = "Jones"
n.IsCurrent = False
colNames.Add(n)



cs = New DataGridTextBoxColumn
cs.MappingName = "lname"
cs.HeaderText = "Last"
cs.Width = 100
ts.GridColumnStyles.Add(cs)

cs = New DataGridTextBoxColumn
cs.MappingName = "fname"
cs.HeaderText = "First"
cs.Width = 100
ts.GridColumnStyles.Add(cs)


cb = New DataGridBoolColumn
cb.AllowNull = False
cb.MappingName = "IsCurrent"
cb.HeaderText = "Current?"
cb.Width = 100

ts.GridColumnStyles.Add(cb)
ts.MappingName = "colNames"

DataGrid1.TableStyles.Clear()
DataGrid1.TableStyles.Add(ts)
Me.DataGrid1.SetDataBinding(colNames, "")


End Sub



'And this after end class 'Form1

Public Class cName
Private m_fName As String
Private m_lName As String
Private m_IsCurrent As Boolean

Public Property IsCurrent() As Boolean
Get
Return m_IsCurrent
End Get
Set(ByVal Value As Boolean)
m_IsCurrent = Value
End Set
End Property

Public Property fname() As String
Get
Return m_fName
End Get
Set(ByVal Value As String)
m_fName = Value
End Set

End Property

Public Property lname() As String
Get
Return m_lName
End Get
Set(ByVal Value As String)
m_lName = Value
End Set

End Property

End Class

When I do this, I get the object's props mapped in the grid OK, but
the checkbox column is allowing tristate even though AllowNulls=False.


The workaroud I'm using is:
Private Sub DataGrid1_MouseUp(ByVal sender As Object, ByVal e As
System.Windows.Forms.MouseEventArgs) Handles DataGrid1.MouseUp
Dim hti As DataGrid.HitTestInfo
hti = DataGrid1.HitTest(e.X, e.Y)
If hti.Column = 0 Then
DataGrid1.Item(hti.Row, hti.Column) = Not
Convert.ToBoolean(DataGrid1.Item(hti.Row, hti.Column))
End If

End Sub

(Not my idea - Thanks to moorejd on www.WindowsForms.net)


YS

AddThis Social Bookmark Button