Solved

wpf datagrid

Posted on 2013-11-14
1
707 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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

MS Dynamics Made Instantly Simpler

Make Your Microsoft Dynamics Investment Count  & Drastically Decrease Training Time by Providing Intuitive Step-By-Step WalkThru Tutorials.

Question has a verified solution.

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

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…
A long time ago (May 2011), I have written an article showing you how to create a DLL using Visual Studio 2005 to be hosted in SQL Server 2005. That was valid at that time and it is still valid if you are still using these versions. You can still re…
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…
In this video, viewers will be given step by step instructions on adjusting mouse, pointer and cursor visibility in Microsoft Windows 10. The video seeks to educate those who are struggling with the new Windows 10 Graphical User Interface. Change Cu…

635 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