C# Datagridview没有';t正确过滤
我的datagridview的过滤器有问题。目前,我正在对我的数据(通过xml文件加载)应用3种类型的日期过滤器:C# Datagridview没有';t正确过滤,c#,windows,forms,datagridview,visual-studio-2015,C#,Windows,Forms,Datagridview,Visual Studio 2015,我的datagridview的过滤器有问题。目前,我正在对我的数据(通过xml文件加载)应用3种类型的日期过滤器: Month filter=>此筛选器使我能够显示与当前选定月份相关的所有数据 1日期筛选器=>显示与当前选定日期关联的所有数据 X日期过滤器=>显示与当前选定日期关联的所有数据(最多31个) 通过在CalendarMonth控件上选择日期应用筛选器。此方法上的筛选器处于激活状态: private void monthCalendar1_DateChanged(object send
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();
}
}
}
private void monthCalendar1\u DateChanged(对象发送方,DateRangeEventArgs e)
{
DateTime current\u date=this.monthCalendar1.SelectionStart;
如果(当前日期.Month!=此.old日期.Month |当前日期.Year!=此.old日期.Year)
{
(this.dataGridView1.DataSource as DataTable).DefaultView.RowFilter=“类似于“*/”的日期+当前日期.ToString(“MM/yyyy”)+”;
System.Console.WriteLine((this.dataGridView1.datasourceas DataTable.DefaultView.RowFilter);
this.old_Date=当前_日期;
this.label_total.Text=this.calculateSum();
this.label\u filter.Text=this.label\u total.Text;
this.label_month.Text=当前_date.ToString(“MMMM(yyyy)”;
}
其他的
{
如果(此.monthCalendar1.SelectionEnd.ToString(“dd/MM/yyyyy”)等于(此.monthCalendar1.SelectionStart.ToString(“dd/MM/yyyyy”))
{
(this.dataGridView1.datasourceas DataTable).DefaultView.RowFilter=“DATE=”+this.monthCalendar1.SelectionStart.ToString(“dd/MM/yyyyy”)+“;
this.label_filter.Text=this.calculateSum();
}
其他的
{
System.Console.WriteLine(“日期>=”+当前日期.ToString(“dd/MM/yyyy”)+“'和=”+本月日历1.SelectionStart.ToString(“dd/MM/yyyy”)+“'和日期当您按12月21日时会发生什么?如果没有结果,那么您可能应该检查数据表
和/或数据集
的区域设置
,这是网格的数据源
以下代码的结果是否符合您的预期(dd/MM/yyyy
)
我发现了错误
我使用了字符串格式的日期列,而不是日期时间格式。我已经对日期时间值进行了所有更改(也使用XML),现在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();
}
}
}
private void monthCalendar1\u DateChanged(对象发送方,DateRangeEventArgs e)
{
DateTime current\u date=this.monthCalendar1.SelectionStart;
//我们将确定是更改的月份/年份还是日期(对数据应用过滤器)
//月/年检查
如果(当前日期.Month!=此.old日期.Month |当前日期.Year!=此.old日期.Year)
{
//应用基本月和年过滤器(标准过滤器)
(this.dataGridView1.DataSource as DataTable)DefaultView.RowFilter=“[DATE]>=”+当前日期.ToString(“01/MM/yyyy”)+“,[DATE]=”+当前日期.ToString(“dd/MM/yyyy”)+“,[DATE]编辑选项似乎不起作用,然后我在这里发布=>我忘记了论坛消息中的基本内容:“嗨,伙计们。“。如果我只在12月21日按此键,我将获得12月21日的所有结果。选定日期工作文件^^上的筛选器。我不需要使用“Locale.DateTimeFormat”,因为代码给出的所有日期都是在我的区域设置时间(法语系统)。编辑:我已向11月21日添加了一个值,当我选择12月21日时,筛选器仍然良好。”(仅显示12月份的结果)@Bloops一个问题:哪个Windows 7?8?10?就在今天,我遇到了一个类似的奇怪问题,即仅Windows 10上的MaskedTextBox和日期格式。所有的区域设置都已设置,它仍然试图像处理我在美国的日期一样处理我的日期。我必须在绑定上单独设置FormatString。我现在正在Win 7(64位)上运行我的程序
Console.WriteLine((this.dataGridView1.DataSource as DataTable).Locale.DateTimeFormat.ShortDatePattern);
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();
}
}
}