如何在没有数据库的 C# windows 应用程序中过滤两个 datetimepicker 之间的 datagridview?
How to filter datagridview between two datetimepicker in a C# windows application without a database?
我想在两个日期时间选择器之间过滤 datagridview 而不涉及数据库。datagridview 具有内存中存储的记录。我正在使用 C# 和 .NET 框架。
我有一个名为“datagridview1”的 DataGridView,以及两个名为“dateTimePickerFromDate”和“dateTimePickerToDate”的日期时间选择器。所以我需要在 datagridview1 中搜索记录,其中记录仅在 2 个所选日期之间,我需要加载 datagridview1。该日期基于 datagridview1 中的 交易日期。
下面是一个示例,它使用具有最少数据的 DataTable 来加载过滤所需的 BindingSource。使用了两个变量,将它们替换为您的 DateTimePicker 控件。
Button1 中的代码切换过滤器。
using System;
using System.Data;
using System.Windows.Forms;
namespace YourNameSpace
{
public partial class DemoForm : Form
{
private readonly BindingSource _bindingSource = new BindingSource();
public DemoForm()
{
InitializeComponent();
_bindingSource.DataSource = MockedData();
dataGridView1.DataSource = _bindingSource;
}
public DataTable MockedData()
{
var dt = new DataTable();
dt.Columns.Add("Id", typeof(int));
dt.Columns.Add("TransactionDate", typeof(DateTime));
dt.Rows.Add(1, new DateTime(2022, 9, 3));
dt.Rows.Add(2, new DateTime(2022, 6, 3));
dt.Rows.Add(3, new DateTime(2022, 10, 1));
dt.Rows.Add(4, new DateTime(2022, 9, 11));
dt.Rows.Add(5, new DateTime(2022, 9, 12));
return dt;
}
private void button1_Click(object sender, EventArgs e)
{
// replace the two vars with DateTimePickers
var lowDate = new DateTime(2022, 9, 1);
var highDate = new DateTime(2022, 9, 12);
if (string.IsNullOrWhiteSpace(_bindingSource.Filter))
{
_bindingSource.Filter = $"TransactionDate >= '{lowDate}' AND TransactionDate <= '{highDate}'";
}
else
{
_bindingSource.Filter = "";
}
}
}
}
我想在两个日期时间选择器之间过滤 datagridview 而不涉及数据库。datagridview 具有内存中存储的记录。我正在使用 C# 和 .NET 框架。
我有一个名为“datagridview1”的 DataGridView,以及两个名为“dateTimePickerFromDate”和“dateTimePickerToDate”的日期时间选择器。所以我需要在 datagridview1 中搜索记录,其中记录仅在 2 个所选日期之间,我需要加载 datagridview1。该日期基于 datagridview1 中的 交易日期。
下面是一个示例,它使用具有最少数据的 DataTable 来加载过滤所需的 BindingSource。使用了两个变量,将它们替换为您的 DateTimePicker 控件。
Button1 中的代码切换过滤器。
using System;
using System.Data;
using System.Windows.Forms;
namespace YourNameSpace
{
public partial class DemoForm : Form
{
private readonly BindingSource _bindingSource = new BindingSource();
public DemoForm()
{
InitializeComponent();
_bindingSource.DataSource = MockedData();
dataGridView1.DataSource = _bindingSource;
}
public DataTable MockedData()
{
var dt = new DataTable();
dt.Columns.Add("Id", typeof(int));
dt.Columns.Add("TransactionDate", typeof(DateTime));
dt.Rows.Add(1, new DateTime(2022, 9, 3));
dt.Rows.Add(2, new DateTime(2022, 6, 3));
dt.Rows.Add(3, new DateTime(2022, 10, 1));
dt.Rows.Add(4, new DateTime(2022, 9, 11));
dt.Rows.Add(5, new DateTime(2022, 9, 12));
return dt;
}
private void button1_Click(object sender, EventArgs e)
{
// replace the two vars with DateTimePickers
var lowDate = new DateTime(2022, 9, 1);
var highDate = new DateTime(2022, 9, 12);
if (string.IsNullOrWhiteSpace(_bindingSource.Filter))
{
_bindingSource.Filter = $"TransactionDate >= '{lowDate}' AND TransactionDate <= '{highDate}'";
}
else
{
_bindingSource.Filter = "";
}
}
}
}