Search code examples
c#wpfwpfdatagrid

How to add columns to a DataGrid on the fly using WPF in C#


Does anyone know of a way to add columns to a WPF DataGrid on the fly? I'm looking to use a DataGrid in my GUI that will display standard data, and that will also allow the user to add and remove columns. When the user adds a column, the user should be able to specify a value that will be entered on each row of the column. I have used the DataGrid in the past by creating a class with properties and a list of these objects fills out the grid easily, where each object in the list is a row. If I could create a new class on the fly or add a new property on the fly to my existing class, it seems like this would be possible.

I've been looking for a way to add properties to an object on the fly as a possible solution, but I haven't found a way to do this yet, and I don't know if it's a feasible solution. I also haven't found a way to create a new class on the fly and again I don't know if it's possible. I have also tried using a DataTable, but I haven't been able to get that to work either.

Here are more details on what I want to accomplish. A screenshot is included below. The datagrid will be populated with data. The user can then enter a column header and column data. When the "Update Column" button is pressed,if the data field contains "!REMOVE_COL" then the column will be removed if it exists and will not be added if it does not exist. If the column doesn't exist, it will add the column and the value entered in the data field will be added to each row. If the column already exists, then the column data will be updated with the value in the data field.

Haha, nevermind on the screenshot. Apparently I don't have enough rep to post images. :/ You can find the screenshot here:

https://docs.google.com/document/d/1B4UVhVDqFdBbsUMOtMLK2PGTrnKLLux2leo_gas9rdw/edit?usp=sharing


Solution

  • Beware that accessing a WPF DataGrid at run-time it nothing like doing the equivalent for a WinForms DataGridView. Anyway, what you want to do is to create a method which builds and returns a DataSet (which may or may not have data, no data will merely define the column structure)

    private DataSet BuildDataGridColumns()
    {
        // Build the data.
        DataSet ds = new DataSet();
        DataTable dt = new DataTable();
        ds.Tables.Add(dt);
    
        // Add the columns to the DataTable.
        DataColumn indexCol = new DataColumn("Key Index", typeof(String));
        dt.Columns.Add(indexCol);
        DataColumn fileCol = new DataColumn("File Name", typeof(String));
        dt.Columns.Add(fileCol);
        DataColumn resCol = new DataColumn("Resource Name", typeof(String));
        dt.Columns.Add(resCol);
        return ds;
    }
    

    To create the DataGrid called dataGrid you then can use

    dataGrid.ItemsSource = BuildDataGridColumns().Tables[0].AsDataView();
    

    To remove/change the shown columns change the ItemSource, using the above should enable you to accomplish all that you require.

    I hope this helps.