wpf grid control for adding dynamic data - c#

What is the WPF control I need if I want the user to be able to input Cross Ccy amounts, ie the data needed for each row is
Ccy One, Ccy Two, Amount
I want a grid-like control where the User can enter data for each cell in the row and once you begin to enter data in the cells, a new row gets added underneath, so the control keeps growing with every entry the user inputs and has no upper limit but grows to fit, using a scrollbar when it goes outside the bounds of the grid container.
Is there a built in control to do this? Or do I have to add functionality to listview/datagrid?

If you want the user to be able to add new rows just set the CanUserAddRows property on the DataGrid to true.
<DataGrid CanUserAddRows="True" ..../>
If you want rows the be added when the user edits a data in a cell in an existing row, you can register to one of the cell edit events (depending when you want the new row to be added) and add rows to the grid or items to the collection it is binded to.
datagrid.CellEditEnding += (grid, args) =>
{
datagrid.Items.Add( ....);
};

this is the standard behavior of a datagrid, if the property CanUserAddRows is set to True

Related

How can I change the cell content in a ListView through code?

I'm dynamically creating a WPF window with a grid-like presentation. There is a header row that contains the column headers. It must always be visible. Below that there is a large number of rows (like hundreds) that can be vertically scrolled. Each row contains a text in the first column and a checkbox in each remaining column.
The list of columns and rows is dynamic, so creating a viewmodel class and binding with templates doesn't work (or would at least be very complicated and require much code while giving up the dynamic nature of the problem). Also, with each checkbox interaction in one row, all other rows might be affected (for the same column) and need to be updated. This would need a lot of interaction between all individual row viewmodels.
I'm looking for a solution to create the columns and rows of an existing empty ListView control and populate all of its data, checkboxes and interaction entirely in C# code.
Specifically I'm missing some ListView method that lets me set the content of a specific cell. While I can add columns to the ListView's GridView.Columns collection, I can't go anywhere from there without templates and bindings. Is is possible to use WPF like this?
The problem is that when I use a template for, say, a checkbox column, all checkboxes do exactly the same an no customisation is possible anymore because I don't create each checkbox, instead WPF creates them for me.
You may create a specific CellTemplate for each GridViewColumn using either XamlReader.Parse:
const string Xaml = "<DataTemplate xmlns=\"http://schemas.microsoft.com/winfx/2006/xaml/presentation\" xmlns:x = \"http://schemas.microsoft.com/winfx/2006/xaml\">" +
"<CheckBox Content=\"{Binding Name}\" IsChecked=\"{Binding IsChecked}\" />" +
"</DataTemplate>";
column.CellTemplate = XamlReader.Parse(Xaml) as DataTemplate;
...or a FrameworkElementFactory:
FrameworkElementFactory checkBox = new FrameworkElementFactory(typeof(CheckBox));
checkBox.SetBinding(CheckBox.ContentProperty, new Binding("Name"));
checkBox.SetBinding(CheckBox.IsCheckedProperty, new Binding("IsChecked"));
column.CellTemplate = new DataTemplate() { VisualTree = checkBox };
This lets you customize the columns but still use the GridView and its functionlity the way you are supposed to. Trying to modifying the generated visual elements is not recommended, especially not if you are using the UI virtualization.

break in refreshing my custom combo box column in RadGridView

I have created a custom grid column for a RadGridview which based on the row DataItem which is a class, finds the corresponding items and put them in the Combobox.
When the page loads, the custom column is freeing to be created only for the rows that are in visual and for other rows, it has been fired to be created when the user scrolls down. Unfortunately, when the user scroll the wrong combo boxes are created for each row.
For example, run the below sample and see the combobox in front of dimensionless row, which must be empty which is correct. Now scroll down and click on a cell and scroll up to the dimension row, which now have a Combobox with values which is erroneous. Note that the dimensions combo box is shifted up now!
Why the combo boxes are wrong after scrolling or windows size change?
It is Virtualization which is causing the behaviour you describe.
You can disable this on a RadGridView like so:
EnableRowVirtualization="False"
This will cause all your data to load whether it's in view or not, and if you have a lot of data this can cause significant performance issues.

creating input fields row automatically in wpf

i am making the form for sales system so that user write data in one row and automatically row adds to add data for other products, i have a datagrid in wpf and i have input fields in datagrid, there are 6 fields in one row, what i need when i write in first field then automatically inputs fields row adds under the current row for new data.
Set CanUserAddRows="True" on datagrid, it will give you the functionality of automatically adding an empty row once the user is done editing the current row.
Thanks

DevExpress XtraGrid: Make column visible to user

I have a grid with lots of columns (ca. 100). I've written a column selector context menu (which has each letter of the alphabet and then as subitems all the columns beginning with that letter).
When the user clicks in the context menu I want to make the column they have chosen visible to the user (preferably in the middle of the visible grid). I don't want to actually mess with the column order, I just want to make sure a column is visible to the user.
Any ideas?
This can be done using the following approach:
1) set the column's Visible property to true.
2) if you want this column to be in the middle of the grid, set its VisibleIndex property to gridView.VisibleColumnsCount / 2;
3) call the GridView's MakeColumnVisible method to make this column visible to the end user.
Use the GridColumn.VisibleIndex property to change the order in which columns are displayed.
VisibleIndex = -1 hides a column IIRC.

DataGridView control scrolls to top when a property changed

On a Windows Form I have a DataGridView control with records that is filled by a data source (data binding). Each record presents a data object.
Not all the rows are displaying: only the first 10 for example. So the user can scroll down to see the another records. Nothing special about this.
But when a user clicks on a row after scrolling, a data property of the object of row is changing and this refreshes the DataGridViewand - it "scrolls" to top of datagrid (maybe the whole DataGridView is refreshing). This is not desirable.
How can I keep the current scroll position during a record update?
You can use the DataGridView's FirstDisplayedScrollingRowIndex property.
It gets/sets the index of the first row displayed on your DGV.
Use it like this:
int rowIndex = dataGridView.FirstDisplayedScrollingRowIndex;
// Refresh your DGV.
dataGridView.FirstDisplayedScrollingRowIndex = rowIndex;
Of course this won't work quite right if sort or add/remove rows to your DGV (you did say you were updating so maybe you're OK).

Categories

Resources