注册 登录
编程论坛 C# 论坛

求解datagridview的条件查询问题

快手刘1号 发布于 2015-03-11 08:40, 578 次点击
      private void ShowDataGridView()
        {
            SQL_conn();
           if (comboBox1.SelectedIndex == -1 && comboBox2.SelectedIndex == -1)
            {
                cmdtext = "select ReiNumber as 识别码,ReiDate as 日期,Department as 部门,name as 出差人,Reason as 出差事由,Cashier as 出纳,Payee as 收款人,TotalRei as 金额 from TravelExpense2";
            }
            else
            {
                if (comboBox1.SelectedIndex == -1&& comboBox2.SelectedIndex != -1)
                {
                    cmdtext = "select ReiNumber as 识别码,ReiDate as 日期,Department as 部门,name as 出差人,Reason as 出差事由,Cashier as 出纳,Payee as 收款人,TotalRei as 金额 from TravelExpense2 where name = " + comboBox2.Text;
                }
                if (comboBox1.SelectedIndex != -1&& comboBox2.SelectedIndex == -1)
                {
                    cmdtext = "select ReiNumber as 识别码,ReiDate as 日期,Department as 部门,name as 出差人,Reason as 出差事由,Cashier as 出纳,Payee as 收款人,TotalRei as 金额 from TravelExpense2 where Department = " + comboBox1.Text;
                }
                if(comboBox1.SelectedIndex != -1&& comboBox2.SelectedIndex != -1)
                {
                cmdtext = "select ReiNumber as 识别码,ReiDate as 日期,Department as 部门,name as 出差人,Reason as 出差事由,Cashier as 出纳,Payee as 收款人,TotalRei as 金额 from TravelExpense2  where Department = '" + comboBox1.Text + "' and name=" + comboBox2.Text;
                }
           }
            SQLiteDataAdapter sda = new SQLiteDataAdapter(cmdtext, conn);
            DataSet ds = new DataSet();
            sda.Fill(ds, "TravelExpense2");
            this.dataGridView1.DataSource = ds.Tables["TravelExpense2"];
            dataGridView1.Visible = true;
            conn.Close();
        
        }
执行时,总是提示数据库表中没有combobox控件中选中的值,请问应该怎么修改?感激不尽!
3 回复
#2
快手刘1号2015-03-11 14:41
没人会?难道论坛里的都是新手?
#3
Maick2015-03-13 14:30
sql 语句错了..如果是文本的话要用 ' ' 引起来..我看你只有后面的其中一个用了单引号 ''
#4
快手刘1号2015-03-13 17:29
回复 3楼 Maick
恩,前天的时候已经做出来了
1