Solved

wpf datagrid

Posted on 2013-11-14
1
692 Views
Last Modified: 2013-11-14
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>
0
Comment
Question by:bkrbbnsEE
1 Comment
 

Accepted Solution

by:
bkrbbnsEE earned 0 total points
ID: 39648570
Guess I was over thinking it.  Since I had the datatables and adapters I just changed my logic:



        DataGrid1.ItemsSource = Nothing

        DataGrid1.ItemsSource = dtAgendaDays

        DataGrid1.Items.Refresh()

Seems to be working fine!
0

Featured Post

How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

Join & Write a Comment

Suggested Solutions

Many of us here at EE write code. Many of us write exceptional code; just as many of us write exception-prone code. As we all should know, exceptions are a mechanism for handling errors which are typically out of our control. From database errors, t…
More often than not, we developers are confronted with a need: a need to make some kind of magic happen via code. Whether it is for a client, for the boss, or for our own personal projects, the need must be satisfied. Most of the time, the Framework…
This is Part 3 in a 3-part series on Experts Exchange to discuss error handling in VBA code written for Excel. Part 1 of this series discussed basic error handling code using VBA. http://www.experts-exchange.com/videos/1478/Excel-Error-Handlin…
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

747 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now