Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 4382124
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 21, 20262026-05-21T12:44:19+00:00 2026-05-21T12:44:19+00:00

I know it’s not really the best way to do it, but I have

  • 0

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 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-21T12:44:19+00:00Added an answer on May 21, 2026 at 12:44 pm
    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
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I know of cufon and one other similar. But what is the best way
I know the input have the maxlength, but I would like to have minlength
I know this is largely an opinion, but I'm interested if you have one
I know that this may be common knowledge, but is there a way to
I know it's not a good idea to loop SQL queries, so I would
I know we already have many posts about this topic, but I just cannot
I know threre a lot of libraries dotnetopenauth,oauthbase, etc. But i need sample of
know nothing about php, but I have this script that reads a folder and
I know you can not set a key value dynamically, but what about the
I know you have to synchronize around anything that would change the structure of

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.