TableView - Setting data type to TableView columns
Hey all,
I'm struggling to figure out how to define a TableViewColumn to be of type Decimal instead of the default type being Text.
I'm in the section where I create a TableViewRowHeader and adding TableViewColumns to it.
I can create a new column:
Dim myTableViewColumn As New TableViewColumn
myTableViewColumn.Name = "Col1"
But when I try to set the type to this column, I get an error " Input string was not in a correct format." when trying to retrieve the table:
myTableViewColumn.DataType = XFDataType.Decimal
Is this syntax correct? I'm assuming that it's in the column definition where I should be defining the datatype but now not so sure. Hopefully someone can point me in the right direction.
As a side note, page 15 in the Table Views User Guide makes reference to a function called CreateTableViewColumn. Is this a custom function or part of the OS library. The compiler doesn't recognize this function.
I figured it out.
The problem happens if you try to add visible headers. The minute you call tableView.Rows.Add, the system will ignore the isHeader property and just assume that any cell in the row must conform to the datatype you've declared for the column (you cannot override datatype per row, it's either the entire column or nothing; that also means you can save yourself the trouble to assign it to every cell).
So really there are only two options at this point: strings everywhere, or no headers in your TableView.
Here's a working implementation that renounces headers:
Dim dt As New DataTable("FakeData") dt.Columns.Add("SomeDescription", GetType(String)) dt.Columns.Add("DecimalNumber", GetType(Decimal)) dt.Rows.Add("Row one", 10.5) dt.Rows.Add("Row Two", 11.0) Dim tv As TableView = New TableView() tv.CanModifyData = False ' column definitions Dim tvHeader As New TableViewRow() For Each dtCol As DataColumn In dt.Columns Dim tvCol1 As New TableViewColumn() tvCol1.Name = dtCol.ColumnName tvCol1.Value = dtCol.ColumnName If tvCol1.Name.XFEqualsIgnoreCase("DecimalNumber") Then tvCol1.DataType = XFDataType.Decimal End If tvCol1.IsHeader = True tv.Columns.Add(tvCol1) ' uncomment this to break ' tvHeader.Items.Add(tvCol1.Name, tvCol1) Next ' uncomment this to break ' tv.Rows.Add(tvHeader) ' data For Each row In dt.Rows Dim tvDataRow1 As New TableViewRow() For Each tvCol As TableViewColumn In tv.Columns Dim col As New TableViewColumn() col.Name = tvCol.Name col.Value = row.Item(tvCol.Name) col.IsHeader = False tvDataRow1.Items.Add(col.Name, col) Next tv.Rows.Add(tvDataRow1) Next Return tv
I would probably consider this a bug, or at the very least something that should be documented. Feel free to submit it and claim the glory 😉