How to properly impliment a DataGridTableStyle

P

pmclinn

I have the following code snippet that works great except when I try to
format the check column to be 2 states not 3 the grid gets corrupted.

'_____________________Works great Begin
dt.Columns.Add("YesNo", 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

'_____________________Works great end

'dt.AcceptChanges()

txtCount.Text = CType(intCounter, String)

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

'_______________This fails

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)

'_______________end of failure
'_______________class refrenced:
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
 
P

pmclinn

Answer: Every column has to be accounted for when you use a template
apparently.
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)
 

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

Top