I have a TAdvStringGrid bound to a database table that I'm trying to preserve user changes to column widths. When I use the column state functions I'm getting a "List index out of bounds (1)" error message. This occurs even if I try to re-apply the exact string returned by the .ColumnStatesToString function:
SettingsTBL.Open;
SettingsTBL.Filtered:=false;
SettingsTBL.Filter:='[Setting] = '+quotedstr('subjtests_columnsettings');
SettingsTBL.Filtered:=true;
if(SettingsTBL.RecordCount>0)
then begin
SettingsTBL.Append;
SettingsTBL.FieldByName('Setting').AsString:='subjtests_columnsettings';
end;
ColumnSettingsStr:=subjecttests_grid.ColumnStatesToString;
SettingsTBL.FieldByName('Value').AsString:=ColumnSettingsStr;
SettingsTBL.Close;
subjecttests_grid.StringToColumnStates(ColumnSettingsStr); <- This is where error occurs
The string looks like this: 15#37,64,64,64,64,64,64,64,64,64,64,64,64,64,122#0#1,0,0,0,0,0,0,0,0,0,0,0,0,0,0
Have also tried to use ColumnSize, which appears to save the widths in an ini file when program closes but does not restore widths.
Any suggestions or comments would be appreciated!
At the moment your grid has the default column settings, do you first call grid.SetColumnOrder before than using the StringToColumnStates , ColumnStatesToString functionality?
Hi guys. I am getting this exact same error. Everything works to the first time, but the second time (when the user performs a different search and I rerun my query and repopulate the grid) it fails. Same code; second pass. I have tried to add .Clear() and .ResetColumnSortOrder(). The exception is thrown during .ResetColumnSortOrder().
// save user column positions and widths
if (recomFollowupQuery->Active) {
ReportHeaderForm->followupGridCols = followupResultsGrid->ColumnStatesToString();
}
recomFollowupQuery->Active = false;
followupResultsGrid->Clear();
followupResultsGrid->ResetColumnOrder();
followupResultsGrid->RemoveAllColumns();
recomFollowupQuery->SQL->SetText(sql.w_str());
recomFollowupQuery->ParamByName("DECLINEDBEFORE")->AsDate = ReportHeaderForm->declinedBefore->DateTime;
recomFollowupQuery->ParamByName("DECLINEDAFTER")->AsDate = ReportHeaderForm->declinedAfter->DateTime;
recomFollowupQuery->Active = true;
followupResultsGrid->SetColumnOrder();
// restore user column positions and widths
if (ReportHeaderForm->followupGridCols.Length()) followupResultsGrid->StringToColumnStates(ReportHeaderForm->followupGridCols);
Hey guys. I have found a solution/workaround for my code, at least. Wanted to post so it might give others a fix or might give you an idea what might need fixing in the grid code.
followupResultsGridd->FilterActive = false;
ResetColumnOrder() causes a grid that is not in default order (i.e. at the time SetColumnOrder() was called), to reorganize its columns to bring it back to default order. Here, you set your dataset inactive, so columns & data are removed from the grid, so calling ResetColumnOrder() doesn't make sense anymore.
So, did you try with just removing the call to ResetColumnOrder()?
I retried using .SetColumnOrder before saving and before retrieving the column string. Did not properly restore but saw hint - columns are being marked as hidden that are not. I manually edited string saved in database (setting all 0 to 1) and that appears to allow column resizing to work.
Not sure what caused the incorrect hidden marked columns. If a problem persists, can you please provide detailed steps with which we can reproduce this issue here so we can investigate?
Hi Bruno,
When you start a new query, will that query return the same set of fields as the query with which ColumnStatesToString() was called? For ColumnStatesToString() and StringToColumnStates() to work, it is of course necessary it concerns the same nr. of columns that is being added to the grid.
Back to the original post - I was able to get a workaround for the hidden columns by having the program get the column string then change the string after the last '#' to all 1s. When I do that I'm able to restore column settings between runs.
Yes Bruno, in case 1.
Please isolate and provide some sample source app with which we can reproduce this issue here. This is the most efficient way to have a detailed & complete view on what you do in the app and either suggest how to handle it correct or find a fix (when needed)
Troy
Have you looked at the string that ColumnStateToString returns? Do you see 0s in the terminal third as I indicated in my post? If so try replacing the 0s with 1s and see if the index error disappears.
You might also be able to modify the string to deal with the new query issue, removing or inserting elements in each of the three sections to match new number of columns. Assume there's some similarity that between the columns in the two queries even if number of column changes.
Neil