开发者

WPF DataGrid - Disable Sorting and Filtering while Editing Rows?

Has anyone tried to implement the following scenario? I would be interested in learning what solutions are out there to accomplish this. This has become a real pain for our users...

We have a WPF DataGrid that gives the user the ability to filter their data rows based on Column (where Column = Value or Begins With, Ends With, Contains, etc etc). They can also sort the data by clicking on one of the Column headers or we also provide them a sort window where they can add multiple columns and choose Ascending/Descending for each column (which i开发者_C百科s simply applied to the "SortDirection" property on each Column they choose, it also seems to use a "CollectionView" along with SortDescriptions).

The issue is when a user is editing data and has a "filter" or "sort" (or both) applied, if the user edits the value on one of those filtered/sorted columns, the row will "move" and take the cursor along with it. If the new value is a value that is "filtered", then the row will disappear entirely (as it should, due to the "filtering" applied by the user).

They would like either one of two things to happen instead:

1 - Go ahead and automatically sort and filter after each cell is changed, if the row "moves", move the cursor to the next row (same cell) instead of the cursor following the row to its new desitation (and possibly scrolling the current viewable data rows off the screen).

2 - As soon as a row is edited, disable all sorting and filtering until the user clicks "Save" or "Refresh" (or some other option)?

I can also see where we might take one approach for filtering and another for sorting.

Does anyone have a better idea or have any ideas on how to implement this?

I have a couple of ideas (For instance, to disable the sorting: in my CellEditEnding event handler, I could store the users sort options, clear them from each column and then reapply them when the user clicks Save/Reset or maybe I could handle the "Sorting" event and set "e.handled" to true, when the data is being edited??). I am just so new to WPF and the DataGrid, I am wondering if there might be a "best practice" out there or a property that I just haven't found such as "DisableSorting=true/false" (lol).

Thoughts? Opinions?


I just had the same problem and seem to have found a solution. It's simple - just add another SortDescription as a secondary sorting criteria.


I decided to try to tackle this from a different angle, based on a post from a Microsoft Employee. Here is what I have tried... It looks like my only remaining option is to create my own DataGrid and override the appropriate methods.

I decided to add a button that will "lock" the DataGrid. At that point, I would disable all sorting, filtering, removing columns, etc etc. Then I would copy the DataTable and change the DataGrid.DataContext to my new Table, when they are done editing, they would "unlock" the DataGrid, at this point I would do a DataTable.Merge() and rebind back to the Master/Source. All their changes would be in the "master" DataTable and they would be able to edit rows and columns without the rows jumping (sorting) around on them or disappearing (filtering).

Then I realized a few things. First, I will need to use the DataTable.DefaultView and get the "filtered" view of the master/source DataTable and assign that to the DataTableCopy (so the filtered rows don't appear in the DataTable they are going to edit). That shouldn't be a problem, I don't think.. Then, I realized something that will be a problem. Once they get the data filtered and sorted like they want it, then I copy it and rebind the DataGrid to the copied table (which won't contain their hidden or filtered rows), the data will no longer be sorted properly. Right??? How can I make it be sorted properly, BUT not resort the DataGrid as they edit the rows and columns?

As you can see, I have basically come full circle to the original problem! If I could just somehow "disable" filtering and sorting, but keep the data ordered and filtered!

I don't have too much experince with this, but couldn't I (in theory) use a disassembler (Reflector? while it is still free..lol...) and build my own DataGrid Class? I would ONLY need to override whatever method is used to do the filtering/sorting. Right? I have browsed around the DataGrid Class a bit, but can't even figure out which methods to "override". I think there is a way to set Reflector up so you can step into (debug) the disassembled code. It looks like this might be my only option.

Any other suggestions??

UPDATE: My assumption was wrong! It looks like if I use DataTable.DefaultView.ToTable(), it not only "preservers" the filtering, but it preserves the "sort order". This solution looks like it is going to work afterall!

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜