1
votes

I know it's not really the best way to do it, but I have a special occasion when I would need to add new columns to a datagrid using code and apply a binding to it to

For example, I got this :

<DataGrid x:Name="GridUtilisateurs" Grid.Row="1" Grid.Column="0" ItemsSource="{Binding}" Style="{StaticResource BaseGrid}">
                <DataGrid.Columns>
                    <DataGridTemplateColumn>
                        <DataGridTemplateColumn.CellTemplate>
                            <DataTemplate>
                                <Canvas Background="{Binding ColorActive}"></Canvas>
                            </DataTemplate>
                        </DataGridTemplateColumn.CellTemplate>
                    </DataGridTemplateColumn>

                    <DataGridTextColumn Header="Usager" Width="*"  MinWidth="50"
                                               Binding="{Binding UserName}"                                               
                                               IsReadOnly="True" />
                 </DataGrid.Columns>
            </DataGrid>

How can I do the same thing, but with code ? The main problem I have is with the binding, all the rest seems fine.

This is what I got so far, but I can't make the binding work. The binding 'path' I would have use in XAML is equivalent to oCol.ColumnName :


            Dim oNewColumn As DataGridColumn
            Dim oBinding As New System.Windows.Data.Binding

            Select Case oCol.DataType

                Case GetType(Boolean)
                    oNewColumn = New DataGridCheckBoxColumn()
                    oNewColumn.MinWidth = 25
                    CType(oNewColumn, DataGridCheckBoxColumn).IsThreeState = False
                    CType(oNewColumn, DataGridCheckBoxColumn).Binding = oBinding
                    oNewColumn.Width = System.Windows.Controls.DataGridLength.SizeToHeader
                Case Else
                    oNewColumn = New DataGridTextColumn()
                    oNewColumn.MinWidth = 50
                    oNewColumn.Width = New System.Windows.Controls.DataGridLength(1, System.Windows.Controls.DataGridLengthUnitType.Star)
                    CType(oNewColumn, DataGridTextColumn).Binding = oBinding
            End Select
            oNewColumn.Header = oCol.ColumnName

            GridData.Columns.Add(oNewColumn)
        Next

        GridData.ItemsSource = MyList

After a while, I finally got to understand what's wrong. I needed to use [Description] because I was given a datetable. Now that this is done, I have another problem though. Since sometime I need to have dates, I would need to use a DataTemplateColumn and I tried using one but I can't really make one work so far. Anyone knows how to do it ? Thanks,

1

1 Answers

1
votes
Dim oNewColumn As DataGridColumn
            Dim oBinding As New System.Windows.Data.Binding

        Select Case oCol.DataType

            Case GetType(Boolean)
                oNewColumn = New DataGridCheckBoxColumn()
                oNewColumn.MinWidth = 25
                CType(oNewColumn, DataGridCheckBoxColumn).IsThreeState = False
                oBinding.Path ="PathName1"
                CType(oNewColumn, DataGridCheckBoxColumn).Binding = oBinding
                oNewColumn.Width = System.Windows.Controls.DataGridLength.SizeToHeader
            Case Else
                oNewColumn = New DataGridTextColumn()
                oNewColumn.MinWidth = 50
                oNewColumn.Width = New System.Windows.Controls.DataGridLength(1,             System.Windows.Controls.DataGridLengthUnitType.Star)
                oBinding.Path="PathName2"
                CType(oNewColumn, DataGridTextColumn).Binding = oBinding
        End Select
        oNewColumn.Header = oCol.ColumnName

        GridData.Columns.Add(oNewColumn)
    Next

    GridData.ItemsSource = MyList