繁体   English   中英

C#筛选Datagridview与2或更多的文本框列表进行搜索

[英]C# Filter Datagridview with 2 or more list of textbox to search

要打印报告,我需要搜索具有相同材料类型和交货日期等的几种材料。

所以我决定创建一个名为“ REPORTS”的新表单,然后在其中添加了4个标签和2个文本框,2个DateTimePicker和1个按钮以及1个DataGridView

标签名称:

label1 = "Material type:"
label2 = "Material Class" 
label3 = "start Date"
label4 = "end Date"

TEXTBOXS

textbox1="material Type"
textbox2="material class"

dateTimePicker的

datetimepicker1="Start date"
datetimepicker2="End date"

BUTTON

button1="SEARCH"

的DataGridView

DataGridView1="myDataGrid"

我的数据库

 void DGview()
    {

    string Constring = "datasource = localhost;port=3306;username=root;password=mysqlpassword";
                    MySqlConnection conDatabase = new MySqlConnection(Constring);
                    MySqlCommand cmddatabase = new MySqlCommand("select * from albany1_b1l1.civil_materials;", conDatabase);



                try
                {
                    MySqlDataAdapter sda = new MySqlDataAdapter();
                    sda.SelectCommand = cmddatabase;
                    DataTable dbdataset = new DataTable();
                    sda.Fill(dbdataset);
                    BindingSource bSource = new BindingSource();

                    bSource.DataSource = dbdataset;
                    myDataGrid.DataSource = bSource;
                    sda.Update(dbdataset);



                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.Message);
                }

    }

然后我将其添加到公开报告()

 public Reports()
    {
        InitializeComponent();
        datagridviewONE();
    }

这是问题

button1

 private void SEARCH_Click(object sender, EventArgs e)
    {
        BindingSource bs = new BindingSource();
        bs.DataSource = myDataGrid.DataSource;
        bs.Filter = "material_class,material_type,last_delivery_date like '%" + m_class.Text + "%','%" + m_type.Text + "%','%" + start_date.Text + "%'";
        myDataGrid.DataSource = bs;
    }

没有错误,但我总是收到指向此代码的警告

bs.Filter = "material_class,material_type,last_delivery_date like '%" + m_class.Text + "%','%" + m_type.Text + "%','%" + start_date.Text + "%'";

您认为是什么问题?

bs.Filter = "material_class,material_type,last_delivery_date like '%" + m_class.Text + "%','%" + m_type.Text + "%','%" + start_date.Text + "%'";

改成

 bs.Filter = "material_class like '%" + m_class.Text + "%' and 
 material_type '%" + m_type.Text + "%' and 
 last_delivery_date '%" + start_date.Text + "%'";

我刚刚解决了问题,谢谢“豆腐”对我的帮助。

BindingSource bs = new BindingSource();
        bs.DataSource = myDataGrid.DataSource;
        bs.Filter = "material_description like '%" + m_description.Text + "%' AND material_type like '%" + m_type.Text + "%'";
        myDataGrid.DataSource = bs;

而不是使用OR我使用AND

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM