Фильтровать значение даты DateTimePicker в BindingSource - PullRequest
0 голосов
/ 30 января 2012
bsCheckVoucherGridView.Filter = 
    string.Format("[CHECK DATE] >= #{0:M/dd/yyyy}# ", 
    dateTimePicker_CheckVoucher_SearchCheckVoucherDate.Value);    
dateTimePicker_CheckVoucher_SearchCheckVoucherDate.Value = DateTime.Now.AddYears(-1);
dateTimePicker_CheckVoucher_SearchCheckVoucherDate.Format = DateTimePickerFormat.Short;
bsCheckVoucherGridView.DataSource = 
    db.GetDataTable("SELECT `CHECK_DATE` AS `CHECK DATE` FROM check_voucher");
bsCheckVoucherGridView.Filter = 
    string.Format("[CHECK DATE] >= {0:M/dd/yyyy} ",
    dateTimePicker_CheckVoucher_SearchCheckVoucherDate.Value);

Приведенный выше код приводит к ошибке «Невозможно выполнить операцию>>» в System.DateTime и System.Double. »

Я собираюсь ответить на мой вопрос.

bsCheckVoucherGridView.Filter = 
    string.Format("[CHECK DATE] >= #{0}# ", 
    dateTimePicker_CheckVoucher_SearchCheckVoucherDate.Value.ToString("dd-MMM-yyyy"));

Это прекрасно работает!

1 Ответ

0 голосов
/ 30 января 2012

вы можете сделать это так:

  // Setup Filter and Sort Criteria
  strExpr = "OrderDate >= '01.03.1998' AND OrderDate <= '31.03.1998'";
  // OR
  // strExpr = "OrderDate >= #01.03.1998# AND OrderDate <= #31.03.1998#";
  strSort = "OrderDate DESC";

  // Use the Select method to find all rows matching the filter.
  foundRows = myTable.Select(strExpr, strSort);

ПОЛНЫЙ КОД

private void BtnFilterAndSort_Click(object sender, System.EventArgs e)
{
  string  strText;
  string  strExpr;
  string  strSort;
  DataRow[] foundRows;
  DataTable myTable;
  myTable = ds.Tables["Orders"];

  // Setup Filter and Sort Criteria
  strExpr = "OrderDate >= '01.03.1998' AND OrderDate <= '31.03.1998'";
  strSort = "OrderDate DESC";

  // Use the Select method to find all rows matching the filter.
  foundRows = myTable.Select(strExpr, strSort);

  // Apply all Columns to the TextBox, this
  // must be done Row-By-Row.
  strText = null;
  for (int i = 0 ; i <= foundRows.GetUpperBound(0); i++)
  {
    for (int j = 0; j <= foundRows[i].ItemArray.GetUpperBound(0); j++)
    {
      strText = strText + foundRows[i][j].ToString() + "\t";
    }
    strText = strText + "\r\n";
    textBox.Text = strText;
  }
}
...