TMSFMXGrid sorting / selection problem

I'm using TMSFMXGrid in my application to represent a selection of files. I think I have most things working, but I'm running into two problems.


First problem is that if I select a number of rows (smDisjunctRow style) and sort the Grid, the selection will stick to the location of the Grid and won't move with the row after the sort. For example:

ROW 1: Value A
ROW 2: Value C (SELECTED)
ROW 3: Value B

after a sort

ROW 1: Value A
ROW 2: Value B (SELECTED)
ROW 3: Value C

I would like ROW 3 to be selected after the sort, because that row now contains the data (Value C) that the user actually selected. Is this possible? Or do I need to create this manually? If I need to do this myself, I was thinking about storing the Selected boolean inside my own objects and link them with the Grid->Objects[Col,Row: Integer] property in the OnSelected event. But I can't find any event that triggers when a cell loses selection. Also, this would probably mean that when I sort the Grid, my objects will be linked to the wrong values again, because the Objects[Col, Row: Integer] property links to cells and not the values, so I'm running out of ideas. Was it a mistake to use this Grid with virtual cells for my use case?

Hi, 


This behavior is currently not supported. We will investigate this here to add this in an update.

Kind Regards, 
Pieter

Hi Pieter,


Thank you for the response. I think I have the solution, but I will need to do some more extensive testing. Here is what I did:

I moved the text data into the Grid instead of doing everything with the OnGetCellData. Then I linked the source objects with the Grid->Objects[0][Row] property and use SortFormat = ssRaw. 

I then wrote these functions for the OnCanSortColumn and OnColumnSorted event where SelectedItems is a TObjectList with OwnsObjects set to false. This seems to work. The data selection is now maintained after sorting. I will need to do some testing to see if it is faster to walk through the Grid, or the TObjectList, but this seems plenty fast for my Grid with up to 10.000 rows.


void __fastcall TMainForm::GridCanSortColumn(TObject *Sender, int ACol,
		  bool &Allow)
{
	TTMSFMXGrid *Grid;


	Grid = (TTMSFMXGrid *)Sender;


	if (Grid->RowSelectionCount < 1)
		return;


	if (SelectedItems->Count > 0)
		SelectedItems->Clear();


	SelectedItems->Capacity = Grid->RowSelectionCount;


	for (int i=0; i<Grid->RowSelectionCount; i++)
		SelectedItems->Add(Grid->Objects[0][Grid->SelectedRow]);
}
//---------------------------------------------------------------------------


void __fastcall TMainForm::GridColumnSorted(TObject *Sender, int ACol,
		  Tmsgriddata::TSortDirection Direction)
{
	TTMSFMXGrid *Grid;
	bool CellFocused = false;


	Grid = (TTMSFMXGrid *)Sender;


	Grid->SelectRows(0,0);


	for (int i=1; i<=Grid->RowCount; i++)
	{
		if (SelectedItems->IndexOf(Grid->Objects[0]) != -1)
		{
			if (!CellFocused)
			{
				Grid->FocusedCell = Cell(0,i);
				CellFocused = true;
			}


			Grid->RowSelect = true;
		}
	}


	SelectedItems->Clear();
}
//---------------------------------------------------------------------------

I forgot to add 




	if (SelectedItems->Count < 1)
		return;


At the top of the TMainForm::GridColumnSorted() function. This will make sure application will not run through the Grid if no Selected objects where stored.

Thank you for your investigation. We will take a look at it here as soon as possible.

Sure. Thank you for some great components. It would be faster and more error proof it you had some internal ID to use for rows, but I need these linked objects anyway in my application to find the correct objects to process after the user selects commands from buttons and popup menus.