我正在尝试通过 2 个日期时间选择器 - startDate 和 endDate 过滤 datagridview 中的截止日期列。
datagridview是TaskTable2, datetimepicker1 是 startSchedule, datetimepicker2 是 endSchedule 和 datagridview 中的 deadline 是 deadlineRow
到目前为止,我已经获得了以下代码,它成功地使不在所选开始日期和结束日期之间的行不可见。
private void scheduleButton_Click(object sender, EventArgs e)
{
DateTime startSchedule = startDate.Value.Date;
DateTime endSchedule = endDate.Value.Date;
if (startSchedule <= endSchedule)// runs foreach loop if startdate and enddate are valid
{
foreach (DataGridViewRow dr in TaskTable2.Rows)// loops through rows of datagridview
{
string deadline = dr.Cells["Deadline"].Value.ToString(); // gets deadline values
DateTime deadlineRow = Convert.ToDateTime(deadline); // converts deadline string to datetime and stores in deadlineRow variable
if (startSchedule <= deadlineRow && deadlineRow <= endSchedule) // filters deadlines that are => startDate and <= endDate
{
dr.Visible = true; // display filtered rows here.
}
else
{
dr.Visible = false; // hide rows that are not beteen start and end date.
}
}
}
else
{
MessageBox.Show("Please ensure Start Date is set before End Date."); // ensures user selects an end date after the start date.
}
}
但是,我有一些存在的问题:
- 当我选择一个不显示任何任务的日期范围时,应用程序崩溃并且出现以下错误:
'与货币经理职位相关联的行不能设为不可见'
- 我有一个打印按钮,用于打印过滤后的结果。 但是,它正在打印存储在 datagridview 中的所有数据,即使某些行在按下计划按钮后可见=false,所以我猜我需要使用不同的方法来删除行而不是隐藏它们。
datagridview 绑定(bind)到 XML 文件,因此只要数据保留在 XML 文件中,就可以从 datagridview 中删除数据以进行过滤和打印。
如有任何帮助,我们将不胜感激!
谢谢
最佳答案
我会为 datagridview
使用 bindingsource
上的 Filter
属性。 Filter
属性允许您查看 DataSource 的子集。
示例来自 MSDN :
private void PopulateDataViewAndFilter()
{
DataSet set1 = new DataSet();
// Some xml data to populate the DataSet with.
string musicXml =
"<?xml version='1.0' encoding='UTF-8'?>" +
"<music>" +
"<recording><artist>Coldplay</artist><cd>X&Y</cd></recording>" +
"<recording><artist>Dave Matthews</artist><cd>Under the Table and Dreaming</cd></recording>" +
"<recording><artist>Dave Matthews</artist><cd>Live at Red Rocks</cd></recording>" +
"<recording><artist>Natalie Merchant</artist><cd>Tigerlily</cd></recording>" +
"<recording><artist>U2</artist><cd>How to Dismantle an Atomic Bomb</cd></recording>" +
"</music>";
// Read the xml.
StringReader reader = new StringReader(musicXml);
set1.ReadXml(reader);
// Get a DataView of the table contained in the dataset.
DataTableCollection tables = set1.Tables;
DataView view1 = new DataView(tables[0]);
// Create a DataGridView control and add it to the form.
DataGridView datagridview1 = new DataGridView();
datagridview1.AutoGenerateColumns = true;
this.Controls.Add(datagridview1);
// Create a BindingSource and set its DataSource property to
// the DataView.
BindingSource source1 = new BindingSource();
source1.DataSource = view1;
// Set the data source for the DataGridView.
datagridview1.DataSource = source1;
//The Filter string can include Boolean expressions.
source1.Filter = "artist = 'Dave Matthews' OR cd = 'Tigerlily'";
}
我使用这种类型的Filter
来显示基于帐户的数据。对于一个帐户,当用户输入帐号时我有一个文本框,我使用 TextChanged
事件来应用过滤器。然后我有一个按钮,用于从绑定(bind)源中删除过滤器。
如果您想按日期过滤,您可以按照此 SO 问题中的说明进行操作:
在不存在的日期上使用过滤器不应使应用程序崩溃,它只会不显示任何内容。
关于c# - 使用日期过滤数据 GridView ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9684897/