In a dynamic crosstab how can one find out the number of columns that where created?
- You are here:
- Home
- Q&A
- Q&A
- PowerBuilder
- Dynamic CrossTabs
Resolved
Dynamic CrossTabs
- How-to
- Reynard Harrison
- PowerBuilder
- Thursday, 8 June 2023 17:00 PM UTC
- Friday, 9 June 2023 07:49 AM UTC
- PowerBuilder
- # Permalink
Hi.
I will provide some code that does what John suggested but doesn't write to a file.
string ls_data, ls_line
integer li_pos, li_counter = 0
ls_data = dw_2.Object.DataWindow.Data
if ls_data <> "" then
ls_line = left(ls_data, pos(ls_data, '~r~n'))
else
return
end if
messagebox("Info:", ls_line)
li_pos = pos(ls_line, "~t")
if li_pos < 1 then return
do while li_pos > 0
li_counter ++
li_pos = pos(ls_line, "~t", li_pos + 1)
loop
// Attention: You have to increment count by 1 because you have tabs = columns count - 1...
messagebox("Columns:", li_counter + 1)
Same example implemented with pfc and using n_cst_string:
string ls_data, ls_parsed_data[], ls_parsed_line[]
n_cst_string lnv_str
ls_data = dw_2.Object.DataWindow.Data
if ls_data = "" then return
lnv_str.of_parsetoarray(ls_data, "~r~n", ls_parsed_data)
messagebox(string(upperbound(ls_parsed_data)), ls_data)
if upperbound(ls_parsed_data) > 0 then
lnv_str.of_parsetoarray(ls_parsed_data[1], "~t", ls_parsed_line)
end if
messagebox(string(upperbound(ls_parsed_line)), ls_parsed_data[1])
Both example may need some adjustments as I did some basic testing.
Andreas.
- John Fauss
- Friday, 9 June 2023 13:44 PM UTC
-
Helpful Loading... Helpful 1
- Friday, 9 June 2023 16:12 PM UTC
- PowerBuilder
- # 1
Just an another way to get the column count from dynamic crosstab.
Long ll_col_count
dw_1.modify('datawindow.crosstab.staticmode=yes')
ll_col_count = Long(dw_1.Describe("DataWindow.Column.Count"))
- John Fauss
- Friday, 9 June 2023 17:39 PM UTC
-
Helpful Loading... Helpful 0
- Andreas Mykonios
- Friday, 9 June 2023 17:41 PM UTC
Very nice. I also wasn't aware. Maybe because I avoid using crosstabs.
Andreas.
-
Helpful Loading... Helpful 1
- Thursday, 8 June 2023 19:36 PM UTC
- PowerBuilder
- # 2
Hi, Reynard -
The following is a little out-of-the-box, but it might work for you (the described technique works, however):
1. Save the crosstab DW contents as a (temporary) CSV file without column headings: dw_1.SaveAs("C:\temp.csv",CSV!,False)
2. Open the CSV file for reading in line mode: li_filenum = FileOpen("C:temp.csv",LineMode!,Read!)
3. Read the first line: ll_numbytes = FileReadEx(li_filenum,ls_csv_line)
4. Count the number of commas and add one (assumes the line does not contain any quoted string value(s) that contain a comma). Code left as an exercise for the reader.
5. Close and delete the temp CSV file.
Best regards, John
- Friday, 9 June 2023 13:42 PM UTC
- PowerBuilder
- # 3
After looking at the code Andreas posted, it occurred to me that the solution can be obtained in a much simpler manner:
Any la_row
// Place the entire first row of data into a variable of type "Any".
la_row = dw_1.Object.Data[1]
// The "Any" variable now contains an array of Any's.
MessageBox("Crosstab Columns","There are "+String(UpperBound(la_row))+" columns.")
Please note that if the detail band contains any computed field DWObjects (such as a crosstabsum), those DWObject(s) will not be included in the results because they are not data columns.
John
- Andreas Mykonios
- Friday, 9 June 2023 15:40 PM UTC
I tried that but didn't worked for me with crosstabs. :-(
I will try it again.
Andreas.
-
Helpful Loading... Helpful 1
- Andreas Mykonios
- Friday, 9 June 2023 15:42 PM UTC
I just saw you are using any datatype.
I will review that for sure.
Andreas.
-
Helpful Loading... Helpful 0
- Thursday, 8 June 2023 19:18 PM UTC
- PowerBuilder
- # 4
Hi Reynard;
AFAIK, there is no direct way to get the Column Count.
I would have expected CrossTabCount(n) to do that but it does not seem to work. :-(
Regards ... Chris
- Page :
- 1
However, you are not allowed to reply to this question.