如何通过提供初始日期和最终日期来过滤记录? [英] How do I filter records by supplying initial date and final date?

查看:106
本文介绍了如何通过提供初始日期和最终日期来过滤记录?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在Visual Studio 2015中开发一个C#.Net WindowsForm应用程序。我正在尝试通过在单击按钮时在表单上提供TextBoxes的初始和最终日期来过滤记录。你能看看我的代码并告诉我出了什么问题吗?对此,我真的非常感激。非常感谢!



我尝试过:



I'm developing a C# .Net WindowsForm application in Visual Studio 2015. I'm trying to filter records by supplying initial and final dates from TextBoxes on a Form when I click a button. Can you please take a look at my code and tell me what's wrong? I really appreciate it. Thanks a lot!

What I have tried:

private void buttonFilter_Click(object sender, EventArgs e)
{
    try
    {
        if (string.IsNullOrEmpty(textBoxDataIni.Text) | string.IsNullOrEmpty(textBoxDataFin.Text))
        {
            qry_retornosBindingSource.Filter = string.Empty;
        }
        else
        {

            qry_retornosBindingSource.Filter = string.Format("data >= {0}%' & data <= '{1}%'", textBoxDataIni.Text, textBoxDataFin.Text);
            this.qry_retornosTableAdapter.Fill(this.dBpetControlDataSet.qry_retornos);
        }
    }
    catch (Exception)
    {
    }
}

推荐答案

我解决了这个问题。我不得不将Convert.ToDateTime转换为TextBox文本并修复代码以过滤记录。请看下面的正确代码:





I solved the problem. I had to Convert.ToDateTime the TextBox Texts and fix the code to filter the records. SEe the correct code below:


private void buttonFilter_Click(object sender, EventArgs e)
{
    try
    {
        if (string.IsNullOrEmpty(textBoxDataIni.Text) || string.IsNullOrEmpty(textBoxDataFin.Text))
        {
            this.qry_retornosBindingSource.Filter = string.Empty;
        }
        else
        {
            this.qry_retornosBindingSource.Filter = string.Format("Data >= '{0}' AND Data <= '{1}'", Convert.ToDateTime(textBoxDataIni.Text), Convert.ToDateTime(textBoxDataFin.Text));
        }
    }
    catch (Exception)
    {
    }
}


这篇关于如何通过提供初始日期和最终日期来过滤记录?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆