Phoenix icon indicating copy to clipboard operation
Phoenix copied to clipboard

Incorrect updating of wxGrid when sorting rows using the SetRowPos function

Open alexkd11 opened this issue 1 year ago • 0 comments

Operating system: Windows 8.1 wxPython version & source: wxPython 4.2.0 Python version & source: Python 3.9

Description of the problem: To sort table rows by a selected column, I make a list of column values and rearrange row positions (SetRowPos) using the sorted list. When sorting in ascending order everything works well, but when sorting in descending order further redraws of the table truncate it (and the ability to scroll lower with the scroller) to the former last row (the row with the highest ID). Repeated direct sort restores the table's functionality. Dragging the last line to the beginning with the mouse also leads to the same problems. However, there is no similar problem for rearranging columns. I assume that when redrawing, the calculation is carried out from the line with the largest ID, not the lines with the new last position.. Code analysis showed that the problem is probably just in wxWidgets. In file wxWidgets/src/generic /grid.cpp in function void wxGrid::CalcDimensions()

// compute the size of the scrollable area
int w = m_numCols > 0 ? GetColRight(GetColAt(m_numCols - 1)) : 0;
int h = m_numRows > 0 ? GetRowBottom(m_numRows - 1) : 0;

The last line should probably also have GetRowBottom(GetRowAt(m_numRows - 1))

Code Example (click to expand)
	def GridColSort(self, col, ascending):
		dataForSort = {row: self.metaData.GetCellValue(row, col) 
				for row in range(self.metaData.GetNumberRows()-1)}
		for index, row in enumerate(sorted(dataForSort, key=lambda x: dataForSort[x], reverse=ascending)):
			self.metaData.SetRowPos(row, index)
			
	def OnGridColSort(self, event):
		col = event.GetCol()
		self.GridColSort(col, False if self.metaData.GetSortingColumn() == wx.NOT_FOUND else self.metaData.IsSortOrderAscending())

alexkd11 avatar Feb 10 '24 20:02 alexkd11