我使用 sqlite 从数据库填充数据网格 - ShowDataBase(string a);
然后我想对它进行排序,当按钮被点击时 (注意!我只想在程序中对其进行排序(而不是修改数据库)。只想删除错误的行)
private void Button_Click(object sender, RoutedEventArgs e)
{
ShowDataBase("Pacients");
if ((bool)SortFromCheckBox.IsChecked)
{
//Delete all data earlier than data that user asked for
for (int i = DataGridMain.Items.Count - 1; i >= 0; i--)
{
DataGridRow row = (DataGridRow)DataGridMain.ItemContainerGenerator.ContainerFromIndex(i);
var item = DataGridMain.Items[i];
var data = DataGridMain.Columns[4].GetCellContent(item);
if ((Convert.ToDateTime(data)) <= (Convert.ToDateTime(SortFromTextBox.Text)))
{
//Smth to delete row № i from datagrid
}
}
}
if ((bool)SortUntilCheckBox.IsChecked)
{
//Delete all data older than data that user asked for
}
if ((bool)SortByClientCheckBox.IsChecked)
{
//Delete all data where client cell is != name that user want to sort by
}
if ((bool)SortByDoctorCheckBox.IsChecked)
{
//Delete all data where doctor cell is != name that user want to sort by
}
}
最佳答案
据我了解,您需要一种功能来过滤和排序数据。您可以为此目的使用内置机制:CollectionView
.您需要创建一个源将设置为您从数据库中获取的数据。
要过滤集合,您可以使用 collectionView.Filter
并为您的目的定义不同的过滤器。对于排序功能,您应该使用 collectionView.SortDescriptions
.通过这种方式,您可以利用 .NET 中已有的工具,只需根据您的需要进行自定义。
使用这种方法,您不会从基础集合中删除任何内容,因此您只需获取一次数据并在您的应用程序中以不同的方式呈现它。如果您使用 MVVM 方法,则此代码应在 ViewModel 中完成。如果你坚持在你的 View 中实现这样的功能,你可以玩<CollectionViewSource>
.
您可以在这里找到一些资源:
关于c# - 如何对我的数据网格表进行排序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29490850/