Datagridview没有正确过滤

时间:2015-12-22 12:33:47

标签: c# windows forms datagridview visual-studio-2015

我的datagridview过滤器有问题。目前,我正在将3种类型的过滤器应用于我的数据(由xml文件加载):

  1. 月过滤器=>此过滤器使我能够显示与所选月份相关的所有数据
  2. 1个日期过滤器=>显示与当前所选日期相关联的所有数据
  3. X date filter =>显示与当前所选日期(最多31个)相关联的所有数据
  4. 通过在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月份没有数据的日期。

    这是问题的GIF:

    过滤器的日志说:

    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选定的日期并没有像它应该做的那样工作..

    任何人都知道这个问题吗?

    有关信息:

    • Visual studio(社区)2015
    • C#
    • Windows窗体
    • 法语系统(法语日期时间,所以=&gt; dd / MM / yyyy)
    PS:抱歉,如果英语不完美。

    谢谢。

2 个答案:

答案 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;)