Forum Discussion

Cosimo's avatar
Cosimo
Contributor II
3 years ago

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 addi...
  • JackLacava's avatar
    JackLacava
    3 years ago

    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 😉