sizing columns in vb.net using the compact framework

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

I have found an example of this in C# and converted the code the vb.net
however I am not getting the expected results. Here is my converted code:

cManager = CType(.BindingContext(InvItem), CurrencyManager)
dgInv.DataSource = InvItem

Dim myGridTableStyle As DataGridTableStyle = New
DataGridTableStyle
myGridTableStyle.MappingName = "InvItem"

Dim colInv As New DataGridTextBoxColumn
With colInv
.MappingName = "Inv"
.HeaderText = "Inv"
.Width = 5
End With
myGridTableStyle.GridColumnStyles.Add(colInv)

Dim colID As New DataGridTextBoxColumn
With colID
.MappingName = "ID"
.HeaderText = "ID"
.Width = 75
End With
myGridTableStyle.GridColumnStyles.Add(colID)

Dim colDue As New DataGridTextBoxColumn
With colDue
.MappingName = "Due"
.HeaderText = "Due"
.Width = 75
End With
myGridTableStyle.GridColumnStyles.Add(colDue)

dgInv.TableStyles.Clear()
dgInv.TableStyles.Add(myGridTableStyle)

Any ideals on what I am doing wrong?
 
I don't see anything obvious that stands out with your table style logic.
Here is a code sample that works for me. If this doesn't work for you then I
would think that the issue is somewhere else.

Protected DataTable1 As DataTable

....

DataTable1 = New DataTable("MyTable")
DataTable1.Columns.Add("Column1", Type.GetType("System.String"))
DataTable1.Columns.Add("Column2", Type.GetType("System.String"))
DataTable1.Rows.Add(New Object() {"John", "Smith"})
DataTable1.Rows.Add(New Object() {"Jill", "Jones"})

Dim DataGridTableStyle1 As New DataGridTableStyle
With DataGridTableStyle1
..MappingName = "MyTable"
End With

Dim DataGridTextBoxColumn1 As New DataGridTextBoxColumn
With DataGridTextBoxColumn1
..MappingName = "Column1"
..HeaderText = "First Column"
..Width = 100
End With
DataGridTableStyle1.GridColumnStyles.Add(DataGridTextBoxColumn1)

Dim DataGridTextBoxColumn2 As New DataGridTextBoxColumn
With DataGridTextBoxColumn2
..MappingName = "Column2"
..HeaderText = "Second Column"
..Width = 100
End With
DataGridTableStyle1.GridColumnStyles.Add(DataGridTextBoxColumn2)

Me.DataGrid1.TableStyles.Add(DataGridTableStyle1)
Me.DataGrid1.DataSource = DataTable1
 
Thanks the issue was the way I was binding the data to the grid:

cManager = CType(.BindingContext(InvItem), CurrencyManager)
dgInv.DataSource = InvItem

InvItem is an array of a class. I have to change my binding routine to:

DataTable1 = New DataTable("MyTable")
DataTable1.Columns.Add("Inv", Type.GetType("System.String"))
DataTable1.Columns.Add("ItemID", Type.GetType("System.String"))
DataTable1.Columns.Add("DueDate", Type.GetType("System.String"))
DataTable1.Columns.Add("Sel", Type.GetType("System.String"))

For X = 0 To UBound(InvItem) - 1
DataTable1.Rows.Add(New Object() {InvItem(X).Inv, InvItem(X).ID,
InvItem(X).Due, InvItem(X).Sel})
Next

Dim DataGridTableStyle1 As New DataGridTableStyle
With DataGridTableStyle1
.MappingName = "MyTable"
End With

Dim DataGridTextBoxColumn1 As New DataGridTextBoxColumn
With DataGridTextBoxColumn1
.MappingName = "Inv"
.HeaderText = "Inv"
.NullText = "N"
.Width = 25
End With
DataGridTableStyle1.GridColumnStyles.Add(DataGridTextBoxColumn1)

dgInv.TableStyles.Add(DataGridTableStyle1)
dgInv.DataSource = DataTable1
 
Back
Top