2016-05-24 3 views
-1

データグリッドを使用していくつかのアイテムを表示するアプリケーションを開発中です。これらの項目にはカテゴリIDが割り当てられます。フィルタリングされたビュー(つまり、同じカテゴリに属する​​アイテム)のみが一度に表示されるように、アプリケーションを動作させる必要があります。WPF C#DataviewとDataGridを使用する上での難点

これを実装するには、DataViewsを使用しています。しかし、問題は、必要なデータ(Name、Price、Condition、Imageの各列)を表示するのではなく、合計11の列を取得することです。 、カテゴリ、および条件ID。基本的には、それが想定されているだけでなく、データテーブルのすべての列も表示しています。

なぜこれを行うのですか、どうすればこの動作を変更できますか?また、DataViewsを使用していても、基礎となるDataSetが更新されるようにするにはどうすればよいですか?

XAML

<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"> 

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

        </DataGridTemplateColumn.CellTemplate> 
       </DataGridTemplateColumn> 

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

    </StackPanel> 


</Window> 

メインウィンドウ

namespace Leisurely_Diversion_Interface 
{ 


    /// <summary> 
    /// Interaction logic for MainWindow.xaml 
    /// </summary> 
    public partial class MainWindow : Window 
    { 

     private int currentCategory; 
     private int lowestBounds; 
     private int upperBounds; 
     private Dictionary<int, String> 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 == "<<") 
      { 
       currentCategory--; 
      } 
      else 
      { 
       currentCategory++; 
      } 
      view.RowFilter = "category = " + currentCategory; 
      itemGrid.ItemsSource = view; 
     } 
    } 
} 

dataBaseAccess.cs

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<int, String> getConditions() 
     { 
      Dictionary<int, string> conditionMap = new Dictionary<int, string>(); 
      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<int, string> getCategories() 
     { 
      try { Dictionary<int, string> categoryMap = new Dictionary<int,string>(); 
      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; 
     } 

    } 
} 

はありがとうございました!

 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 == "<<") 
      { 
       currentCategory--; 
      } 
      else 
      { 
       currentCategory++; 
      } 
      ds.Tables[0].DefaultView.RowFilter = "category = " + currentCategory; 

     } 
    } 
} 

答えて

0

気にしない:

編集:私は、次のコードを使用するときに同じことが起こります。この例では、障害は非常に簡単でした。私はDataGridのAutoGenerateColumnsプロパティをfalseに設定することを怠った。

<DataGrid AutoGenerateColumns="False" x:Name="itemGrid" ItemsSource="{Binding}"> 

これを追加した後、私が投稿したコードの2番目の形式を使用しました。すべてが正常に動作しているようです。

関連する問題