Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

wpf datagrid

Posted on 2013-11-14
1
Medium Priority
?
717 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

New feature and membership benefit!

New feature! Upgrade and increase expert visibility of your issues with Priority Questions.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

For those of you who don't follow the news, or just happen to live under rocks, Microsoft Research released a beta SDK (http://www.microsoft.com/en-us/download/details.aspx?id=27876) for the Xbox 360 Kinect. If you don't know what a Kinect is (http:…
Today I had a very interesting conundrum that had to get solved quickly. Needless to say, it wasn't resolved quickly because when we needed it we were very rushed, but as soon as the conference call was over and I took a step back I saw the correct …
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…
Despite its rising prevalence in the business world, "the cloud" is still misunderstood. Some companies still believe common misconceptions about lack of security in cloud solutions and many misuses of cloud storage options still occur every day. …

963 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