WPF C# Difficulties with Using a Dataview and DataGrid
<p>I am working an application that uses a datagrid to display some items. These items are assigned a category ID. I need the application to work so that only a filtered view (that is, items belonging to the same category) are shown at a time.</p>

<p>To go about implementing this, I am using DataViews. However, the problem is that instead of displaying only the data I want (the columns Name, Price, Condition, and Image Path), I get a total of 11 columns - the four I want plus id, price, name, image, description, category, and conditionID. Basically, it is displaying not only what it is supposed to, but also all of the columns from my datatable.</p>

<p>Why is it doing this and how can I change this behavior. Also, how can I ensure the underlying DataSet is updated even though I am using DataViews? </p>

<p>XAML</p>

<pre><code>&lt;Window x:Class="Leisurely_Diversion_Interface.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
  xmlns:local="clr-namespace:Leisurely_Diversion_Interface"
    Title="MainWindow" Height="350" Width="525" Loaded="Window_Loaded_1"&gt;

    &lt;Window.Resources&gt;
        &lt;ObjectDataProvider x:Key="conDictionary" ObjectType="{x:Type local:databaseAccess}" MethodName="getConditions" /&gt;
    &lt;/Window.Resources&gt;
    &lt;StackPanel Orientation="Vertical"&gt;
        &lt;DataGrid x:Name="itemGrid" ItemsSource="{Binding}"&gt;
            &lt;DataGrid.Columns&gt;
                &lt;DataGridTextColumn Header="Name" Binding="{Binding name}"&gt;&lt;/DataGridTextColumn&gt;
                &lt;DataGridTextColumn Header="Price" Binding="{Binding price}"&gt;&lt;/DataGridTextColumn&gt;
                &lt;DataGridTemplateColumn Header="Condition"&gt;
                    &lt;DataGridTemplateColumn.CellTemplate&gt;
                        &lt;DataTemplate&gt;
                            &lt;ComboBox  ItemsSource="{Binding Source={StaticResource conDictionary}}" SelectedValuePath="Key" DisplayMemberPath="Value"&gt;&lt;/ComboBox&gt;
                    &lt;/DataTemplate&gt;

                    &lt;/DataGridTemplateColumn.CellTemplate&gt;
                &lt;/DataGridTemplateColumn&gt;

                &lt;DataGridTextColumn Header="Image Path" Binding="{Binding image}"&gt;&lt;/DataGridTextColumn&gt;
            &lt;/DataGrid.Columns&gt;
        &lt;/DataGrid&gt;
        &lt;StackPanel Orientation="Horizontal" VerticalAlignment="Center" HorizontalAlignment="Center"&gt;
        &lt;Button x:Name="backButton" Content="&amp;lt;&amp;lt;" HorizontalAlignment="Left"/&gt;
        &lt;Button x:Name="forwardButton"  Content="&amp;gt;&amp;gt;" HorizontalAlignment="Right"/&gt;
        &lt;/StackPanel&gt;

    &lt;/StackPanel&gt;


&lt;/Window&gt;
</code></pre>

<p>MainWindow</p>

<pre><code>namespace Leisurely_Diversion_Interface
{


    /// &lt;summary&gt;
    /// Interaction logic for MainWindow.xaml
    /// &lt;/summary&gt;
    public partial class MainWindow : Window
    {

        private int currentCategory;
        private int lowestBounds;
        private int upperBounds;
        private Dictionary&lt;int, String&gt; categoryMap;
        DataView view;

        public MainWindow()
        {
            InitializeComponent();

            // attach events

            backButton.Click += navigate;
            forwardButton.Click += navigate;
        }

        private void Window_Loaded_1(object sender, RoutedEventArgs e)
        {
            DataSet ds = new DataSet();
            databaseAccess access = new databaseAccess(ConfigurationManager.ConnectionStrings["leisurelyDiversion"].ConnectionString);

            categoryMap = access.getCategories();
            var keyList = categoryMap.Keys.ToList();
            keyList.Sort();

            lowestBounds = (int)keyList[0];
            upperBounds = (int)keyList[keyList.Count - 1];
            currentCategory = lowestBounds;

            ds.Tables.Add(access.getTable("stock"));
            MessageBox.Show(currentCategory.ToString());
            view = ds.Tables[0].DefaultView;
            view.RowFilter = "category = " + currentCategory;

            itemGrid.ItemsSource = view;
        }

        private void navigate(Object sender, RoutedEventArgs e)
        {
            MessageBox.Show("EVENT!"); 
            Button selected = (Button)sender;
            if (selected.Content == "&lt;&lt;")
            {
                currentCategory--;
            }
            else
            {
                currentCategory++;
            }
            view.RowFilter = "category = " + currentCategory;
            itemGrid.ItemsSource = view;
        }
    }
}
</code></pre>

<p>dataBaseAccess.cs</p>

<pre><code> class databaseAccess
    {
        private MySqlConnection con = new MySqlConnection();
        private MySqlDataAdapter adapter = new MySqlDataAdapter();



        public databaseAccess(String connectionString)
        {
            try {         
                con.ConnectionString = connectionString;
            }
            catch (Exception e)
            {
                Console.WriteLine(e.StackTrace);
            }
        }

        private bool connect()
        {
            try
            {
                con.Open();
                return true;
            } catch(MySqlException e) {
                Console.WriteLine(e.StackTrace);
                return false;
            }
        }

        public DataTable getTable(String tableName)
        {
            if (this.connect())
            {
                DataTable table = new DataTable();
                MySqlCommand command = new MySqlCommand("SELECT * FROM " + tableName, con);
                MySqlDataAdapter adapter = new MySqlDataAdapter(command);
                adapter.Fill(table);

                con.Close();
                return table;

            }
            con.Close();
            return null;
        }

        public Dictionary&lt;int, String&gt; getConditions()
        {
            Dictionary&lt;int, string&gt; conditionMap = new Dictionary&lt;int, string&gt;();
            if (this.connect())
            {
                try
                {
                    MySqlCommand com = new MySqlCommand("SELECT * FROM conditions", con);
                    MySqlDataReader reader = com.ExecuteReader();
                    while (reader.Read())
                    {
                        conditionMap.Add((int)reader["conditionID"], (string)reader["name"]);


                    }
                    con.Close();
                    return conditionMap;
                }
                catch (Exception e)
                {
                    Console.WriteLine(e.StackTrace);
                }
            }

            return null;
        }

        public Dictionary&lt;int, string&gt; getCategories()
        {
            try { Dictionary&lt;int, string&gt; categoryMap = new Dictionary&lt;int,string&gt;();
            if (this.connect())
            {
                MySqlCommand com = new MySqlCommand("SELECT * FROM categories", con);
                MySqlDataReader reader = com.ExecuteReader();
                while (reader.Read())
                {
                    categoryMap.Add((int)reader["id"], (string)reader["name"]);


                }
                con.Close();
                return categoryMap;
            }
            }
            catch (Exception e) { Console.WriteLine(e.StackTrace); }
             return null;
        }

    }
}
</code></pre>

<p>Thank you so much!</p>

<p>EDIT: The same thing happens when I use the following code:</p>

<pre><code>     private void Window_Loaded_1(object sender, RoutedEventArgs e)
        {

            databaseAccess access = new databaseAccess(ConfigurationManager.ConnectionStrings["leisurelyDiversion"].ConnectionString);

            categoryMap = access.getCategories();
            var keyList = categoryMap.Keys.ToList();
            keyList.Sort();

            lowestBounds = (int)keyList[0];
            upperBounds = (int)keyList[keyList.Count - 1];
            currentCategory = lowestBounds;

            ds.Tables.Add(access.getTable("stock"));



            itemGrid.ItemsSource = ds.Tables[0].DefaultView;
            ds.Tables[0].DefaultView.RowFilter = "category = " + currentCategory;
        }

        private void navigate(Object sender, RoutedEventArgs e)
        {
            MessageBox.Show("EVENT!"); 
            Button selected = (Button)sender;
            if (selected.Content == "&lt;&lt;")
            {
                currentCategory--;
            }
            else
            {
                currentCategory++;
            }
            ds.Tables[0].DefaultView.RowFilter = "category = " + currentCategory;

        }
    }
}
</code></pre>