Link to home
Start Free TrialLog in
Avatar of bkrbbnsEE
bkrbbnsEEFlag for United States of America

asked on

wpf datagrid

I'm using Visual Studios 2010, wpf with vb.net.  I have a window with a 3 radio buttons and a datagrid.  When the user selects a radio button I load the datagrid from a table.

The datagrid loads with the data from the datatable but it appends the columns to existing datagrid.  So, the first selection loads fine but and subsequent selection adds the new columns to the right of the columns from the previous selection.

How do I remove the previous columns and load the datagrid from the new table with just those columns displayed?  

I've tried:
    DataGrid1.Items.Clear()  -- data is cleared from rows but columns and headings remain
           &
    BindingOperations.ClearAllBindings(DataGrid1) - -- data is cleared from rows but columns and headings remain
            &
    For Each dc As DataGridColumn In DataGrid1.Columns
                           DataGrid1.Columns.Remove(dc)

     Next   -- throws InvalidOperationException: Collection was modified; enumeration operation may not execute.
 


Code to load the datagrid similar for each radio button selected.  Different number of coumns being displayed depending on the selection.

 Dim col1 As New DataGridTextColumn()
        Dim col2 As New DataGridTextColumn()
        Dim col3 As New DataGridTextColumn()
        Dim col4 As New DataGridTextColumn()
        Dim col5 As New DataGridTextColumn()

        DataGrid1.Columns.Add(col1)
        DataGrid1.Columns.Add(col2)
        DataGrid1.Columns.Add(col3)
        DataGrid1.Columns.Add(col4)
        DataGrid1.Columns.Add(col5)


        For Each drA As System.Data.DataRow In dtAgenaItems.Rows
            DataGrid1.Items.Add(New myAgendaItems With {.dayName = drA.Item("AGENDA_DAY"), .dayItem = drA.Item("ITEM_NAME"), .asscDisplay = drA.Item("ASSOCIATED_DISPLAY"), .sortDay = drA.Item("SORT_DAY"), .sortItem = drA.Item("SORT_ITEM")})
            col1.Binding = New Binding("dayName")
            col2.Binding = New Binding("dayItem")
            col3.Binding = New Binding("asscDisplay")
            col4.Binding = New Binding("sortDay")
            col5.Binding = New Binding("sortItem")

        Next

        col1.Header = "DAY"
        col2.Header = "ITEM"
        col3.Header = "DISPLAY"
        col4.Header = "SORT DAY"
        col5.Header = "SORT ITEM"
        blnFirstTime = False
        DataGrid1.Items.Refresh()


-------
XAML
<Window x:Class="SettingsPage"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Title="SettingsPage" Height="300" Width="300">
    <Grid>
        <RadioButton Content="Agenda List" Height="16" Name="RadioButton1" GroupName="TableSelection" Margin="84,36,96,208" />
        <RadioButton Content="Items" Height="16" Name="RadioButton3" GroupName="TableSelection" Margin="84,59,84,186" />
        <RadioButton Content="Agenda List Items" Height="16" Name="RadioButton2" GroupName="TableSelection" Margin="84,80,64,164" />

        <DataGrid AutoGenerateColumns="False" Height="147" Margin="12,102,0,0" Name="DataGrid1"  VerticalAlignment="Stretch" Width="254" HorizontalAlignment="Stretch" />
    </Grid>
</Window>
ASKER CERTIFIED SOLUTION
Avatar of bkrbbnsEE
bkrbbnsEE
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial