unvell / ReoGrid

Fast and powerful .NET spreadsheet component, support data format, freeze, outline, formula calculation, chart, script execution and etc. Compatible with Excel 2007 (.xlsx) format and working on .NET 3.5 (or client profile), WPF and Android platform.
https://reogrid.net
MIT License
1.32k stars 390 forks source link

Lost DataTable column captions #178

Open Gnuelmpf opened 5 years ago

Gnuelmpf commented 5 years ago

Hello Jing, it's me again ... :] My problem is, that column captions of a DataTable are lost if i put a DT on a worksheet. The strange thing is that this won't happen for every DT, though they are build in a row from one data set.

Here you can see the status before adding the DTs: image

After inserting the DTs, the "Value" & "Rt" caption are lost: image

Only here everything is fine: image

In the next 3 worksheets all column captions are lost: image image image

There is nothing special in my code. I can't figure it out, if its a coding problem, a VS2017 bug or (hopefully not) a ReoGrid problem. image

You can see that the column captions in the DT for worksheet("Main") are present: image

The DT for worksheet("MBUS") has all its column captions in dt(2) => List(of...) too: image

I can't see were the problem is & want to avoid to code an ugly workaround.

May be i can't see the forest for the trees.

Thx a lot for your attention Wish you a sunny weekend

jingwood commented 5 years ago

It's strange problem. Can you post the code how you adding a datatable onto ReoGrid worksheet? Thanks.

Gnuelmpf commented 5 years ago

Hi Jing, I hope this will help. Its the whole Sub. The original code where i did paste the datatable onto a worksheet is the code above.

image

Gnuelmpf commented 5 years ago

Hello Jing, this is the sub in REAL code now. :)) Hope this will work stand alone:

`Private Sub LoadConfigFile() Try Dim ProtLst As New List(Of String) Dim FileTXT_Array As New List(Of String) Dim FiTx_Ary_RowCnt As Int16

        Dim fileRead = My.Computer.FileSystem.OpenTextFileReader(Application.StartupPath & "\ChannelConfig.cfg", Encoding.GetEncoding(28591))   'iso-8859-1 Western European (ISO)
        Do Until fileRead.EndOfStream = True
            FileTXT_Array.Add(fileRead.ReadLine)
        Loop
        fileRead.Close()

        ProtLst.Add("Main")
        ProtLst.Add("All")

        Dim i As UInt16 = 0
        For Each ProtType As String In FileTXT_Array
            If ProtLst.Contains(ProtType.Split(vb_Tab)(4).ToString) Then
            Else
                ProtLst.Add(ProtType.Split(vb_Tab)(4).ToString)
            End If
            i += CByte(1)
        Next
        ProtLst.Remove("Protocol")
        FiTx_Ary_RowCnt = CShort(i)

        For Each ProtType In ProtLst
            Dim dt_ As New DataTable
            With dt_
                .Clear()

                Dim hader = FileTXT_Array(0).Split(vb_Tab)

                If ProtType = "Main" Then
                    .Columns.Add("Name") : .Columns.Add("Value") : .Columns.Add("Unit") : .Columns.Add("Rt")
                Else
                    For Each head In hader
                        .Columns.Add(head)
                    Next
                End If

                For Each header In FileTXT_Array
                    If header.Split(vb_Tab)(4) = ProtType Then
                        .Rows.Add(header.Split(vb_Tab))
                    ElseIf ProtType = "All" Then
                        .Rows.Add(header.Split(vb_Tab))
                    ElseIf ProtType = "Main" Then
                        .Rows.Add(header.Split(vb_Tab)(0), "", header.Split(vb_Tab)(1), "")
                    End If
                Next

                DTL.Add(dt_)
            End With
        Next

        Dim ii As Byte = 0
        For Each DataTable In DTL
            Dim SheetName As String = (ProtLst(ii).ToString)

            With grid
                If ii > 0 Then
                    .Worksheets.Add(.CreateWorksheet(SheetName))
                Else
                    .CurrentWorksheet.Name = SheetName
                End If

                '|inserting DTs => loosing header names
                grid.Worksheets(SheetName).Resize(DTL(ii).Rows.Count, DTL(ii).Columns.Count)
                grid.Worksheets(SheetName)("A1") = DTL(ii)

                ''| => WORKAROUND <= | for inserting DTs
                'With .Worksheets(SheetName)
                '    .Resize(DTL(ii).Rows.Count + 1, DTL(ii).Columns.Count)
                '    grid.Worksheets(SheetName)("A2") = DTL(ii)
                '    For v As Int32 = 0 To .ColumnCount - 1
                '        Dim ColHeadName As String = DTL(ii).Columns(v).ColumnName.ToString
                '        grid.Worksheets(SheetName).SetCellData(0, v, ColHeadName) 'Header into row(0) 
                '        grid.Worksheets(SheetName).ColumnHeaders.Item(v).Text = ColHeadName
                '    Next
                '    If grid.Worksheets(SheetName).GetCellData(0, 0).ToString = grid.Worksheets(SheetName).GetCellData(1, 0).ToString Then
                '        grid.Worksheets(SheetName).DeleteRows(1, 1)
                '    End If

                '    'Call Set_ControlAppearanceStyle(SheetName)
                '    'Call Set_Worksheet_Property(SheetName)
                '    .RowHeaders(0).IsAutoHeight = False
                '    .RowHeaders(0).Height += CUShort(10)
                'End With
            End With

            ii += CByte(1)
        Next

        Catch ex As Exception
        Debug.WriteLine(DateTime.UtcNow.ToString("hh:mm:ss.fffffff") & " LoadConfigFile > " & ex.ToString)
    End Try
End Sub`

Use this as ChannelConfig.cfg: All values are tab spaced (chr(9)), if its lost here. Header_A Header_B Header_C Header_D Header_E row_A1 row_B1 row_C1 row_D1 row_E1 row_A2 row_B2 row_C2 row_D2 row_E2 row_A3 row_B3 row_C3 row_D3 row_E3

OK the carrige return is lost here :( If you paste the config values into notepad then replace the space after Header_E, row_E1, row_E2 and row_E3 with a carrige return (chr(13)). Should look like this: image