DataGridGetCellLayout is Formatting the wrong cell when columns are moved

For the example to work take the following steps:

  • Drop a TMSFNCDataGrid onto a Form
  • Change the name to DataGrid
  • Write Joined into the name-property of the second column (Joined)

Constructor with the following code:

__fastcall TForm67::TForm67(TComponent* Owner)
	: TForm(Owner)
{
	this->DataGrid->MoveColumn(2, 0);
}
//---------------------------------------------------------------------------

In the DataGridCellLayout-Event the following code:

void __fastcall TForm67::DataGridGetCellLayout(TObject *Sender, TTMSFNCDataGridCell *ACell)
{
	auto pColumn = this->DataGrid->Columns->Items[ACell->Column];

	if (pColumn->Name == L"Joined")
	{
		ACell->Layout->Fill->Color = clRed;
	}
}
//---------------------------------------------------------------------------

Expected behavior: The Joined column has red background. Actual behavior: the Name column has red Background:

I ended up with this "workaround" which does what i want to achieve:

	auto pColumn = this->DataGrid->Columns->Items[ACell->Column];

	if (pColumn->Name == L"Joined")
	{
		int nIndex{this->DataGrid->Root->RealToDisplayColumn(pColumn->Index)};
		this->DataGrid->Layouts[nIndex][ACell->Row]->Fill->Color = clRed;
	}

But in my opinion ACell->Layout should format tha right cell?

This is by design, the indexes don't change, so RealToDisplayColumn is the correct workaround.

Hi,

We re-evaluated this an noticed we applied fixes for the column moving. We have not yet released this, so you might want to await the next release. Then, you will be able to remove the RealToDisplayColumn since the move operation will properly transfer column properties.

1 Like