Creating Datagrid Template Columns in Code behind.

Mar 26, 2009 at 5:17 PM

 My XAML code at the moment looks like

<wpftk:DataGrid Name="dgMatrix" ItemsSource="{Binding Path=CollectionOfCollection}" AutoGenerateColumns="False"
                                RowHeight="28"  EnableColumnVirtualization="True" EnableRowVirtualization="True"
                                HeadersVisibility="None" VerticalScrollBarVisibility="Auto" HorizontalScrollBarVisibility="Auto" />

And my code behind where I generate my datagrid is as below

       private void PopulateTheDataGrid()
            for (int iLoop = 0; iLoop < columnCount; iLoop++)
                DataGridColumn dtxc = new Microsoft.Windows.Controls.DataGridTextColumn
                                                Width = 56,
                                                Binding = new Binding(string.Format("[{0}].Value", iLoop))                                               

My dataset is a Collection of a Collection, containing my objects, just like a matrix. Say, I have a class called R then my dataset is List< List< R >>.

What I want is rather than creating the DataGridTextColumn I want to create a DataGridTemplateColumn with a TextBox inside it. Can someone please help me with this.

Thanks in advance,

May 29, 2009 at 6:00 PM


DataGridTextColumn generates cells that have as content a TextBox and that text box is the actual element that display the value in a cell. So, DataGridTextColumn is what you need.