Category: Wpf datagrid dynamic rows and columns

Full code is accessible on GitHub Blog repository. Wpf application is done in MVVM pattern with one main window. Dynamic grid is implemented as user control that contains DataGrid control bound to observable collection of collections of cell view models. In this implementation collection of cells is recreated each time if grid width or grid height is changed, and it leads to some application pauses.

In the following post this issue is solved with asynchronous method that updates cell array. Circ.

n. 123, other implementation for cells could be used, for example, 2-dimensional array of cells ICellViewModels[][] works well. Values of color properties are assigned to corresponding properties of CellView control. View model for each cell implements ICellViewModel interface that defines property for data model that implements ICell interface and command for changing state for the cell. Cell height equals to actual height of data grid minus 2 divided by number of rows.

The same width of cells is controlled by Width property of cell data template:. Similarly, cell width equals to actual width of data grid minus 2 divided by number of columns.

And there is a definition of DataGrid control:.

wpf datagrid dynamic rows and columns

All used IP-addresses, names of servers, workstations, domains, are fictional and are used exclusively as a demonstration only. In the previous post Wpf data grid is considered such that it has dynamically defined number of rows and columns but all […]. You are commenting using your WordPress.

You are commenting using your Google account. You are commenting using your Twitter account. You are commenting using your Facebook account. Notify me of new comments via email. Notify me of new posts via email. This site uses Akismet to reduce spam. Learn how your comment data is processed.By using our site, you acknowledge that you have read and understand our Cookie PolicyPrivacy Policyand our Terms of Service.

Stack Overflow for Teams is a private, secure spot for you and your coworkers to find and share information. I'm new to WPF. The way we used to do it in windows forms. I have No. I had the same problem. DataGrid relies on property fields of an item object.

ExpandoObject enables to add new properties dynamically. The code below explains how to do it:. Note that this is not the way how the component should be used, however, it simplifies a lot if you have only programmatically generated data eg. Unfortunately, with 47 columns defined this way, it doesn't bind to the data fast enough for me.

Any suggestions? It was a neat solution, although complex. I posted a sample project describing how to use PropertyDescriptor and lambda delegates with dynamic ObservableCollection and DynamicObject to populate a grid with strongly-typed column definitions.

If your data is not a object with known type, you could create a data structure that would enable access by any number of columns and specify a PropertyDescriptor for each "column". Then you just bind to Users collections and columns are autogenerated as you speficy them.

Strings passed to property descriptors are names for column headers. At runtime you can add more PropertyDescriptors to 'Users' add another column to the grid. If you already have the databinding in place John Myczek answer is complete.

If not you have at least 2 options I know of if you want to specify the source of your data. But I think you should use your own defined collection instead of a weakly typed DataTable no offense, but you can't tell from the code what each column represents. If your type has a property FirstName defined, you can then do what John Myczek pointed out. This obviously doesn't work if you don't know properties you will need to show in your dataGrid, but if that is the case you will have more problems to deal with, and I believe that's out of scope here.

How are we doing? Please help us improve Stack Overflow. Take our short survey. Learn more. Asked 11 years ago.Datagrid controls are great for displaying data that is stored in a table.

One row in the database table equals one row in the datagrid. When data is stored in multiple tables, say table A and B, and the row A has a one-to-many also called 1:N, parent-child, or master-detail relation to a row in table B, then row A can reference multiple rows in table B.

This type of data can be shown in a master-detail data view type. Another type of data relation is the type many-to-many also called N:M relation. The row of table A can have multiple references to rows in table B. But in addition to the previous case, a row of table B can be referenced by multiple rows of Table A. This article describes a method many-to-many relations can be displayed and modified in a WPF datagrid control. This article is split in two parts.

In this first part, I focus on the solution of handling dynamic columns. In order to simplify the solution, I broke an architectural constraint, which is that objects of a top layer should not be used in lower layers in this case, the grid columns that are a part of the GUI layer, and not the view model layer. The second part of this article fixes this constraint. The sample code implements a user administration form in which users, roles and the user-role assignment can be administered.

The roles and the users are displayed in two data grids. The user role assignment is done in the user data grid. Therefore this grid has the dynamic contents, displaying each role as a separate check box column. The user-role assignment is done by checking the respective check box.

An entry in the UserRole table means that the user referenced by its user id has a role assigned referenced by the role's id. If there is no entry for a certain user-role combination, then that means that the user in question does not have the corresponding role assigned.

The data model is implemented using the. NET DataSet. It is a good in-memory database with referential integrity, and it contains built-in notification delegates that publish the insertion, removal and modification of data rows. The application is written using the MVVM design pattern.Posted by: admin November 29, Leave a comment. The way we used to do it in windows forms. I have No. I had the same problem.

DataGrid relies on property fields of an item object. ExpandoObject enables to add new properties dynamically. The code below explains how to do it:. Note that this is not the way how the component should be used, however, it simplifies a lot if you have only programmatically generated data eg.

Any suggestions? It was a neat solution, although complex. I posted a sample project describing how to use PropertyDescriptor and lambda delegates with dynamic ObservableCollection and DynamicObject to populate a grid with strongly-typed column definitions. Then you just bind to Users collections and columns are autogenerated as you speficy them. Strings passed to property descriptors are names for column headers. If you already have the databinding in place John Myczek answer is complete.

If not you have at least 2 options I know of if you want to specify the source of your data. If your type has a property FirstName defined, you can then do what John Myczek pointed out. February 21, c Leave a comment. Questions: Closed. This question needs to be more focused.

Dynamic Columns in a WPF DataGrid Control (Part 1)

It is not currently accepting answers. Want to improve this question? Update the question so it focuses on one problem only by editing this pThis article demonstrates the creation of a DataGrid column, the cell style and adding a data trigger into the DataGrid. Normally at design time when we create a DataGrid, we also create the required columns or set the autogenareted column property of the DataGrid to true to create automatically generate the columns based on the bound item source.

Suppose you need to create a DataGrid without knowing how many columns you to need to create and you don't know the columns names with their binding value and other properties like cell background color and so on.

In the preceding case this article will help you. This article creates a DatagridColumn at runtime having other properties, like header, cell style, Trigger and so on.

In this article we will learn the following points:. To create a DataGridColumn at run time you will require the following namespaces. These namespaces help to create controls, binding required to bind a property with a DataGrid cellStyles, Triggers and so on. Creating DataGridColumn. Bind property. Adding cell style in the DataGrid at runtime. At the design time, to apply a style to the datagrid cell, for example foreground and background colors, we are doing the following XAML code. But if we want to set colors with the DataGrid cell at runtime then we are binding the foreground and background properties with a property of datacontext or with a control's background or foreground property like the following XAML code:.

In the code above the foreground of the DataGrid is set from the background property of the TextBox having the name txtfont and the background is set from the background property of the TextBox txtBackground. The binding mode is two-way because when the background changes both the text box of the cell background and foreground will be changed automatically. To add the cell style at runtime as in the preceding XAML code, you will need to use the style and setter class that comes under the System.

Windows namespace. This class will help you to set the background, font style and Add trigger and so on. A style consists of a list of setters.

If you apply this style to an element it sets all properties with the specified values. The following code accomplishes the same thing that is specified in the preceding XAML code. Adding trigger into DataGrid at runtime. We require triggers when we want something to change when something happens in the application.

For example we want the background color of a DataGrid cell to change when a value changes that meets a specific criteria.

This article has explained how to add a data trigger to a DataGridcell to change the background color. Now we will do the same thing using C code. We will need a datatrigger class from the System. Data namespace. View All. Kailash Chandra Behera Updated date, Jul 16 Controls System. Data System. Now we will create the same thing using C code. Use the namespace System. Controls in your window class if it is not used.

Suppose my DataGrid name is " dgvRates" as in the following:. Next Recommended Article. Getting Started With. NET 5. Getting Started with ML.This article demonstrates the creation of a DataGrid column, the cell style and adding a data trigger into the DataGrid.

Normally at design time when we create a DataGrid, we also create the required columns or set the autogenareted column property of the DataGrid to true to create automatically generate the columns based on the bound item source. Suppose you need to create a DataGrid without knowing how many columns you to need to create and you don't know the columns names with their binding value and other properties like cell background color and so on.

In the preceding case this article will help you. This article creates a DatagridColumn at runtime having other properties, like header, cell style, Trigger and so on.

Grid Layout in WPF

In this article we will learn the following points:. To create a DataGridColumn at run time you will require the following namespaces. These namespaces help to create controls, binding required to bind a property with a DataGrid cellStyles, Triggers and so on. Creating DataGridColumn. Bind property. Adding cell style in the DataGrid at runtime. At the design time, to apply a style to the datagrid cell, for example foreground and background colors, we are doing the following XAML code.

But if we want to set colors with the DataGrid cell at runtime then we are binding the foreground and background properties with a property of datacontext or with a control's background or foreground property like the following XAML code:.

In the code above the foreground of the DataGrid is set from the background property of the TextBox having the name txtfont and the background is set from the background property of the TextBox txtBackground. The binding mode is two-way because when the background changes both the text box of the cell background and foreground will be changed automatically. To add the cell style at runtime as in the preceding XAML code, you will need to use the style and setter class that comes under the System.

Windows namespace. This class will help you to set the background, font style and Add trigger and so on. A style consists of a list of setters. If you apply this style to an element it sets all properties with the specified values.

The following code accomplishes the same thing that is specified in the preceding XAML code. Adding trigger into DataGrid at runtime. We require triggers when we want something to change when something happens in the application.

Getting Started with the WPF DataGrid of Syncfusion

For example we want the background color of a DataGrid cell to change when a value changes that meets a specific criteria. This article has explained how to add a data trigger to a DataGridcell to change the background color. Now we will do the same thing using C code.

We will need a datatrigger class from the System. Data namespace. View All. Kailash Chandra Behera Updated date, Jul 16 Controls System. Data System. Now we will create the same thing using C code.

wpf datagrid dynamic rows and columns

Use the namespace System. Controls in your window class if it is not used. Suppose my DataGrid name is " dgvRates" as in the following:.

Next Recommended Article. Understanding ASP. Getting Started With. NET 5.The post is devoted to the Wpf datagrid with cells that have defined fixed size but a number of rows and columns is updated dynamically in order to fill all available space. For example, such grid could be used in games at infinite 2D field or implementation of cellular automaton.

Solution uses C 6. Net 4. Wpf application is done in MVVM pattern with one main window. View model of dynamic data grid contains cell, view and grid sizes, data model for cell set, and collection of collections of cell view models.

View size properties are bound to actual size of data grid control.

wpf datagrid dynamic rows and columns

Actually, it is not a clear approach from the point of MVVM pattern, as view model should know nothing about view, but it is realized in accurate way via binding and attached properties. Grid size, i. As number of rows and columns are integers, real size of cells on the view could not equal to values of cell width and height.

After control size is changed and number of rows and columns of grid are calculated, cell set is recreated, but state of cells are preserved. Then collection of cell view models is updated by asynchronous method.

wpf datagrid dynamic rows and columns

Method analyses necessary changes and remove or add rows and remove or add cell view models to rows. Asynchronous method allows to keep application responsible, and using cancellation token allows to cancel updating if control size is changed again. There is an issue with binding to view size - as bindings are executed in single thread, new values of view width and height come in different moments.

It means that it is necessary wait for another one. In addition, in order to prevent too frequent changes of grid sizes if user are resizing window slowly, timer is used in application.

The timer is created in constructor and starts or restarts each time one view height or view width are changed. When timer ticks, method checks that both width and height are valid and recreate cell set. After new cells set is created, collection of cell view models should be updated. In the previous post, this collection was recreated each time and it leads to application hangs.

Grid with dynamic number of rows and columns, part 2

This issue is solved by asynchronous method of updating current collection. As cell view models is stored as collection of collections, each inner collection corresponds to the row of grid. Max Cells. Count, GridHeight. The following cases are possible:.

These rows should be removed:. Unity is used as dependency container. We look forward to sharing our expertise, consulting you about your product idea, or helping you find the right solution for an existing project.

Your message is received. Svitla's sales manager of your region will contact you to discuss how we could be helpful. This site uses cookies. By using our site, you agree to our Privacy and Cookie Policy.