开发者

DataGridVeiw (WinForm) - Determine when rows have been reordered

In one of our WinForm Apps, we are using a DataGridView with Column Sorting and SortDescriptions enabled.

If the user modifies one of the cells that is being sorted by, the sorting is applied and the grid paints the rows in the new order.

I need to access the DataGridRows BEFORE they are painted, but after they have been sorted. I know I could use DataGridView.RowPrePaint(), but that seems like overkill. It fires when the mouse hovers over certain cells, etc. I just need to get to the Rows when the ordering has changed. I thought maybe I could handle the "Sorted" event on the DGV, but that only fires after the user clicks one of the column headersm but I need to get to the rows when the DGV applies any of the existing Column Sorts or SortDescriptions. Basically, the Rows could be reordered (sorted automatically) after one of the Cells is edited the DGV is sorted开发者_JS百科 on OR after the row is mofified via a RowEdit "Template".

I have googled this quite a bit today and haven't come up with any suggestions.

Any advice? (Other than just handling the DataGridView.RowPrePaint Event....)


I am not sure if it works, but if I were you I would keep the RowPrePaint() event.

Set a flag

bool RowSorted;

Set the RowSorted variable true in the sorting function and again fire the RowPrePaint Event in sorting function after the rows are sorted.

In the RowPrePaint function I would check the flag. If true, paint the rows; if false e.Handled = true;


I found the answer. This is the exact problem I am facing (from msdn):

When a DataGridView control containing both bound and unbound columns is sorted, the values in the unbound columns can't be maintained automatically. To maintain these values, you must implement virtual mode by setting the VirtualMode property to true and handling the CellValueNeeded and CellValuePushed events.

For more information, see How to:Implement Virtual Mode in the Windows Forms DataGridView Control. In addition, sorting by unbound columns in bound mode is not supported.

Hope this helps.

Sincerely, Linda Liu

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜