filter & save to xml function doesnt work viewmodel

Kran2022 381 Reputation points

Hello Morning:

First i read xml file to fill the datagrid edit, save the xml file, then export the datagrid to excel file using OpenXML, then read the excel file to fille the datagrid.

When i jsut read the xml file, i can filter the datagrid and edit datagrid cells & save back to the file.

when i read the excel file to datagrid though i exported as obervable collectoion, the filter function returns nothing and SaveFile button click doesnt work, data.Save("xxx.txt"); the xml elemnts return empty.

It looks like when i read the excel file, i need to bind with class product or i need to change to something else? Any help? thanks.

Below attached example exml file and excel file.

Filter datagrid using combo box & radio buttons:

	<!-- combobox filter items -->
   <StackPanel VerticalAlignment="Top" Margin="10,0,10,0" Grid.Column="0">
				<Label Content="Product Category Search" HorizontalAlignment="Left" Margin="0,0,0,0" />
				<TextBox Text="{Binding MainProductSearch,UpdateSourceTrigger=PropertyChanged}" 
			 HorizontalAlignment="Left"  Width="150" Margin="5,5" />
				<ComboBox Width="150" ItemsSource="{Binding MainProductCategory}"
			  Text="{Binding MainProductSearch,UpdateSourceTrigger=PropertyChanged}"  
			  Margin="5,5" HorizontalAlignment="Left" />
				<Label Content="Product Name Search" Margin="5,5" HorizontalAlignment="Left" />
				<TextBox Text="{Binding SizeSearch, UpdateSourceTrigger=PropertyChanged}" 
			 Margin="5,5" HorizontalAlignment="Left" Width="150"  />
				<ComboBox Width="150" ItemsSource="{Binding ProductName}"
			  Text="{Binding SizeSearch, UpdateSourceTrigger=PropertyChanged}"
			  Margin="5,5" HorizontalAlignment="Left"/>
	<!-- Radio button filter items -->          
	<StackPanel VerticalAlignment="Bottom" Margin="01,0,10,10" Grid.Column="1" Width="150" HorizontalAlignment="Left">
		<Button Content="Reset Filter" HorizontalAlignment="Left" Margin="0,10,10,10" Width="75" 
  Command="{Binding MyCommand}" CommandParameter="Reset" />
		<RadioButton Content="Metallic" 
	   IsChecked="{Binding Path=MediaType, Converter={StaticResource mconv}, ConverterParameter=Metallic}"/>
		<RadioButton Content="Sticker"
	   IsChecked="{Binding Path=MediaType, Converter={StaticResource mconv}, ConverterParameter=Sticker}"/>
		<RadioButton Content="Perfo"
	   IsChecked="{Binding Path=MediaType, Converter={StaticResource mconv}, ConverterParameter=Perfo}"/>
		<RadioButton Content="SuperMatte"
	   IsChecked="{Binding Path=MediaType, Converter={StaticResource mconv}, ConverterParameter=SuperMatte}"/>


<DataGrid VerticalAlignment="Top" HorizontalAlignment="Left" 

                  SelectedItem="{Binding SelectedProduct}"

                  ItemsSource="{Binding View}" AutoGenerateColumns="False" 

                  CanUserAddRows="False" ScrollViewer.VerticalScrollBarVisibility="Visible" 


                  Height="{Binding RelativeSource={RelativeSource AncestorType={x:Type DockPanel}}, Path=ActualHeight}"  >


                        <DataGridTextColumn Header="MianProduct" Binding="{Binding Mainproduct}" Width="*" IsReadOnly="True"/>

                        <DataGridTextColumn Header="Name" Binding="{Binding Name}" Width="*" IsReadOnly="True"/>

                        <DataGridTextColumn Header="Price" Binding="{Binding Price}" Width="*" />

                        <DataGridTemplateColumn Header="Visible"  Width="100" >



                                    <CheckBox IsChecked="{Binding Visible, UpdateSourceTrigger=PropertyChanged}" />




                        <DataGridTextColumn Header="NameIcon" Binding="{Binding NameIcon}" Width="*" />



Product View Model:

private void Execute(object parm) //method to save back to the xml file price & visible attributes for each products 

			XmlDocument doc = new XmlDocument();
			foreach (Products pp in productpricelist)
				foreach (XmlElement pn in doc.SelectNodes("/Data/Products/*"))

					foreach (XmlNode visibility in pn.SelectNodes("Visibilities"))
						foreach (XmlNode productVisibilty in visibility.SelectNodes("ProductVisibility"))
							productVisibilty.Attributes["Visible"].InnerText = pp.Visible;

							foreach (XmlNode price in productVisibilty.SelectNodes("Prices"))
								foreach (XmlNode productPrice in price.SelectNodes("ProductPrice"))
									productPrice.Attributes["Price"].InnerText = pp.Price;


the above code works when i have only 1 attribute of price & visible but its not updating to all the attributes / visible for each products.

Pricefile.txt example

Excel file: example

|User's image

Windows Presentation Foundation
Windows Presentation Foundation
A part of the .NET Framework that provides a unified programming model for building line-of-business desktop applications on Windows.
2,665 questions
An object-oriented and type-safe programming language that has its roots in the C family of languages and includes support for component-oriented programming.
10,193 questions
{count} votes

Accepted answer
  1. Peter Fleischer (former MVP) 19,231 Reputation points

    If you load data from Excel you use DataTable with DataRows but Filter works with "item as Product". GetCollection returns a ObservableCollection<object>. In your code object is a dynamic object, no Product. Your Filter returns true for every item if there's no ObservableCollection<Product>.

    1 person found this answer helpful.

0 additional answers

Sort by: Most helpful