WPF c#, bind datagrid column with code behind

As far as i understand you want to add data grid column from code behind and that column should work with binding..?

here is sample snippet to add datagrid column from code behind

 var col = new DataGridTextColumn();
            col.Header = "d";
            col.Binding = new Binding("RoomNumber");
            dataGrid1.Columns.Add(col);

With this approach you can add as many columns as you want and you can give data binding at run time for each column and you can specify itemssource at once....

make sure to mark AutoGenerateColumns="False" in your data grid so that you can avoid unwanted columns get added from itemssource..


The answer from bathineni (and others) is essentially correct, so long as, at some point you have set the ItemsSource property of the DataGrid to your collection.

Tags:

C#

Wpf

Datagrid