Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Incorrect updating of wxGrid when sorting rows using the SetRowPos function #2525

Open
alexkd11 opened this issue Feb 10, 2024 · 0 comments
Open

Comments

@alexkd11
Copy link

alexkd11 commented Feb 10, 2024

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())
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant