Solved

wpf datagrid

Posted on 2013-11-14
1
695 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

DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

Question has a verified solution.

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

In my previous article (http://www.experts-exchange.com/Programming/Languages/.NET/.NET_Framework_3.x/A_4362-Serialization-in-NET-1.html) we saw the basics of serialization and how types/objects can be serialized to Binary format. In this blog we wi…
Whether you've completed a degree in computer sciences or you're a self-taught programmer, writing your first lines of code in the real world is always a challenge. Here are some of the most common pitfalls for new programmers.
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…
Internet Business Fax to Email Made Easy - With eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, fr…

896 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

14 Experts available now in Live!

Get 1:1 Help Now