Search code examples
c#wpfdatagridgridruntime

First steps in WPF: how to fill in a cell in a grid?


I'm coming from Delphi, Java, ..., and now I need to make my first WPF program.

I am making a program for reading and writing information to existing tables.

My idea is very simple:

  • Create a grid and put it on the form
  • Read the table:
    • First read the column names. Add those to the first cells of the grid (var_Grid.Cells[0,i] = "Column_Name";)
    • Second read all the data, currently in the table, and fill it in in the grid (var_grid.Cells[i,j] = "data";
  • Do further processing

As I want to do some processing on my grid first, I would like to use a grid component, not linked to a database (so no TDBGrid, for the ones who know Delphi).

I quickly realised that WPF Grid is meant for adding components to a form (like a JPanel with a GridLayout, for the Java programmers), so I thought opting for the DataGrid component.

However, when I check on how to select a cell in a DataGrid: I see DataGridCellInfo, ItemContainerGenerator, ContainerFromIndex, ..., all that for just getting a cell? (There's even no code for entering data in that cell)

I believe the DataGrid is far too complicated, there must be a WPF visual component, which is far easier for filling up than that one, but searching on Grid in the toolbox gives no answers.

Does anybody know of an easier way to work with the WPF DataGrid or does anybody know of an easier component that does what I want?

Edit after first comments

I started with the ListView and the GridView. I managed quite rapidly getting a GridViewColumn working, but then I'm having the issue that I don't know the amount of columns I'll be having for the ItemsSource (I tried working with a List<List<string>> but that seemed to be too naïve :-)

Second edit
Apparently there's a mismatch between:

  • what I would like to do, and:
  • why I would like to do that.

What I would like to do, is create a grid of which, at runtime, I can define the amount of rows and columns and fill in the cells.

However, the discussion in the comments are flooded by the why I would like to do that.

So I would like to rephrase my question: is there a WPF visual grid component of which I can set and alter, at runtime, row and column number and fill in cell by cell?

If that grid component would have the possibility to have a header row, that would be nice.

Does anybody know about such a visual WPF component?

Thanks in advance


Solution

  • You have gone fundamentally wrong way of implementation. You want to directly manipulate the content of WPF UI elements. But WPF is built on a very different concept. The main way to get data in WPF is to use Data Context. UI elements themselves get values ​​for their properties through Bindings. By default, bindings use Data Context as the source.

    Therefore, for the correct implementation, you must first create an entity that will provide Data for one DataGrid row in its properties. Then create an observable collection with these entities. And bind this collection to DataGrid.ItemsSource.

    The DataGrid will automatically create a column for each property of the entity and display the property value in it. If you wish, you can customize the desired view of DataGrid and its cells yourself.

    Thus, your task will be reduced to working with the collection and its elements, instead of a rather complex search inside the DataGrid and its visual tree.

    One of the universal sources for a DataGrid can be a DataTable. This type should be used when you cannot predefine all the required properties for an entity of one row.