DataGridView的日期过滤器

时间:2019-05-28 15:28:30

标签: c# datetime datagridview

我需要为DataGridView创建一个日期过滤器,该过滤器通过此代码获取数据

    DataSet dataSet = new DataSet();
    OleDbDataAdapter dataAdapter = new OleDbDataAdapter();
    OleDbDataAdapter dataAdapter1 = new OleDbDataAdapter();

    string query = "select * from Договор";
    string query1 = "select * from Поставщики";

    conn = new OleDbConnection(conn_string);

    dataAdapter.SelectCommand = new OleDbCommand(query, conn);
    dataAdapter1.SelectCommand = new OleDbCommand(query1, conn);

    dataAdapter.TableMappings.Add("Table", "Договор");
    dataAdapter1.TableMappings.Add("Table", "Поставщики");

    dataAdapter.Fill(dataSet);
    dataAdapter1.Fill(dataSet);

    BindingSource dogSource = new BindingSource();
    dogSource.DataSource = dataSet;
    dogSource.DataMember = "Договор";
    dataGridView1.DataSource = dogSource;

    BindingSource postSource = new BindingSource();
    postSource.DataSource = dataSet;
    postSource.DataMember = "Поставщики";

    DataGridViewComboBoxColumn comboColumn = new DataGridViewComboBoxColumn();
    comboColumn.HeaderText = "Поставщики";
    comboColumn.DataSource = postSource;

    comboColumn.DataPropertyName = "Код поставщика";

    comboColumn.DisplayMember = "Название";

    comboColumn.ValueMember = "Код поставщика";

我曾经使用此代码过滤数据,但由于DataSource,现在它不起作用了。

    DataView dv = new DataView(dt);
    dv.RowFilter = string.Format(" CONVERT([Дата заключения], 'System.DateTime') >= '{0:dd.MM.yyyy}' AND CONVERT([Дата заключения], 'System.DateTime') <= '{0:dd.MM.yyyy}'",
    dateTimePicker1.Value.ToShortDateString());;
    dataGridView1.DataSource=dv;

我会寻求任何帮助,谢谢。

1 个答案:

答案 0 :(得分:1)

不确定是否正确,但这对我来说是可行的。

DataTable all = dataSet.Tables[0];
DataView dv = new DataView(all);
dv.RowFilter = string.Format(" [Дата заключения] >= '{0:dd.MM.yyyy}' AND [Дата заключения] <= '{0:dd.MM.yyyy}'", dateTimePicker1.Value.ToShortDateString());
dataGridView1.DataSource=dv;