Search code examples
bindingwpftoolkitdatagridcolumn

DataGridColumn binding in Code


I am searching a simple method through which i can bind my DataGridColumn with a Datatable columns on the fly. I should also be able to define the width of each column.

Example :-

If My program generates 5 columns in a datatable, so the DataGrid should be able to dispaly 5 columns with each having different column width.


Solution

  • I figured out the answer:

    dtgrdAtlas.Columns.Add(
                            new DataGridTextColumn
                            {
                                Header = dc.ColumnName,
                                Width = 100,
                                IsReadOnly = true,
                                Binding = new Binding(string.Format("[{0}]", dc.ColumnName))
                            });
    
    dtgrdAtlas = DataGrid
    

    dc = DataColumn belonging to a data table

    It does the binding too!