我的datagridview过滤器有问题。目前,我正在将3种类型的过滤器应用于我的数据(由xml文件加载):
通过在CalendarMonth控件上选择日期来应用过滤器。过滤器在此方法上处于活动状态:
private void monthCalendar1_DateChanged(object sender, DateRangeEventArgs e)
{
DateTime current_date = this.monthCalendar1.SelectionStart;
if (current_date.Month != this.old_Date.Month || current_date.Year != this.old_Date.Year)
{
(this.dataGridView1.DataSource as DataTable).DefaultView.RowFilter = "DATE LIKE '*/" + current_date.ToString("MM/yyyy") + "'";
System.Console.WriteLine((this.dataGridView1.DataSource as DataTable).DefaultView.RowFilter);
this.old_Date = current_date;
this.label_total.Text = this.calculateSum();
this.label_filter.Text = this.label_total.Text;
this.label_month.Text = current_date.ToString("MMMM (yyyy)");
}
else
{
if (this.monthCalendar1.SelectionEnd.ToString("dd/MM/yyyy").Equals(this.monthCalendar1.SelectionStart.ToString("dd/MM/yyyy")))
{
(this.dataGridView1.DataSource as DataTable).DefaultView.RowFilter = "DATE = '" + this.monthCalendar1.SelectionStart.ToString("dd/MM/yyyy") + "'";
this.label_filter.Text = this.calculateSum();
}
else
{
System.Console.WriteLine("DATE >= '" + current_date.ToString("dd/MM/yyyy") + "' AND <= '" + this.monthCalendar1.SelectionEnd.ToString("dd/MM/yyyy") + "'");
(this.dataGridView1.DataSource as DataTable).DefaultView.RowFilter = string.Format("DATE >= '" + this.monthCalendar1.SelectionStart.ToString("dd/MM/yyyy") + "' And DATE <= '" + this.monthCalendar1.SelectionEnd.ToString("dd/MM/yyyy") + "'");
this.label_filter.Text = this.calculateSum();
}
}
}
当我选择X时,问题就出现了。选择日期后,我有一个奇怪的错误。事实上,当我选择12/10到12/12时,过滤器会显示10月和10月的结果。十一月!这是12月份没有数据的日期。
过滤器的日志说:
DATE >= '09/12/2015' AND <= '10/12/2015'
DATE >= '09/12/2015' AND <= '11/12/2015'
DATE >= '09/12/2015' AND <= '12/12/2015'
DATE LIKE '*/01/2016'
DATE LIKE '*/12/2015'
正如我们在GIF中看到的,其他2个过滤器工作正常。但X选定的日期并没有像它应该做的那样工作..
任何人都知道这个问题吗?
有关信息:
谢谢。
答案 0 :(得分:0)
当你按12月21日时会发生什么?如果没有结果,那么您应该检查Locale
和/或DataTable
的{{1}},这是您网格的数据源。
以下代码的结果是您所期望的(DataSet
)?
dd/MM/yyyy
答案 1 :(得分:0)
我找到了错误^^。
我使用了字符串格式的DATE列,而不是 DateTime 格式。我已经做了所有更改(也使用XML)来使用DateTime值,现在X日期的过滤器工作正常!
这里是新过滤方法的代码:
private void monthCalendar1_DateChanged(object sender, DateRangeEventArgs e)
{
DateTime current_date = this.monthCalendar1.SelectionStart;
// we will identify if is the month/year who as changed or the day (to apply a filter on the data)
// month/year check
if (current_date.Month != this.old_Date.Month || current_date.Year != this.old_Date.Year)
{
// applying basic month & year filter (standard filter)
(this.dataGridView1.DataSource as DataTable).DefaultView.RowFilter = "[DATE] >= '" + current_date.ToString("01/MM/yyyy") + "' AND [DATE] <= '"
+ DateTime.DaysInMonth(current_date.Year, current_date.Month).ToString() + current_date.ToString("/MM/yyyy") + "'";
System.Console.WriteLine((this.dataGridView1.DataSource as DataTable).DefaultView.RowFilter);
this.old_Date = current_date;
this.label_total.Text = this.calculateSum();
this.label_filter.Text = this.label_total.Text;
this.label_month.Text = current_date.ToString("MMMM (yyyy)");
}
else
{
if (this.monthCalendar1.SelectionEnd.ToString("dd/MM/yyyy").Equals(current_date.ToString("dd/MM/yyyy")))
{
//only one date is selected
// the day as changed
(this.dataGridView1.DataSource as DataTable).DefaultView.RowFilter = "[DATE] = '" + current_date.ToString("dd/MM/yyyy") + "'";
System.Console.WriteLine((this.dataGridView1.DataSource as DataTable).DefaultView.RowFilter);
this.label_filter.Text = this.calculateSum();
}
else
{
// more than 1 date is selected
(this.dataGridView1.DataSource as DataTable).DefaultView.RowFilter = string.Format("[DATE] >= '" + current_date.ToString("dd/MM/yyyy") + "' AND [DATE] <= '" + this.monthCalendar1.SelectionEnd.ToString("dd/MM/yyyy") + "'");
System.Console.WriteLine((this.dataGridView1.DataSource as DataTable).DefaultView.RowFilter);
this.label_filter.Text = this.calculateSum();
}
}
}
帮助@Arie的Thx;)