TonyW TonyW - 5 months ago 14
Vb.net Question

Hiding columns from datagrid when autogenerate=false

I create columns in codebehind as such.. each column is bound to a property.

Dim column_selected As New DataGridCheckBoxColumn()
column_selected.Header = "Selected"
column_selected.Binding = New Binding("IsChecked")
dgvResults.Columns.Add(column_selected)


I want to be able to hide a column, based on a checkbox or something of that nature, where I can hide/show them at will.

I've heard about binding visibility to a property Boolean, but i'm not sure how to do that when the columns are created in code behind.

Any idea on how to accomplish this? Say I want to have a single checkbox that hides a specific column, if you unchecked it, it shows it.

Answer

If logic of setting column visibility doesn't contains any business logic and this is clear UI operation. Then I think you can just put it in the code-behind, what is wrong with that?

XAML

<CheckBox Checked="HideColumn" Unchecked="UnhideColumn"/>

Code-behind

Protected Sub HideColumn()
    'your code
End Sub

Protected Sub UnhideColumn()
    'your code
End Sub