FNCGrid saving column sizes in runtime

In vcl stringgrid exist the ColumnSize.save method to saving column sizes in runtime.

I need do same with FNCGrid. Exist a method to do this ?

You can use the following methods to save and load:

  TMSFNCGrid1.ColumnStatesToString;
  TMSFNCGrid1.StringToColumnStates()

thanks.

Hello Pieter,

is there a similar function for TMSFNCDataGrid? As the mentiond methods doesn't exist.

Hi,

With TTMSFNCDataGrid, you can save the widths to JSON:

s := TMSFNCDataGrid1.Root.ColumnWidthDictionary.JSON;

It will look something like:

'[{"2":100},{"1":250},{"0":70},{"3":100}]'

and then restore them with

  TMSFNCDataGrid1.BeginUpdate;
  TMSFNCDataGrid1.Root.ColumnWidthDictionary.JSON := s;
  TMSFNCDataGrid1.EndUpdate;

Thank you. But this doesn't really work. As we are using C++ Builder, there is no JSON-Property (or at least i don't know how i can get to it).

So i tried to implement it myself:

	String strFileToSave{TF::TempPfadErmitteln() + L"\\Spaltenbreiten.json"};
	TWolfJSON json{};

	for(int nSpalte{0}; nSpalte < this->DataGrid->ColumnCount; ++nSpalte)
	{
		json.AddNumber(IntToStr(nSpalte), this->DataGrid->ColumnWidths[nSpalte]);
		if (nSpalte == 5)
			break;
	}

	auto upStringList{std::make_unique<TStringList>()};
	upStringList->Text = json.ToJSON();
	upStringList->SaveToFile(strFileToSave);

This generates a JSON-File and saves it. Which works, except, that the ColumnWidth is always the one, that was set in the Designer and not the actual width, that the user set.

That's strange, the ColumnWidths property should, at all times, hold the exact width the column has, even when the user has set it at designtime or changed it at runtime. If it's not the width you restore then it's overwritten. Are you sure you have the exact number of columns required to set the widths? When are you loading the JSON?

Sorry, my fault. I just realized i had a second grid, that was overwriting the values of the first one :man_facepalming:

No worries.