Sorting a GridView - winforms

I have a question regarding GridView windows forms control. As a datasource, I am using a DataTable object. When the grid view is sorted, the DataTable's row indices are not sorted. In this way, when accessing the DataTable using indices from GridView, I get the unexpected rows. How can this situation be handled?

Without fully knowing how you need to reference it, you can put the table's row identifier in a column and set the column's Visible property to false and have access to it that way.

Related

WPF multiple selection of a nested datagrid into a list

I’m trying to select multiple rows in my nested datagrid and put the rows into a list or at least get all the first column values of the selected rows, but I can’t find anywhere on the web that details how to do that. Is it even possible?
Can someone show me a quick example?
YourDataGrid.SelectedItems;
This will get you all the rows you have selected in code behind, To multi select use Ctrl key while selecting and don't forget to allow Extended selection mode(I guess its the default), put this SelectionMode="Extended" in your xaml.
And you can access rows like this
DataRowView row = (DataRowView)YourDataGrid.SelectedItems[0];
And you can access Cells like this
row["ColumnName"];

Creating an Empty DataGrid

I was wondering what the best way was to create an empty datagrid.
For example after you have hit new in excel, You have a grid with empty rows and columns.
I am using c# with WPF and .net 4.0.
Thank you.
As the comments have suggested, a datagrid is not a spreadsheet, but a method to display / edit existing data. That said if you want something similar, feel free to populate a collection with default / 'empty' objects and bind that to your grid. It just means that after working with the data, you will have to define a method to capture only the edited rows. This still means that the column-bound properties of your class need to be known ahead of time.
A DataGrid is used to display a collection. If you want to create an individual row DataGrid is not really the right tool. You could but a single empty row in the DataGrid using a collection of only on row. There is a lot of guidance on Master Detail on MSDN. If you don't know how many columns at design time you could used a DataGrid to turn the row vertical with column 1 as name and column as value so now you have one record but with a collection of fields.

writing a grid that has both column and row virtualization

I need to write an excel-like grid that can have a lot of cells (400x400). All columns have the same width and all rows the same height. Each cell can contain text or be empty and each cell can have a column and/or row span. I suppose this will never work with the Grid panel and I suppose I will need UI virtualization in both column and row direction.
So my first try was to create a virtualizing grid by deriving from VirtualizingPanel and implement IScrollInfo. This could have "easily" be the solution except that I ran into a problem:
To provide IScrollInfo with the relevant information about scroll size and position and to be able to detemine wich items need to be created (realized) next using the ItemsContainerGenerator, I need to know the column index, row indeox and columnspan for each child item (cell). The only way I can think of to do this is using attach properties. The problem is: I can only read the values of attached properties if the ItemContainer that has them is already realized. So I am in a catch 22 here. To know what to realize I need to realize all items. To provide the data for IScrollInfo I need to realize all items.
So it seems that I am at a dead end with this approach.
Do you have any idea how I could implement a control like this or know how I could reslove the above problem?
It strikes me that you may not need to instanciate the UI elements themselves- you can very easily have a collection of DependencyObject-derived viewmodels, each of which has the WidthProperty and HeightProperty set (and possibly bound to the equivalent Width and Height properties of the visible cell UI element, once those are created).
Storing 160,000 (400x400) class instances shouldn't be a problem, especially if you are able to index into the set using row and column.

Sorting DataTable with WPF DataGrid?

Okay, this is driving me nuts. I've spent hours trying to figure out what should be a simple solution, but I'm having no luck.
I have a [WPF Toolkit] DataGrid on an XAML page that has a DataTable as its ItemsSource. I also have a button on my page that gets the DataGrid's SelectedIndex (selected row) and uses it as a variable in a function that reads the bound DataTable's row at that index and returns a value. Everything works fine until I click a column header to sort it. It sorts the DataGrid but does not sort the DataTable with it, so my SelectedIndex has changed but the index of the DataTable has not, thus it returns the wrong value.
I've looked for Column Header click events - no luck; I tried to get the header of the column by which the grid is currently sorted - nada; I tried to use a "Click" EventSetter on the DataGridTextColumn template - not supported.
I'm completely at a loss. If WPF is supposed to be an improvement over Windows Forms, why has some of the simple functionality been removed? (It's also dumb that you have to bind data just to add rows, just saying.) I can use a Windows Forms DataGrid and won't have any trouble, but then I can't style it.
Maybe I'm not performing the check properly or something...? Below is my retrieval/output code. Anyone have any ideas??? Your help will be greatly appreciated!
DataRow selectedRow = my_data.Tables[0].Rows[my_grid.SelectedIndex];
MessageBox.Show(selectedRow["ItemName"]);
The sorting is applied to the DefaultView of the datatable.So it will not be applied to the Datatable directly.To access the sorted table use
DataTable.DefaultView.ToTable()

Retrieving Cells within a WPF DataGrid Column

I need to get all the cells within a certain column.
there is no property called "Cells" in the GridViewColumn class.
Is there any other way of doing this?
Thanks.
May I ask you the purpose of getting those cells? And when you say you want a cell, do you want the visual of that cell or the data associated with it?
If it is data, then getting it directly from your ViewModel will be a preferred approach. So if you are following proper MVVM while implementing this, you can have a method in the ViewModel which can easily expose you a specific collection of cell-data corresponds to a column
Have a look at http://techiethings.blogspot.com/2010/05/get-wpf-datagrid-row-and-cell.html

Resources