1

I have a datagrid that displays 18 properties of an object that has 149 properties.

6/18 are always displayed for every object.

12/18 change depending on which group of the objects I want to display.

How can I change the binding of the columns I want to change at runtime programmatically.

I've tried

        myBinding.Source = CutterList
        myBinding.Path = New PropertyPath("ToolNumCEBF")
        myBinding.Mode = BindingMode.TwoWay
        myBinding.UpdateSourceTrigger = UpdateSourceTrigger.PropertyChanged
        BindingOperations.SetBinding(dataGrid1.Columns.Item(1), TextBlock.TextProperty, myBinding)

CutterList is the original ObservableCollection of my datagrid.

myBinding.Path = New PropertyPath("ToolNumCEBF")

I think SHOULD point to a Public Property within my class that is the base of the ObservableCollection, but I'm not sure.

BindingOperations.SetBinding(dataGrid1.Columns.Item(1), TextBlock.TextProperty, myBinding)

As far as I understand is trying to set the binding of my specified column, the text to be displayed, using the other stuff I set with myBinding?

Trying to do this feel like iceskating uphill.

I've also tried just changing my DataGrid's overall itemsource without any luck, so if you have any insights there, I'm all ears.

ItsToka
  • 31
  • 5
  • It'd be easier to help with a rough image of how you want to show the data in the ``DataGrid``. – Andrew KeepCoding Sep 14 '22 at 00:13
  • Andrew KeepCoding I really don’t know how to explain it any better. Several of the columns don’t need to have their binding changed because they are properties that are universal across all of my objects. But several are the same type of property but slightly different like for instance if I wanted a column that just said “temperature” but depending on user stuff it could be either in metric or imperial or kelvin, etc. I know there would be a way easier way to accomplish that specific task, but my similar properties are speeds recommended for different machines, and all pulled from a big CSV. – ItsToka Sep 14 '22 at 00:33

1 Answers1

1

Figured out a solution. Cast the type of the column from the regular DataGridColumn to DataGridTextColumn. Then it let me set the binding like so:

        Dim tempCol As New DataGridTextColumn
        tempCol = dataGrid1.Columns.Item(1)

        Dim myBinding = New Binding
        myBinding.Source = CutterList
        myBinding.Path = New PropertyPath("ToolNumCEBF")
        myBinding.Mode = BindingMode.TwoWay

        tempCol.Binding = myBinding

This actually doesn't work. It seems to remove the old binding, but something with my Path isn't right. It's not giving an error just producing blank cells.

Ok, actually did figure it out now, a solution, not the reasoning...

For some reason trying to add the .Source and .Mode was unnecessary and causing some kind of issue, when I deleted them, it worked as intended, So i guess as long as your source doesn't change, you can change the binding of a column programmatically like so:

        Dim tempCol As New DataGridTextColumn
        tempCol = dataGrid1.Columns.Item(0)

        Dim myBinding = New Binding
        myBinding.Path = New PropertyPath("ToolNumCEBF")

        tempCol.Binding = myBinding
ItsToka
  • 31
  • 5