Creating an Empty DataGrid - wpf

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.

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"];

How to properly manage a dynamic table of data for gui

So I need to display a DataGrid in WPF. I'm using RadGridView from Telerik and Caliburn.Micro framework using the MVVM strategy.
In my presentation there are differing number of columns, X+2 to be exact as there is always a Label column and a Total column and then X number of others that hold a decimal number.
I do know the number at runtime before I create the classes I need so that could help.
So I'm currently considering a few methods of doing this but some of them have drawbacks so I would love if someone could point me towards a solution.
Create a ViewModel class for each row and for each Cell. The X number will be a collection and binding to it is problematic. I would need to customize the RadGridView itsefl to actually manage this.
Create a DataTable an the run and bind to it. I haven't managed to make this work and not sure if RadGridView actually supports DataTable. This will also not allow me to keep metadata for each cell, something I need to be easily able to update the cells because column + row aren't the only distinguishing keys I need.
Somehow dynamically create a object with x+2 properties to bind to.
Are there any solutions, guides or something out there to help me here?

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.

Totals Row in a DataGridView

I am developing a winform application application. I wanted to show sum of columns in last row of each column. This row must always be visible.
At moment I am thinking about adding another datagridview just beneath my datagridview with records, and would show the sum in that bottom datagridview.
If there is a better way to achieve this task?
No, need of adding another datagridview
Solution 1: Please refer to this solution
Solution 2: If the above link is not exactly what you want then,
You can try to manually add last, summary, row in which you can display information that you need. For example, you can try to do the following:
Read data from database and fill System.Data.DataTable
Add one column to the newly created DataTable – that column might be set
to true for the last, summary, row
Programmatically add one extra row that contains suitable summary data
Do the data binding to DataGridView control
Using appropriate event, bold or otherwise graphically distinct summary
row (row that have extra column value
set to true)
You can do it in the same way as you suggest, like placing a datagridview for displaying the sum. you can also handle the Horizontal Scroll with this, if there are more columns.
Another method is there in this link
Another way you can Add Rows to your datasource itself to display the sum.
Even if this question is quite old-ish I'd like to propose an extension to Niraj Doshis answer. His answer holds true for a data bound DataGridView. I recently had the problem to calculate the summary in a user-editable DataGridView, whose solution differs in the details. Anyway It's quite straight-forward too. I am writing my functions on a higher abstraction level, which outlines the workflow, but elides the implementation details.
First of all you'll have to initialize the DataGridView, see
private void InitializeDataGridView()
{
SetColumnTypes();
AddEmptyRow();
AddSummaryRow();
}
I set DataGridView.AllowUsersToAddRows to false for the new row would be located beneath the summary row. Hence I am adding an empty row, which the user may fill with his data. The summary row is set to ReadOnly since we do not want our user to edit it. Whenever a CellEndEdit is raised I am updating the DataGridView with the following method
private void UpdateDataGridView()
{
RemoveSummaryRow();
RemoveEmptyRows();
UpdateRowNumbers();
AddEmptyRow();
AddSummaryRow();
}
First I remove the summary and all empty rows (you'll have to take care. If you are trying to delete the row you just edited an exception will be thrown. I've not yet figured out how to do this, but I just made up the solution. I'll amend when I came up with the solution.) Afterwards I'm setting a running number in each of the rows. This is not really required, but a detail of my implementation. At the end I add an empty row again which the user may use to add further data and then calculate and add the summary row.
As I said before this is not yet a ready-made solution, but rather I concept which works but with some quirks and bugs.

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