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.
Related
I am trying to display items in a grid however the rows and the columns should be generated dynamically. I actually implemented a custom control that derives from Grid control and provide additional properties such as RowCount.
Here is a picture of the grid generated by my custom control using two dimensinal string array as datasource:
But i think my control consumes more than needed resources because it simply destroys column and row definitions and recreates them. Is there any simpler way to implement that control?
You don't need to create a custom control to do that for you... you can use a standard DataGrid. There have been a number of questions on displaying dynamic data in a DataGrid. Please take a look at some of these posts:
How do I bind a WPF DataGrid to a variable number of columns?
DataGridColumn Binding with Dynamically Generated Data
Visualizing 2D Data in a Table
Displaying multidimensional data in WPF
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.
I am assigning a datatable with multiple columns and zero rows to WPF data-grid. I am using auto-generated columns. As there are no rows to display the AutoGeneratingColumn event is not at all triggered. Because of the that it renders Data-grid in a weird manner: one single template row and now columns.
Is there any workaround for this problem? Please guide.
Regards,
Priyank
This is actually by design. The data grid uses reflection internally to infer the columns from the data type that is available in the ItemSource collection. When there are no items it is not possible for the data grid to display the column headers correctly.
There are two possible solution to this:
Bind your grid to a static resource. This way the grid will know the
clr type to which it will be bound and will correctly generate the
columns.
Do not rely on AutoGeneratedColumns ;)
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'm new to Silverlight, so this might be a simple question to answer. I have a grid (not DataGrid from the toolkit), so that data displayed in it can be easily tabulated. I want to bind the grid to a string array that never changes, hence the number of rows and columns in the grid will not change either. I see that I cannot specify a template like I can for a ListBox (ListBox.ItemTemplate) and I don't want to define each row and column explicitly (with a TextBlock in each one of them) in XAML. And I also dont want to generate rows and columns dynamically in code behind as it is not blendable. What's the best way to do this?
You might like this post