I have an application in C# + WPF and I need to fill a datagrid.
I tried to fill the DataGrid using its property ItemsSource.
DataGrid.ItemsSource = <ElementList>;
Where ElementList is a List of strings.
When I run it, it generates a row for each element of the list but it does not show anything.
Does your data have properties? Fields will not work, also AutoGenerateColumns needs to be true unless you created columns manually.
Related
I am working on a silverlight application using MVVM. My requirements is to display existing user data in data grid so that first two columns remains non-editable and rest will be editable.
At start datagrid loads data from database, at that point if user click on data grid first two columns should be non-editable.
After that user insert a new row (i create a button, when that is clicked a new row is added at the bottom of the grid) all columns should be editable including first two. Now user can click Add row buttons more than once, point is rows created by Add button click should be editable.
I am stuck at this problem since yesterday any help would be great!
I dont know Silverlight but I think the following should work:
On your command to add a new row, set a flag like "AllRowsEditable" to true and throw a PropertyChanged for this Property. In your view you bind the IsReadonly property of the first two columns to that "AllRowsEditable" property.
EDIT: Write a ViewModel for your DataGrid items. For example "RowViewModel". To have a good structure I would introduce two properties like "IsFirstPropertyReadOnly" and "IsSecondPropertyReadOnly" in that ViewModel. "...firstProperty..." is your properties name. In your XAML you can bind to this properties. In your first initialization you load the items and set the property values to true. All items added after that you set that properties to false.
Scenario
ComboBox C depends on the selected value of ComboBox B, which depends on the selected value of ComboBox A. All of these ComboBoxes are in a DataGrid.
Common Road Blocks:
The User must be able to add new rows (This Requires a ItemsSource item type that has a parameterless constructor).
To access the database to populate the List of Available Options for the Comboboxes, the current project would require the Database Credentials/DataContext be passed into the Constructor.
Attempt 1
I've tried using 3 CollectionViewSources, one for each of the ComboBoxes (Concept Code Here), but the SelectedItem of the ComboBox gets automatically selected in the other DataGrid rows. I need to find a way to isolate the CollectionViewSource to each row.
I've considered just adding the CollectionViewSource data to each of the DataGrid Items so I can just bind to it that way, but I have to access the database to generate the CollectionViewSource.
I also tried not sharing the CollectionViewSource as seen in this question, but that destroyed the link between the 3 ComboBoxes, as well as the Rows. If I could just set the CollectionViewSources to be shared within each DataGrid Row and not between each of them, I think it would work. I just can't find a way to do that.
Attempt 2
I've looked at this question: How to get cell level ComboBox for WPF DataGrid?
This would work, but the User needs to be able to add rows to the DataGrid. The example code in that question also uses a parameterless constructor. I am in a situation where access to the database to populate the lists would have to be passed into the constructor.
The Question
How do I do this correctly?
Bind to a List or ObservableCollection, not a CollectionViewSource
CollectionViewSource tracks the Current Item, so changing the value in one ComboBox will change it in all ComboBoxes
I've encountered a problem trying to add items to WPF DataGrid. I want to load a matrix of M x N at runtime and place all the elements in the grid. However I've found only solutions that use ItemSource property or Binding for columns and as I understand they do not cope, because I need to create a predefined class. How could I accomplish that?
Column binding is only used when AutoGenerateColumns is set to false, so you've declaratively specified the exact columns you want and therefore have to also specify where they get their data from. (Good tutorial here).
You should set the datagrid's ItemsSource to an IEnumerable of objects - this means you can use a List, an array of your objects, or a straight DataTable.
If you need to be totally dynamic with the columns in the datagrid, then either set AutoGenerateCOlumns to true, or write some logic to programmatically create and add the columns when appropriate.
I have a DataGrid that is bound to a List (ListOne). Inside the grid I'd like to display an extra TextBox that contains fields found in another Class that is not the same as the Class used in ListOne.
I've done this before with a ComboBox using the DataGridTemplateColumn, the problem is I'm not sure how the Binding would work when using a TextBox?
ListOne contains code and description which is bound and displays correctly. My SQL Stored Procedure returns extra values using a Join, which I'd like to display as editable fields ListOne.
Any ideas?
You should probably try to bind the DataGrid directly to the joined data as i imagine that doing joins directly in the cells may be difficult and round-about.
I was wondering if there is a cohesive trick to populate a DataGrid in a WPF Application with some empty rows so the user can leave some rows empty and fill some others following to them?? my DataGrid is Databound to a CollectionViewSource
Create some dummy rows in the Source collection referenced by the CollectionViewSource.