我正在使用一个使用数据网格显示某些项目的应用程序。这些项目分配了类别ID。我需要应用程序才能工作,以便一次只显示过滤后的视图(即属于同一类别的项目)。
要实现这一点,我正在使用DataViews。然而,问题是,我不是只显示我想要的数据(列名称,价格,条件和图像路径),而是总共有11列 - 我想要的四个列加上ID,价格,名称,图像,描述,category和conditionID。基本上,它不仅显示它应该是什么,而且还显示我的数据表中的所有列。
为什么要这样做,我该如何改变这种行为。此外,即使我使用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="<<" HorizontalAlignment="Left"/>
<Button x:Name="forwardButton" Content=">>" 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 :(得分:0)
没关系。在这种情况下,故障非常简单。我忽略了将DataGrid的AutoGenerateColumns属性设置为false。
<DataGrid AutoGenerateColumns="False" x:Name="itemGrid" ItemsSource="{Binding}">
在添加之后,我使用了我发布的第二种代码形式,所有代码似乎都正常工作。