build an array for col headers

Smee

Well-Known Member
Licensed User
Longtime User
I am using the table example with a database. i need to use a field returned from the database as a column header for col2 3 etc. How can i do this to replace the headers? Also there may be more than 4 cols. i will not know in advance. how can i build the string in the array. I tried stringbuilder but that does not work it treats the string as one column

B4X:
Table1.SetHeader(Array As String("Prod Code", "Description", "Col2", "Col3","Col4"))

TIA
 

mc73

Well-Known Member
Licensed User
Longtime User
I don't know about this custom table, but I suppose you can parse a list inside the setHeader.
 
Upvote 0

Smee

Well-Known Member
Licensed User
Longtime User
Thanks for the reply but i want to also know how to dynamically build the Array As String part for other uses.
 
Upvote 0

Mahares

Expert
Licensed User
Longtime User
Here is what you need:
B4X:
txt = "SELECT Field1, Field2, Field3 FROM MyTable" 
     Cursor1=SQL1.ExecQuery(txt)     
      Dim Col_Name(3) As String
      For i=0 To Cursor1.ColumnCount-1
         Col_Name(i)=Cursor1.GetColumnName(i)
      Next
      Col_Name = Array As String(Col_Name(0), Col_Name(1) , Col_Name(2))
      Table1.SetHeader(Col_Name)
 
Upvote 0

Smee

Well-Known Member
Licensed User
Longtime User
Thanks very much for the reply Mahares,

looking at the code, that is very close to what i need and looks like it will work for the array part. However ,

B4X:
Dim Col_Name(3) As String
...
...
Col_Name = Array As String(Col_Name(0), Col_Name(1) , Col_Name(2))

shows that the number of columns is known in advance

what i need is similar to

B4X:
Counter=cursor1.ColumnCount
Dim Col_Name(Counter) As String
for x= 0 to Counter
Col_Name1=Col_Name1 & Col_Name(x)
next

Col_Name = Array As String(Col_Name1)

Obviously this would not work but it should give you the idea of what i am trying to achieve.
Any ideas?

Many thanks
 
Upvote 0

mc73

Well-Known Member
Licensed User
Longtime User
B4X:
Dim Col_Name() As String
Dim tempList As String
For i=0 To yourCursor.ColumnCount-1
        tempList=tempList & ",," & yourCursor.GetColumnName (i)
Next
Col_Name=Regex.Split (",,",tempList.SubString (2))
Table1.SetHeader(Col_Name)
 
Upvote 0

Mahares

Expert
Licensed User
Longtime User
Here is another solution where you do not need to know the number of columns in advance. You have choices:
B4X:
   txt = "SELECT Field1, Field2, Field3 FROM MyTable" 
        Cursor1=SQL1.ExecQuery(txt)    
      Dim MyColCount As Int
      MyColCount=Cursor1.ColumnCount
        Dim Col_Name(MyColCount) As String
        For i=0 To MyColCount-1
            Col_Name(i)=Cursor1.GetColumnName(i)
        Next
        Table1.SetHeader(Col_Name)
 
Upvote 0

mc73

Well-Known Member
Licensed User
Longtime User
Here is another solution where you do not need to know the number of columns in advance. You have choices:
B4X:
   txt = "SELECT Field1, Field2, Field3 FROM MyTable" 
        Cursor1=SQL1.ExecQuery(txt)    
        Dim MyColCount As Int
        MyColCount=Cursor1.ColumnCount
        Dim Col_Name(MyColCount) As String
        For i=0 To MyColCount-1
            Col_Name(i)=Cursor1.GetColumnName(i)
        Next
        Table1.SetHeader(Col_Name)

You are absolutely right, Mahares, no need for the splits I performed earlier :)
 
Upvote 0

Smee

Well-Known Member
Licensed User
Longtime User
Excellent,

Thank you both for your help and efforts
:icon_clap:
 
Upvote 0
Top