Windows Form Datagrid Checkbox 3 states problem

P

pmclinn

Below is the code I'm using to dynamically fill a datagrid from a
datatable (fed by an ole connection). Everything is working great
except that my users have to click 2 time on the checkbox to select it,
and if they click on it again it gets greyed out... click on the check
again and then it disapears.

What I want is to be able to be able to:
Click on the box once to check it.
Click on it again to remove the check.

How do you fix this issue?

Dim sql As String = "select A, b, c, d...."

Dim sConnString As String = _
"Provider...."
Dim myConnection As New OleDbConnection

dgResults.DataSource = dt
dt.Columns.Add("Yes/No", System.Type.GetType("System.Boolean"))
dt.Columns.Add("Number", System.Type.GetType("System.String"))
dt.Columns.Add("LOT", System.Type.GetType("System.String"))
dt.Columns.Add("House", System.Type.GetType("System.String"))
dt.Columns.Add("Node", System.Type.GetType("System.String"))
Dim dr As DataRow
dr = dt.NewRow
Try
myConnection = New OleDb.OleDbConnection(sConnString)
Dim myCommand As New OleDbCommand(sql, myConnection)
myConnection.Open()

Dim myReader As OleDbDataReader = myCommand.ExecuteReader
Dim intCounter As Integer = 0
While myReader.Read()
dr = dt.NewRow
dr(0) = False
dr(1) = myReader(0)
dr(2) = myReader(1)
dr(3) = myReader(2)
dr(4) = myReader(3)
dt.Rows.Add(dr)
intCounter += 1
End While
txtCount.Text = CType(intCounter, String)

myReader.Close()
myConnection.Close()
myConnection.Dispose()

Catch
Console.Out.WriteLine("Error: " & Err.Description)
myConnection.Close()
myConnection.Dispose()

End Try
 
K

Ken Tucker [MVP]

Hi,

Here is a datagrid bool column that will prevent the 3rd null state.
Use this column in your datagrid column style.

http://msdn.microsoft.com/library/d...tingwindowsformsdatagridvisualbasicprimer.asp


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

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

If FalseValue.Equals(GetColumnValueAtRow(source, rowNum)) Then

setcolumnvalueatrow(source, rowNum, TrueValue)

Else

setcolumnvalueatrow(source, rowNum, FalseValue)

End If

End Sub



End Class



Ken

-------------------------
Below is the code I'm using to dynamically fill a datagrid from a
datatable (fed by an ole connection). Everything is working great
except that my users have to click 2 time on the checkbox to select it,
and if they click on it again it gets greyed out... click on the check
again and then it disapears.

What I want is to be able to be able to:
Click on the box once to check it.
Click on it again to remove the check.

How do you fix this issue?

Dim sql As String = "select A, b, c, d...."

Dim sConnString As String = _
"Provider...."
Dim myConnection As New OleDbConnection

dgResults.DataSource = dt
dt.Columns.Add("Yes/No", System.Type.GetType("System.Boolean"))
dt.Columns.Add("Number", System.Type.GetType("System.String"))
dt.Columns.Add("LOT", System.Type.GetType("System.String"))
dt.Columns.Add("House", System.Type.GetType("System.String"))
dt.Columns.Add("Node", System.Type.GetType("System.String"))
Dim dr As DataRow
dr = dt.NewRow
Try
myConnection = New OleDb.OleDbConnection(sConnString)
Dim myCommand As New OleDbCommand(sql, myConnection)
myConnection.Open()

Dim myReader As OleDbDataReader = myCommand.ExecuteReader
Dim intCounter As Integer = 0
While myReader.Read()
dr = dt.NewRow
dr(0) = False
dr(1) = myReader(0)
dr(2) = myReader(1)
dr(3) = myReader(2)
dr(4) = myReader(3)
dt.Rows.Add(dr)
intCounter += 1
End While
txtCount.Text = CType(intCounter, String)

myReader.Close()
myConnection.Close()
myConnection.Dispose()

Catch
Console.Out.WriteLine("Error: " & Err.Description)
myConnection.Close()
myConnection.Dispose()

End Try
 
P

pmclinn

Ken,

This template will not work with my code format will it? I'm
dynamically building the checkbox.

-Peter
 
P

pmclinn

I added in this code and Ken's class and all I get is on checkbox cell.
How should I modify my prog to take advange of this class? Any help
would be appreciated. I've trying to get this to work for a couple
hours and its driving me crazy.

....myConnection.Dispose()

Dim tsGrid As New DataGridTableStyle
tsGrid.MappingName = "dt"

Dim cstYesNo As New NoNullBoolColumn
cstYesNo.AllowNull = False
cstYesNo.MappingName = "YesNo"
cstYesNo.HeaderText = "YesNo"
tsGrid.GridColumnStyles.Add(cstYesNo)
dgResults.TableStyles.Add(tsGrid)
 
P

pmclinn

Answer: ( Private WithEvents dt As New DataTable("dtResults"))
Dim tsGrid As New DataGridTableStyle
tsGrid.MappingName = "dtResults"

Dim cstYesNo As New NoNullBoolColumn
cstYesNo.MappingName = "YesNo"
cstYesNo.AllowNull = False
cstYesNo.HeaderText = "YesNo"
cstYesNo.Alignment = HorizontalAlignment.Center
tsGrid.GridColumnStyles.Add(cstYesNo)


Dim cstNumber As New DataGridTextBoxColumn
cstNumber.MappingName = "Number"
cstNumber.HeaderText = "Number"
cstNumber.Alignment = HorizontalAlignment.Center
tsGrid.GridColumnStyles.Add(cstNumber)

Dim cstLOT As New DataGridTextBoxColumn
cstLOT.MappingName = "LOT"
cstLOT.HeaderText = "LOT"
cstLOT.Alignment = HorizontalAlignment.Center
tsGrid.GridColumnStyles.Add(cstLOT)

Dim cstHouse As New DataGridTextBoxColumn
cstHouse.MappingName = "House"
cstHouse.HeaderText = "House"
cstHouse.Alignment = HorizontalAlignment.Center
tsGrid.GridColumnStyles.Add(cstHouse)

Dim cstNode As New DataGridTextBoxColumn
cstNode.MappingName = "Node"
cstNode.HeaderText = "Node"
cstNode.Alignment = HorizontalAlignment.Center
tsGrid.GridColumnStyles.Add(cstNode)

dgResults.TableStyles.Add(tsGrid)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads


Top