WINFORM 多条件动态查询 通用代码的设计与实现

  

          经常碰到多条件联合查询的问题,以前的习惯认为很简单总会从头开始设计布局代码,往往一个查询面要费上老半天的功夫,而效果也不咋地。

       前段时间做了个相对通用的多条件动态查询面,复用起来还是挺方便的,放上来共参考指导 。
                                                                         供下载的源文件链接  :   多条件动态查询通用模板下载

         主要的运行后布局:

            a.JPG

   
    主要的通用功能和要求:

           
       
主要的方法体:

      动态的显示查询条件:

              

 1 None.gif     // 初始化联合查询的页面显示
 2 None.gif
 3 None.gif         private   void  ConditionBind()
 4 ExpandedBlockStart.gifContractedBlock.gif         dot.gif {
 5ExpandedSubBlockStart.gifContractedSubBlock.gif            /**///查询条件邦定
 6InBlock.gif            DataTable dt = new DataTable();
 7InBlock.gif            DataColumnCollection columns = dt.Columns;
 8InBlock.gif            columns.Add("name");
 9InBlock.gif            columns.Add("key");
10InBlock.gif            DataRowCollection rows = dt.Rows;
11InBlock.gif            rows.Add("所有""All");
12InBlock.gif            rows.Add("单据号""Code");
13InBlock.gif            rows.Add("供应商名称""SupplierName");
14InBlock.gif            rows.Add("经办人""EmployeesName");
15InBlock.gif            rows.Add("时间""time");  
18InBlock.gif            try
19ExpandedSubBlockStart.gifContractedSubBlock.gif            dot.gif{
20InBlock.gif                for (int i = 0; i < this.fpl.Controls.Count; i++)
21ExpandedSubBlockStart.gifContractedSubBlock.gif                dot.gif{
23InBlock.gif                    if (this.Controls.Find("fpl" + i, true).Length > 0)
24ExpandedSubBlockStart.gifContractedSubBlock.gif                    dot.gif{
25InBlock.gif                        ((ComboBox)this.Controls.Find("cbSelect" + i, true)[0]).DisplayMember = "name";
26InBlock.gif                        ((ComboBox)this.Controls.Find("cbSelect" + i, true)[0]).ValueMember = "key";
27InBlock.gif                        //用copy解决联动问题
28InBlock.gif                        ((ComboBox)this.Controls.Find("cbSelect" + i, true)[0]).DataSource = dt.Copy();
29ExpandedSubBlockEnd.gif                    }

31ExpandedSubBlockEnd.gif                }

34ExpandedSubBlockEnd.gif            }

35InBlock.gif            catch (Exception ex)
36ExpandedSubBlockStart.gifContractedSubBlock.gif            dot.gif{
37InBlock.gif                MessageBox.Show(ex.Message);
38ExpandedSubBlockEnd.gif            }

42ExpandedBlockEnd.gif        }


          按查询条件设置显示模式:

        

 1 None.gif    private   void  SetFilterCondition( ref  ComboBox conditionselect)
 2 ExpandedBlockStart.gifContractedBlock.gif         dot.gif {
 3InBlock.gif            try
 4ExpandedSubBlockStart.gifContractedSubBlock.gif            dot.gif{
 5InBlock.gif                for (int i = 0; i < this.fpl.Controls.Count; i++)
 6ExpandedSubBlockStart.gifContractedSubBlock.gif                dot.gif{
 7InBlock.gif                    if (conditionselect.Name == "cbSelect" + i.ToString())
 8ExpandedSubBlockStart.gifContractedSubBlock.gif                    dot.gif{
 9InBlock.gif                        if (conditionselect.Text == "时间")
10ExpandedSubBlockStart.gifContractedSubBlock.gif                        dot.gif{
11InBlock.gif                            if (this.Controls.Find("fplFilter" + i, true).Length > 0)
12InBlock.gif                                this.Controls.Find("fplFilter" + i, true)[0].Visible = true;
13InBlock.gif                            if (this.Controls.Find("txtFilter" + i, true).Length > 0)
14InBlock.gif                                this.Controls.Find("txtFilter" + i, true)[0].Visible = false;
15InBlock.gif                            if (this.Controls.Find("cbFilter" + i, true).Length > 0)
16InBlock.gif                                this.Controls.Find("cbFilter" + i, true)[0].Visible = false;
17ExpandedSubBlockEnd.gif                        }

18InBlock.gif                        else if (conditionselect.Text == "供应商名称")//在通用中需要修改或添加
19ExpandedSubBlockStart.gifContractedSubBlock.gif                        dot.gif{
20InBlock.gif                            if (this.Controls.Find("fplFilter" + i, true).Length > 0)
21InBlock.gif                                this.Controls.Find("fplFilter" + i, true)[0].Visible = false;
22InBlock.gif                            if (this.Controls.Find("txtFilter" + i, true).Length > 0)
23InBlock.gif                                this.Controls.Find("txtFilter" + i, true)[0].Visible = false;
24InBlock.gif                            if (this.Controls.Find("cbFilter" + i, true).Length > 0)
25InBlock.gif                                this.Controls.Find("cbFilter" + i, true)[0].Visible = true;
26ExpandedSubBlockEnd.gif                        }

27InBlock.gif                        else
28ExpandedSubBlockStart.gifContractedSubBlock.gif                        dot.gif{
29InBlock.gif                            if (this.Controls.Find("fplFilter" + i, true).Length > 0)
30InBlock.gif                                this.Controls.Find("fplFilter" + i, true)[0].Visible = false;
31InBlock.gif                            if (this.Controls.Find("txtFilter" + i, true).Length > 0)
32InBlock.gif                                this.Controls.Find("txtFilter" + i, true)[0].Visible = true;
33InBlock.gif                            if (this.Controls.Find("cbFilter" + i, true).Length > 0)
34InBlock.gif                                this.Controls.Find("cbFilter" + i, true)[0].Visible = false;
36ExpandedSubBlockEnd.gif                        }

40ExpandedSubBlockEnd.gif                    }

41ExpandedSubBlockEnd.gif                }

42ExpandedSubBlockEnd.gif            }

43InBlock.gif            catch (Exception ex)
44ExpandedSubBlockStart.gifContractedSubBlock.gif            dot.gif{
45InBlock.gif                MessageBox.Show(ex.Message);
46ExpandedSubBlockEnd.gif            }

47ExpandedBlockEnd.gif        }

 


添加条件 

 1 None.gif
 2 None.gif         private   void  AddFilter()
 3 ExpandedBlockStart.gifContractedBlock.gif         dot.gif {
 4InBlock.gif            try
 5ExpandedSubBlockStart.gifContractedSubBlock.gif            dot.gif{
 6InBlock.gif                for (int i = 0; i < this.fpl.Controls.Count; i++)
 7ExpandedSubBlockStart.gifContractedSubBlock.gif                dot.gif{
 8ContractedSubBlock.gifExpandedSubBlockStart.gif                    可替换代码#region 可替换代码
 9InBlock.gif                    //if (((FlowLayoutPanel)this.fpl.Controls[i]).Visible == false)
10InBlock.gif                    //{
11InBlock.gif                    //    ((FlowLayoutPanel)this.fpl.Controls[i]).Visible = true;
12InBlock.gif                    //    break;
13InBlock.gif                    //}
14ExpandedSubBlockEnd.gif                    #endregion

16InBlock.gif                    if (this.Controls.Find("fpl" + i, true).Length > 0)
17ExpandedSubBlockStart.gifContractedSubBlock.gif                    dot.gif{
18InBlock.gif                        if (this.Controls.Find("fpl" + i, true)[0].Visible == false)
19ExpandedSubBlockStart.gifContractedSubBlock.gif                        dot.gif{
20InBlock.gif                            this.Controls.Find("fpl" + i, true)[0].Visible = true;
21InBlock.gif                            break;
22ExpandedSubBlockEnd.gif                        }

23ExpandedSubBlockEnd.gif                    }

25ExpandedSubBlockEnd.gif                }

26ExpandedSubBlockEnd.gif            }

27InBlock.gif            catch (Exception ex)
28ExpandedSubBlockStart.gifContractedSubBlock.gif            dot.gif{
29InBlock.gif                MessageBox.Show(ex.Message);
30ExpandedSubBlockEnd.gif            }

31ExpandedBlockEnd.gif        }

          
提取sql语句

 

        

 1 None.gif    private   string  BuildSQL()
 2 ExpandedBlockStart.gifContractedBlock.gif         dot.gif {
 3InBlock.gif            try
 4ExpandedSubBlockStart.gifContractedSubBlock.gif            dot.gif{
 6InBlock.gif                StringBuilder sb = new StringBuilder();
 7InBlock.gif                //需要的时候修改表明 得到通用
 8InBlock.gif                sb.Append("select * from InStoreBill_View ");
 9InBlock.gif                //用于判断是否是第一条数据 用于添加where的判断
10InBlock.gif                int isFirst = 0;
11InBlock.gif                for (int i = 0; i < this.fpl.Controls.Count; i++)
12ExpandedSubBlockStart.gifContractedSubBlock.gif                dot.gif{
13ContractedSubBlock.gifExpandedSubBlockStart.gif                    生成sql语句#region   生成sql语句
14InBlock.gif                    if (this.Controls.Find("fpl" + i, true)[0].Visible == true)
15ExpandedSubBlockStart.gifContractedSubBlock.gif                    dot.gif{
16InBlock.gif
17InBlock.gif                        if (this.Controls.Find("cbSelect" + i, true)[0].Text != "所有")
18ExpandedSubBlockStart.gifContractedSubBlock.gif                        dot.gif{
19InBlock.gif                            ComboBox selectCondition = (ComboBox)this.Controls.Find("cbSelect" + i, true)[0];
20InBlock.gif
21InBlock.gif                            if (this.Controls.Find("txtFilter" + i, true)[0].Visible == true)
22ExpandedSubBlockStart.gifContractedSubBlock.gif                            dot.gif{//为本类型
23InBlock.gif                                isFirst++;
24InBlock.gif                                if (isFirst == 1)//如果是第一次进入的话  isfirst应该为1
25ExpandedSubBlockStart.gifContractedSubBlock.gif                                dot.gif{
26InBlock.gif                                    sb.Append("  where  ");
27ExpandedSubBlockEnd.gif                                }

28InBlock.gif                                else if (isFirst > 1)
29ExpandedSubBlockStart.gifContractedSubBlock.gif                                dot.gif{
30InBlock.gif                                    sb.Append(" and ");
31ExpandedSubBlockEnd.gif                                }

32InBlock.gif                                else
33ExpandedSubBlockStart.gifContractedSubBlock.gif                                dot.gif{ }
34InBlock.gif
35InBlock.gif                                sb.Append(string.Format(" {0} like '%{1}%' ", selectCondition.SelectedValue.ToString().Trim(), this.Controls.Find("txtFilter" + i.ToString(), true)[0].Text.Trim()));
36InBlock.gif
37ExpandedSubBlockEnd.gif                            }

38InBlock.gif                            else if (this.Controls.Find("cbFilter" + i, true)[0].Visible == true)
39ExpandedSubBlockStart.gifContractedSubBlock.gif                            dot.gif{//下拉框类型
40InBlock.gif                                isFirst++;
41InBlock.gif                                if (isFirst == 1)//如果是第一次进入的话  isfirst应该为1
42ExpandedSubBlockStart.gifContractedSubBlock.gif                                dot.gif{
43InBlock.gif                                    sb.Append("  where  ");
44ExpandedSubBlockEnd.gif                                }

45InBlock.gif                                else if (isFirst > 1)
46ExpandedSubBlockStart.gifContractedSubBlock.gif                                dot.gif{
47InBlock.gif                                    sb.Append(" and ");
48ExpandedSubBlockEnd.gif                                }

49InBlock.gif                                else
50ExpandedSubBlockStart.gifContractedSubBlock.gif                                dot.gif{ }
51InBlock.gif                                sb.Append(string.Format(" {0} like '%{1}%' ", selectCondition.SelectedValue.ToString().Trim(), this.Controls.Find("cbFilter" + i.ToString(), true)[0].Text.Trim()));
52InBlock.gif
53InBlock.gif
54ExpandedSubBlockEnd.gif                            }

55InBlock.gif                            else
56ExpandedSubBlockStart.gifContractedSubBlock.gif                            dot.gif{//时间类型
57InBlock.gif                                isFirst++;
58InBlock.gif                                if (isFirst == 1)//如果是第一次进入的话  isfirst应该为1
59ExpandedSubBlockStart.gifContractedSubBlock.gif                                dot.gif{
60InBlock.gif                                    sb.Append("  where  ");
61ExpandedSubBlockEnd.gif                                }

62InBlock.gif                                else if (isFirst > 1)
63ExpandedSubBlockStart.gifContractedSubBlock.gif                                dot.gif{
64InBlock.gif                                    sb.Append(" and ");
65ExpandedSubBlockEnd.gif                                }

66InBlock.gif                                else
67ExpandedSubBlockStart.gifContractedSubBlock.gif                                dot.gif{ }
68InBlock.gif                                sb.Append(string.Format(" {0} between '{1}' and '{2}' ", selectCondition.SelectedValue.ToString().Trim(), ((DateTimePicker)this.Controls.Find("dtp" + i.ToString() + "Begin"true)[0]).Value.ToShortDateString(), ((DateTimePicker)this.Controls.Find("dtp" + i.ToString() + "End"true)[0]).Value.ToShortDateString()));
69InBlock.gif
70ExpandedSubBlockEnd.gif                            }

71InBlock.gif
72ExpandedSubBlockEnd.gif                        }

73ExpandedSubBlockEnd.gif                    }

74ExpandedSubBlockEnd.gif                    #endregion

75ExpandedSubBlockEnd.gif                }

76InBlock.gif                return sb.ToString();
77ExpandedSubBlockEnd.gif            }

78InBlock.gif            catch (Exception ex)
79ExpandedSubBlockStart.gifContractedSubBlock.gif            dot.gif{
80InBlock.gif                MessageBox.Show(ex.Message);
81InBlock.gif                return "";
82ExpandedSubBlockEnd.gif            }

84ExpandedBlockEnd.gif        }

 

       注: 在设计过程中觉得最烦乱得是布局的设计 ,也许是不太熟练,浪费了很多的时间,好在通用或之际copy就ok了

      供下载的源文件链接  :   多条件动态查询通用模板下载

转载于:https://www.cnblogs.com/huomm/archive/2008/03/22/1117552.html

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值