WPF Grid: Dynamic Rows and Columns

Object tagging is the functionality that allows objects to be tagged to a control. This can be used in situations where the control is available, but an object cannot be accessed using standard application logic. In the case of this sample, the available control is the CheckBox in the DataGridCell and the required object is the role that corresponds to the column. The https://traderoom.info/ role is tagged to the column and can be retrieved at a later time. The ObjectTag itself is a DependencyProperty that can be attached to any type of control that is derived from DependencyObject. 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 ListView control

The Role and User tables are bound to the data grid controls via a DataView. The DataView allows the modification, insertion and removal of rows and the prevention of these actions. The data grid control can handle the data manipulation using the DataView. The sample code implements a user administration form in which users, roles and the user-role assignment can be administered.

A WPF application

They are configured by the SelectionMode and SelectionUnit property. The Datagrid column definition in WPF and its corresponding row definition are represented by two parameters – GridWidth and GridHeight. This example defines three columns and binds them to the A, B, and C properties of the data object.

Create a new class and derive from DynamicGridControl<>.

To freeze a numer of columns just set the FrozenColumnCount property to the number of columns you want to freeze. You can define a an AlternatingRowBackground that is applied every even row. You can additionally specify an AlternationCount if you only want to ink every every n-th data row. To enable grouping you have to define a CollectionView that wpf dynamic table contains to least one GroupDescription that defines the criterias how to group. The problem with this approach is that the user can’t add the same letter/column twice because of the Dictionary but you can Adapt and change the code to fit your needing. Connect and share knowledge within a single location that is structured and easy to search.

In both cases, the user and the role roles are retrieved and the conversion result (if the user has the role assigned or not) is returned. The user row is fetched from the DataGridCell’s DataContext, which contains the DataRowView instance that has the user row in its Row property. The role is retrieved from the ColumnTag that is assigned to the column when it was added. It displays a grid of cells that correspond to a collection of objects (rows) containing properties (columns). You need to supply the data storage – a collection of objects. The number of objects in the collection (the collection count) will determine the number of rows in the grid.

The data grid control’s column property is declared as read-only, so it cannot be bound to a view model property. The DataGridColumnsBehavior is an attached behavior that overcomes this limitation. As an added bonus (and to prevent extra state logic) I added the functionality that the CheckBox control is not shown in the user data grid new item row. The DataGridCheckBoxColumn style has to be modified, and the Visibility flag of the CheckBox has to be set, depending on the contents of the DataGridCell.

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. 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.

After setting , the final source code of  the MainWindow.XAML is given below. If you want to sort the row / column headers, assign a RowComparer / ColumnComparer. If you need WPF to populate datagrid, please refer to the following discussion on Microsoft’s Q&A blog. By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy. Find centralized, trusted content and collaborate around the technologies you use most. The DataSet can be used together with database connections to store and retrieve data from SQL servers, etcetera.

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. Every DataSet table has a set of events that can be used to get notified on data modifications.. This mechanism is used to add, remove and update the dynamic columns when the role table is modified.

In the next article, I will show a solution that implements the same application, but with a more strict separation of business logic and GUI controls into their respective layers. In this first part, I focus on the solution of handling dynamic columns. The value converter’s Convert method is called, every time the DataGrid cell is initially modified or lost its focus.

  1. Every DataSet table has a set of events that can be used to get notified on data modifications..
  2. In this first part, I focus on the solution of handling dynamic columns.
  3. This means that the default columns, the user’s first and last name, have to be in this collection too.
  4. In this case, it would be a boolean property in the user data row, which represents the user to role assignment.

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. If you need dynamic columns, you should always use a DataTable as data source. This way you can avoid to build the DataGrid explicitly in C#/code-behind and instead use the auto-generation feature. You can look at the WPF grid example in the image below, which shows a 10×5 grid table.

The post is devoted to the WPF gridview with a dynamically-defined number of rows and columns but all cells have the same width and height. For example, such a grid could be used in chess or checkers games for 8×8 field. 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. 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. Values of color properties are assigned to corresponding properties of CellView control.