在 BindingSource 中过滤 DateTimePicker 的日期值

发布于 2024-12-29 13:39:47 字数 920 浏览 1 评论 0原文

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"));

这非常有效!

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

The code above throuws an error of "Cannot perform '>=' operation on System.DateTime and System.Double."

I'm going to answer to my question.

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

This works perfectly!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

拥抱影子 2025-01-05 13:39:47

你可以这样做:

  // 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;
  }
}

you can do it like this:

  // 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);

FULL CODE

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;
  }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文