1
yangle
2023-05-26 301e6a60651ad026c3d9773e7bf6d9c76e936ca5
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
 
namespace Pub_Control
{
    public partial class frm_S_BillQueryCondition_Base : Form
    {
        public frm_S_BillQueryCondition_Base()
        {
            InitializeComponent();
        }
        const int IDCol = 0;
        const int BracketLCol = 1;
        const int FieldNameCol = 2;
        const int ControlCol = 3;
        const int ConditionCol = 4;
        const int BracketRCol = 5;
        const int RelationCol = 6;
        const int FindCol = 7;
        //
        public string SqlStr = "";
        public string FieldsArray = "";
        private string mvarViewName = "";
        private int CopyRow = 0;
        private string CopyField = "";
        private string CopyDataType = "";
        private bool Copyed;
        public Pub_Class.ClsPub.Enum_OKTag OKTag;
        public string ModName;
        public string ViewName;
        Pub_Class.ClsSqlHelper oCn = new Pub_Class.ClsSqlHelper();
        //
        Clsxt_ICSchemeMain oItem = new Clsxt_ICSchemeMain();
        public Pub_Class.ClsPub.Enum_BillStatus BillStatus;
        //当前选中方案ID
        Int64 HInterID = 0;
        //
        public void SetViewName(string sViewName,bool b)
        {
            string[] tArray;
            DataSet Ds = new DataSet();
            int j;
            if (Pub_Class.ClsPub.isStrNull(sViewName).Length == 0)
            {
                return;
            }
            mvarViewName = sViewName;
            Ds = oCn.RunProcReturn("select column_name,data_type from information_schema.columns where table_name ='" + ViewName.Trim() + "' order by  ORDINAL_POSITION", "information");
            grdCondition.RowCount = 1;
            for (int i = 0; i < Ds.Tables[0].Rows.Count; i++)
            {
                //grid1
                grdCondition.RowCount = i + 1;
                grdCondition.Rows[i].Cells[FieldNameCol].Value = Ds.Tables[0].Rows[i]["column_name"].ToString().Trim();
                grdCondition.Rows[i].Cells[IDCol].Value = Ds.Tables[0].Rows[i]["data_type"].ToString().Trim();
                //
                DataGridViewComboBoxCell sCellRel = new DataGridViewComboBoxCell();
                sCellRel.FlatStyle = FlatStyle.System;
                sCellRel.DisplayStyle = DataGridViewComboBoxDisplayStyle.Nothing;
                sCellRel.DropDownWidth = 10;
                sCellRel.Items.Add("并且");
                sCellRel.Items.Add("或者");
                grdCondition.Rows[i].Cells[RelationCol] = sCellRel;
                grdCondition.Rows[i].Cells[6].Value = "并且";
                //
                DataGridViewComboBoxCell sCellCom = new DataGridViewComboBoxCell();
                sCellCom.FlatStyle = FlatStyle.System;
                sCellCom.DisplayStyle = DataGridViewComboBoxDisplayStyle.Nothing;
                sCellCom.DropDownWidth = 10;
                sCellCom.Items.Add(" ");
                sCellCom.Items.Add("=");
                sCellCom.Items.Add(">");
                sCellCom.Items.Add(">=");
                sCellCom.Items.Add("<");
                sCellCom.Items.Add("<=");
                sCellCom.Items.Add("<>");
                sCellCom.Items.Add("包含");
                sCellCom.Items.Add("左包含");
                sCellCom.Items.Add("右包含");
                grdCondition.Rows[i].Cells[ControlCol] = sCellCom;
                //
                if (Ds.Tables[0].Rows[i]["column_name"].ToString().Trim().ToUpper().Substring(0, 1) == "H")//H开头的列隐藏
                {
                    grdCondition.Rows[i].Visible = false; 
                }
                
                //
                DataGridViewCheckBoxCell sCell = new DataGridViewCheckBoxCell();
                sCell.ThreeState = false;
                sCell.Value = 1;
 
            }
            if (!b)
            {
                Sub_GetSchemeInfo();
            }
 
        }
 
        //读取方案信息
        private void Sub_GetSchemeInfo()  //Lock
        {
            //当前选中方案,若没有选中则为 默认方案,若不存在默认方案则自动新增
            if (HInterID == 0)
            {
                if (oItem.ShowDefault(this.ModName))
                {
                    HInterID = oItem.HInterID;
                }
                else//不存在则 生成默认方案
                {
                    return;
                }
            }
            if (!oItem.ShowBill(HInterID, ref Pub_Class.ClsPub.sExeReturnInfo))
                return;
            //foreach (Clsxt_ICSchemeSub oSub in oItem.DetailColl)
            //{
            //    //循环控件,找到则赋值
            //    foreach (Control ct in this.tabPage1.Controls)
            //    {
            //        if (oSub.HKey.Trim().ToLower() == ct.Name.Trim().ToLower())
            //        {
            //            if (ct.Name.Trim().ToLower().Substring(0, 3) == "txt" || ct.Name.Trim().ToLower().Substring(0, 3) == "cmb")
            //            {
            //                ct.Text = oSub.HValue.Trim();
            //            }
            //        }
            //    }
            //    foreach (Control ct in this.panel8.Controls)
            //    {
            //        if (oSub.HKey.Trim().ToLower() == ct.Name.Trim().ToLower())
            //        {
            //            if (ct.Name.Trim().ToLower().Substring(0, 3) == "txt" || ct.Name.Trim().ToLower().Substring(0, 3) == "cmb")
            //            {
            //                ct.Text = oSub.HValue.Trim();
            //            }
            //        }
            //    }
            //}
            //得到3个GRID
            try
            {
                Pub_Class.ClsSqlHelper oCn2 = new Pub_Class.ClsSqlHelper();
                DataSet DSet;
                for (int i = 0; i < grdCondition.RowCount; i++)
                {
                    DSet = oCn2.RunProcReturn("select * from Xt_grdCondition where HUserID='" + Pub_Class.ClsPub.CurUserName + "' and HBillName='" + ViewName.Trim() + "' and HRow=" + i, "Xt_grdCondition");
                    if (Pub_Class.ClsPub.isInt(DSet.Tables[0].Rows.Count) != 0)
                    {
                        grdCondition.Rows[i].Cells[0].Value = DSet.Tables[0].Rows[0]["HIDCol"];
                        grdCondition.Rows[i].Cells[1].Value = DSet.Tables[0].Rows[0]["HBracketLCol"];
                        grdCondition.Rows[i].Cells[2].Value = DSet.Tables[0].Rows[0]["HFieldNameCol"];
                        grdCondition.Rows[i].Cells[3].Value = DSet.Tables[0].Rows[0]["HControlCol"];
                        grdCondition.Rows[i].Cells[4].Value = DSet.Tables[0].Rows[0]["HConditionCol"];
                        grdCondition.Rows[i].Cells[5].Value = DSet.Tables[0].Rows[0]["HBracketRCol"];
                        grdCondition.Rows[i].Cells[6].Value = DSet.Tables[0].Rows[0]["HRelationCol"];
                    }
                }
            }
            catch (Exception e)
            {
                return;
            }
        }
 
        //
        private bool Sub_DeleteSchemeInfo()
        {
            MessageBox.Show("此功能有待完善!");
            return false;
        }
 
        //保存方案
        private bool Sub_SaveSchemeInfo()  //Lock
        {
            //保存当前选中方案,若没有选中则为 默认方案
            if (HInterID == 0)
            {
                if (oItem.ShowDefault(this.ViewName))
                {
                    HInterID = oItem.HInterID;
                    BillStatus = Pub_Class.ClsPub.Enum_BillStatus.BillStatus_Modify;
                }
                else//不存在则 生成默认方案
                {
                    BillStatus = Pub_Class.ClsPub.Enum_BillStatus.BillStatus_AddNew;
                }
            }
            else
            {
                BillStatus = Pub_Class.ClsPub.Enum_BillStatus.BillStatus_Modify;
            }
            //加载到类
            oItem.HBillName = ModName;
            oItem.HBillType = oItem.MvarItemKey;
            oItem.HStopflag = false;
            oItem.HUserCode = Pub_Class.ClsPub.CurUserName;
            //
            oItem.DetailColl.Clear();
            //foreach (Control ct in this.tabPage1.Controls)
            //{
            //    Clsxt_ICSchemeSub oSub = new Clsxt_ICSchemeSub();
            //    if (ct.Name.Trim().ToLower().Substring(0, 3) == "txt" || ct.Name.Trim().ToLower().Substring(0, 3) == "cmb")
            //    {
            //        oSub.HKey = ct.Name.Trim();
            //        oSub.HValue = ct.Text.Trim();
            //        oSub.HInterID = this.HInterID;
            //        oItem.DetailColl.Add(oSub);
            //    }
            //}
            //foreach (Control ct in this.panel8.Controls)
            //{
            //    Clsxt_ICSchemeSub oSub = new Clsxt_ICSchemeSub();
            //    if (ct.Name.Trim().ToLower().Substring(0, 3) == "txt" || ct.Name.Trim().ToLower().Substring(0, 3) == "cmb")
            //    {
            //        oSub.HKey = ct.Name.Trim();
            //        oSub.HValue = ct.Text.Trim();
            //        oSub.HInterID = this.HInterID;
            //        oItem.DetailColl.Add(oSub);
            //    }
            //}
            //
            if (BillStatus == Pub_Class.ClsPub.Enum_BillStatus.BillStatus_Modify)
            {
                if (!oItem.ModifyBill(HInterID, ref  Pub_Class.ClsPub.sExeReturnInfo))
                    return false;
            }
            else if (BillStatus == Pub_Class.ClsPub.Enum_BillStatus.BillStatus_AddNew)
            {
                oItem.HName = "默认方案";
                if (!oItem.AddBill(ref Pub_Class.ClsPub.sExeReturnInfo, HInterID))
                    return false;
            }
            else
            {
                return false;
            }
            //保存3个GRID
            try
            {
                Pub_Class.ClsSqlHelper oCn1 = new Pub_Class.ClsSqlHelper();
                oCn1.RunProc("delete Xt_grdCondition where HUserID='" + Pub_Class.ClsPub.CurUserName + "' and HBillName='" + ViewName.Trim() + "'");
                for (int i = 0; i < grdCondition.RowCount; i++)
                {
 
                    oCn1.RunProc("Insert Into Xt_grdCondition   " +
                    "(HUserID,HBillName,HRow,HIDCol,HBracketLCol,HFieldNameCol,HControlCol" +
                    ",HConditionCol,HBracketRCol,HRelationCol" +
                    ") " +
                    " values('" + Pub_Class.ClsPub.CurUserName + "','" + ViewName.Trim() + "'," + i +
                    ",'" + Pub_Class.ClsPub.isStrNull(grdCondition.Rows[i].Cells[0].Value) + "'" +
                    ",'" + Pub_Class.ClsPub.isStrNull(grdCondition.Rows[i].Cells[1].Value) + "'" +
                    ",'" + Pub_Class.ClsPub.isStrNull(grdCondition.Rows[i].Cells[2].Value) + "'" +
                    ",'" + Pub_Class.ClsPub.isStrNull(grdCondition.Rows[i].Cells[3].Value) + "'" +
                    ",'" + Pub_Class.ClsPub.isStrNull(grdCondition.Rows[i].Cells[4].Value).Replace("'", "''") + "'" +
                    ",'" + Pub_Class.ClsPub.isStrNull(grdCondition.Rows[i].Cells[5].Value).Replace("'", "''") + "'" +
                    ",'" + Pub_Class.ClsPub.isStrNull(grdCondition.Rows[i].Cells[6].Value) + "')");
                }
                return true;
            }
            catch (Exception e)
            {
                return false;
            }
 
        }
 
        private void cmdCancel_Click(object sender, EventArgs e)
        {
            this.Tag = "CANCEL";
            SetViewName(mvarViewName,false);
            this.Visible = false;
        }
 
        private void cmdOk_Click(object sender, EventArgs e)
        {
            Sub_SaveSchemeInfo();
            if (GetSql())
            {
                this.Tag = "OK";
                this.Visible = false;
            }
        }
        //
        private void initGrid()
        {
            grdCondition.RowTemplate.Height = 18;
            grdCondition.RowTemplate.MinimumHeight = 18;
            grdCondition.ColumnHeadersHeight = 35;
            grdCondition.RowHeadersVisible = false;
            grdCondition.ColumnHeadersVisible = true;
            grdCondition.ColumnCount = 7;
            grdCondition.RowCount = 1;
            grdCondition.EnableHeadersVisualStyles = false;
            grdCondition.AllowUserToAddRows = false;             //是否允许自增--否
            grdCondition.AllowUserToDeleteRows = false;          //是否允许删除--否 
            grdCondition.AllowUserToResizeColumns = true;         //允许调整列宽--是
            grdCondition.AllowUserToResizeRows = false;           //允许调整行高--否
            grdCondition.AllowUserToOrderColumns = false;
            grdCondition.GridColor = Color.LightGray;
            grdCondition.Columns[BracketLCol].HeaderText = "左括号";
            grdCondition.Columns[FieldNameCol].HeaderText = "字段名称";
            grdCondition.Columns[ControlCol].HeaderText = "比较关系";
            grdCondition.Columns[ConditionCol].HeaderText = "数值";
            grdCondition.Columns[BracketRCol].HeaderText = "右括号";
            grdCondition.Columns[RelationCol].HeaderText = "逻辑";
            //
            grdCondition.EditMode = DataGridViewEditMode.EditOnEnter;      //编辑模式
            grdCondition.RowsDefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleRight;
            grdCondition.ColumnHeadersDefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleCenter;
            grdCondition.ColumnHeadersHeightSizeMode = DataGridViewColumnHeadersHeightSizeMode.DisableResizing; //允许用户调整列宽
            grdCondition.Columns[IDCol].Visible = false;
 
            grdCondition.Columns[BracketLCol].Width = 50;
            grdCondition.Columns[FieldNameCol].Width = 120;
            grdCondition.Columns[ControlCol].Width = 60;
            grdCondition.Columns[ConditionCol].Width = 90;
            grdCondition.Columns[BracketRCol].Width = 50;
            grdCondition.Columns[RelationCol].Width = 50;
            for (int i = 0; i < grdCondition.ColumnCount; i++)
            {
                grdCondition.Columns[i].SortMode = DataGridViewColumnSortMode.NotSortable;
            }
        }
 
        private void frm_S_BillQueryCondition_Base_Load(object sender, EventArgs e)
        {
            initGrid();
        }
 
        private void timer1_Tick(object sender, EventArgs e)
        {
            timer1.Enabled = false;
            SetViewName(ViewName,false);
        }
 
       
        private string CHStoENG(string sCHS)
        {
            switch (sCHS)
            {
                case "包含":
                    return "LIKE";
                case "左包含":
                    return "LIKEL";
                case "右包含":
                    return "LIKER";
                case "并且":
                    return "AND";
                case "或者":
                    return "OR";
                default:
                    return sCHS;
 
            }
        }
 
        //private void grdMain_BeforeEdit(object sender, AxVSFlex7._IVSFlexGridEvents_BeforeEditEvent e)
        //{
        //    string tStr = "";
        //    if (e.col == 2 || e.col == 1 || e.col == 5)
        //    {
        //        e.cancel = true;
        //        return;
        //    }
        //    if (e.col == 3)
        //    {
        //        switch (grdCondition.get_TextMatrix(e.row, IDCol).ToUpper().Trim())
        //        {
        //            case "NVARCHAR":
        //                tStr = " " +
        //                    "|=" +
        //                    "|>" +
        //                    "|>=" +
        //                    "|<" +
        //                    "|<=" +
        //                    "|<>" +
        //                    "|包含" +
        //                    "|左包含" +
        //                    "|右包含";
        //                break;
        //            case "CHAR":
        //                tStr = " " +
        //                    "|=" +
        //                    "|>" +
        //                    "|>=" +
        //                    "|<" +
        //                    "|<=" +
        //                    "|<>" +
        //                    "|包含" +
        //                    "|左包含" +
        //                    "|右包含";
        //                break;
        //            case "VARCHAR":
        //                tStr = " " +
        //                    "|=" +
        //                    "|>" +
        //                    "|>=" +
        //                    "|<" +
        //                    "|<=" +
        //                    "|<>" +
        //                    "|包含" +
        //                    "|左包含" +
        //                    "|右包含";
        //                break;
        //            case "BIT":
        //                tStr = " " +
        //                    "|=" +
        //                    "|<>";
        //                break;
        //            default:
        //                tStr = " " +
        //                    "|=" +
        //                    "|>" +
        //                    "|>=" +
        //                    "|<" +
        //                    "|<=" +
        //                    "|<>" +
        //                    "|包含" +
        //                    "|左包含" +
        //                    "|右包含";
        //                break;
        //        }
        //        grdCondition.set_ColComboList(3, tStr);
        //        return;
        //    }
        //    if (e.col == 6)
        //    {
        //        tStr = "并且|或者";
        //        grdCondition.set_ColComboList(6, tStr);
        //        return;
        //    }
        //    return;
        //}
 
        //private void grdMain_ValidateEdit(object sender, AxVSFlex7._IVSFlexGridEvents_ValidateEditEvent e)
        //{
        //    if (e.col == 4)
        //    {
        //        switch (grdCondition.get_TextMatrix(e.row, IDCol).Trim().ToUpper())
        //        {
        //            case "MONEY":
        //                //if (Pub_Class.ClsPub.IsNumeric(grdCondition.EditText) == false)
        //                //{
        //                    e.cancel = true;
        //                //}
        //                break;
        //            case "INT":
        //                //if (Pub_Class.ClsPub.IsNumeric(grdCondition.EditText) == false)
        //                //{
        //                    e.cancel = true;
        //                //}
        //                break;
        //            case "FLOAT":
        //                //if (Pub_Class.ClsPub.IsNumeric(grdCondition.EditText) == false)
        //                //{
        //                    e.cancel = true;
        //                //}
        //                break;
        //            default:
 
        //                break;
 
        //        }
        //    }
        //}
        //
        private bool GetSql()  //Lock
        {
            string tSql = "";
            string tFilter = "";
            string sEngCondi = "";
            string sLinkCondi = "";
            FieldsArray = tSql;
            //得到显示项目
            string HeadSql = "";
            HeadSql = "";
            //
            tSql = "select Top 5000 * " + HeadSql + " from " + mvarViewName.Trim() + " Where 1=1 ";
            for (int i = 0; i < grdCondition.RowCount; i++)
            {
                sEngCondi = CHStoENG(Pub_Class.ClsPub.isStrNull(grdCondition.Rows[i].Cells[3].Value));
                sLinkCondi = CHStoENG(Pub_Class.ClsPub.isStrNull(grdCondition.Rows[i].Cells[6].Value));  // and or
                if (sEngCondi.Trim().Length != 0)
                {
                    switch (sEngCondi.Trim().ToUpper())
                    {
                        case "LIKE":
                            tFilter = tFilter + Pub_Class.ClsPub.isStrNull(grdCondition.Rows[i].Cells[1].Value) + " " + Pub_Class.ClsPub.isStrNull(grdCondition.Rows[i].Cells[2].Value) + " LIKE '%" + Pub_Class.ClsPub.isStrNull(grdCondition.Rows[i].Cells[4].Value).Trim().Replace("'", "''") + "%' " + Pub_Class.ClsPub.isStrNull(grdCondition.Rows[i].Cells[5].Value) + " " + sLinkCondi.Trim() + " ";
                            break;
                        case "LIKEL":
                            tFilter = tFilter + Pub_Class.ClsPub.isStrNull(grdCondition.Rows[i].Cells[1].Value) + " " + Pub_Class.ClsPub.isStrNull(grdCondition.Rows[i].Cells[2].Value) + " LIKE '" + Pub_Class.ClsPub.isStrNull(grdCondition.Rows[i].Cells[4].Value).Trim().Replace("'", "''") + "%' " + Pub_Class.ClsPub.isStrNull(grdCondition.Rows[i].Cells[5].Value) + " " + sLinkCondi.Trim() + " ";
                            break;
                        case "LIKER":
                            tFilter = tFilter + Pub_Class.ClsPub.isStrNull(grdCondition.Rows[i].Cells[1].Value) + " " + Pub_Class.ClsPub.isStrNull(grdCondition.Rows[i].Cells[2].Value) + " LIKE '%" + Pub_Class.ClsPub.isStrNull(grdCondition.Rows[i].Cells[4].Value).Trim().Replace("'", "''") + "' " + Pub_Class.ClsPub.isStrNull(grdCondition.Rows[i].Cells[5].Value) + " " + sLinkCondi.Trim() + " ";
                            break;
                        default:
                            switch (Pub_Class.ClsPub.isStrNull(grdCondition.Rows[i].Cells[0].Value).Trim().ToUpper())
                            {
                                case "BIT":
                                    tFilter = tFilter + Pub_Class.ClsPub.isStrNull(grdCondition.Rows[i].Cells[1].Value) + " " + Pub_Class.ClsPub.isStrNull(grdCondition.Rows[i].Cells[2].Value) + sEngCondi + " " + Pub_Class.ClsPub.isStrNull(grdCondition.Rows[i].Cells[4].Value).Trim() + "  " + Pub_Class.ClsPub.isStrNull(grdCondition.Rows[i].Cells[5].Value) + " " + sLinkCondi.Trim() + " ";
                                    break;
                                case "MONEY":
                                    tFilter = tFilter + Pub_Class.ClsPub.isStrNull(grdCondition.Rows[i].Cells[1].Value) + " " + Pub_Class.ClsPub.isStrNull(grdCondition.Rows[i].Cells[2].Value) + sEngCondi + "" + Pub_Class.ClsPub.isStrNull(grdCondition.Rows[i].Cells[4].Value) + " " + Pub_Class.ClsPub.isStrNull(grdCondition.Rows[i].Cells[5].Value) + " " + sLinkCondi.Trim() + " ";
                                    break;
                                case "DECIMAL":
                                    tFilter = tFilter + Pub_Class.ClsPub.isStrNull(grdCondition.Rows[i].Cells[1].Value) + " " + Pub_Class.ClsPub.isStrNull(grdCondition.Rows[i].Cells[2].Value) + sEngCondi + "" + Pub_Class.ClsPub.isStrNull(grdCondition.Rows[i].Cells[4].Value) + " " + Pub_Class.ClsPub.isStrNull(grdCondition.Rows[i].Cells[5].Value) + " " + sLinkCondi.Trim() + " ";
                                    break;
                                default:
                                    tFilter = tFilter + Pub_Class.ClsPub.isStrNull(grdCondition.Rows[i].Cells[1].Value) + " " + Pub_Class.ClsPub.isStrNull(grdCondition.Rows[i].Cells[2].Value) + sEngCondi + "'" + Pub_Class.ClsPub.isStrNull(grdCondition.Rows[i].Cells[4].Value) + "' " + Pub_Class.ClsPub.isStrNull(grdCondition.Rows[i].Cells[5].Value) + " " + sLinkCondi.Trim() + " ";
                                    break;
                            }
                            break;
                    }
                }
                else
                {
                    if (Pub_Class.ClsPub.isStrNull(grdCondition.Rows[i].Cells[1].Value).Trim().Length != 0 || Pub_Class.ClsPub.isStrNull(grdCondition.Rows[i].Cells[5].Value).Trim().Length != 0)
                    {
                        MessageBox.Show("第" + i.ToString() + "行没有条件,不应该有括号出现,请取消此括号!", "提示");
                        return false;
                    }
                }
            }
            if (tFilter.Trim().Length != 0)
            {
                tFilter = tFilter.Substring(0, tFilter.Trim().Length - 3);
                SqlStr = tSql + " AND " + tFilter;
            }
            else
            {
                SqlStr = tSql;
            }
            return true;
        }
 
        //加括号
        private void cmdBracket_Click(object sender, EventArgs e)
        {
            int BRow = -1, ERow = 0;
            if (grdCondition.SelectedCells == null || grdCondition.SelectedCells.Count == 0)
            {
 
                return;
            }
            //找最大行,最小行
            for (int i = 0; i < grdCondition.SelectedCells.Count; i++)
            {
                if (grdCondition.SelectedCells[i].RowIndex > ERow)
                {
                    ERow = grdCondition.SelectedCells[i].RowIndex;
                }
                if (BRow == -1)
                {
                    BRow = grdCondition.SelectedCells[i].RowIndex;
                }
                else
                {
                    if (grdCondition.SelectedCells[i].RowIndex < BRow)
                    {
                        BRow = grdCondition.SelectedCells[i].RowIndex;
                    }
                }
            }
            //
            if (BRow != -1)
            {
                grdCondition.Rows[BRow].Cells[BracketLCol].Value = Pub_Class.ClsPub.isStrNull(grdCondition.Rows[BRow].Cells[BracketLCol].Value) + "(";
                grdCondition.Rows[ERow].Cells[BracketRCol].Value = Pub_Class.ClsPub.isStrNull(grdCondition.Rows[ERow].Cells[BracketRCol].Value) + ")";
            }
        }
        //减括号
        private void cmdUNBracket_Click(object sender, EventArgs e)
        {
            int BRow = -1, ERow = 0;
            if (grdCondition.SelectedCells == null || grdCondition.SelectedCells.Count == 0)
            {
 
                return;
            }
            //找最大行,最小行
            for (int i = 0; i < grdCondition.SelectedCells.Count; i++)
            {
                if (grdCondition.SelectedCells[i].RowIndex > ERow)
                {
                    ERow = grdCondition.SelectedCells[i].RowIndex;
                }
                if (BRow == -1)
                {
                    BRow = grdCondition.SelectedCells[i].RowIndex;
                }
                else
                {
                    if (grdCondition.SelectedCells[i].RowIndex < BRow)
                    {
                        BRow = grdCondition.SelectedCells[i].RowIndex;
                    }
                }
            }
            //
            if (Pub_Class.ClsPub.isStrNull(grdCondition.Rows[BRow].Cells[BracketLCol].Value) != "" && Pub_Class.ClsPub.isStrNull(grdCondition.Rows[ERow].Cells[BracketRCol].Value) != "")
            {
                string s;
                s = Pub_Class.ClsPub.isStrNull(grdCondition.Rows[BRow].Cells[BracketLCol].Value);
                grdCondition.Rows[BRow].Cells[BracketLCol].Value = s.Substring(0, s.Length - 1);
                s = "";
                s = Pub_Class.ClsPub.isStrNull(grdCondition.Rows[ERow].Cells[BracketRCol].Value);
                grdCondition.Rows[ERow].Cells[BracketRCol].Value = s.Substring(0, s.Length - 1);
            }
            else
            {
                if (Pub_Class.ClsPub.isStrNull(grdCondition.Rows[BRow].Cells[BracketLCol].Value) == "")
                {
                    MessageBox.Show("选择的第一行没有括号!", "提示");
                }
                else
                {
                    MessageBox.Show("选择的最后一行没有括号!", "提示");
                }
            }
        }
        //复制
        private void cmdCopy_Click(object sender, EventArgs e)
        {
            if (grdCondition.CurrentRow != null)
            {
                CopyField = Pub_Class.ClsPub.isStrNull(grdCondition.Rows[grdCondition.CurrentRow.Index].Cells[FieldNameCol].Value);
                CopyDataType = Pub_Class.ClsPub.isStrNull(grdCondition.Rows[grdCondition.CurrentRow.Index].Cells[IDCol].Value);
                cmdPaste.Enabled = true;
            }
        }
        //黏贴
        private void cmdPaste_Click(object sender, EventArgs e)
        {
            grdCondition.Rows.Insert(grdCondition.CurrentRow.Index, 1);
            //
            grdCondition.Rows[grdCondition.CurrentRow.Index - 1].Cells[IDCol].Value = CopyDataType;
            grdCondition.Rows[grdCondition.CurrentRow.Index - 1].Cells[FieldNameCol].Value = CopyField;
            //
            DataGridViewComboBoxCell sCellRel = new DataGridViewComboBoxCell();
            sCellRel.FlatStyle = FlatStyle.System;
            sCellRel.DisplayStyle = DataGridViewComboBoxDisplayStyle.Nothing;
            sCellRel.DropDownWidth = 10;
            sCellRel.Items.Add("并且");
            sCellRel.Items.Add("或者");
            grdCondition.Rows[grdCondition.CurrentRow.Index - 1].Cells[RelationCol] = sCellRel;
            grdCondition.Rows[grdCondition.CurrentRow.Index - 1].Cells[RelationCol].Value = "并且";
            //
            DataGridViewComboBoxCell sCellCom = new DataGridViewComboBoxCell();
            sCellCom.FlatStyle = FlatStyle.System;
            sCellCom.DisplayStyle = DataGridViewComboBoxDisplayStyle.Nothing;
            sCellCom.DropDownWidth = 10;
            sCellCom.Items.Add(" ");
            sCellCom.Items.Add("=");
            sCellCom.Items.Add(">");
            sCellCom.Items.Add(">=");
            sCellCom.Items.Add("<");
            sCellCom.Items.Add("<=");
            sCellCom.Items.Add("<>");
            sCellCom.Items.Add("包含");
            sCellCom.Items.Add("左包含");
            sCellCom.Items.Add("右包含");
            grdCondition.Rows[grdCondition.CurrentRow.Index - 1].Cells[ControlCol] = sCellCom;
            //
        }
        //删除
        private void cmdDelete_Click(object sender, EventArgs e)
        {
            if (grdCondition.CurrentRow != null)
            {
                //if (ClsPub.isStrNull(grdCondition.get_TextMatrix(grdCondition.Row, BracketLCol)) != ""
                //    || ClsPub.isStrNull(grdCondition.get_TextMatrix(grdCondition.Row, BracketRCol)) != "")
                //{
                //    MessageBox.Show("被删除行不能有括号!", "提示");
                //    return;
                //}
                grdCondition.Rows.RemoveAt(grdCondition.CurrentRow.Index);
            }
        }
        //恢复
        private void cmdRestore_Click(object sender, EventArgs e)
        {
            SetViewName(ViewName,true);
        }
 
 
         
 
        public virtual void Sub_GridKeyView(int sKeyCode, int sRow, int sCol, DataGridView grdCondition, DataGridViewTextBoxEditingControl oEdit)
        {
            //
        }
 
        DataGridViewTextBoxEditingControl EditingControl;
        private void grdCondition_CellBeginEdit(object sender, DataGridViewCellCancelEventArgs e)
        {
            if (grdCondition.CurrentCell.ColumnIndex != ControlCol && 
                grdCondition.CurrentCell.ColumnIndex != ConditionCol &&
                grdCondition.CurrentCell.ColumnIndex != RelationCol )
            {
                e.Cancel = true;
            }
            else
            {
                //
            }
        }
 
        private void EditingControl_KeyDown(object sender, KeyEventArgs e)
        {
            //业务处理 
            Sub_GridKeyView(e.KeyValue, grdCondition.CurrentRow.Index, grdCondition.CurrentCell.ColumnIndex, grdCondition, EditingControl);
        }
 
         
        private void grdCondition_EditingControlShowing(object sender, DataGridViewEditingControlShowingEventArgs e)
        {
            if (grdCondition.CurrentCell != null)
            {
                if (e.Control is DataGridViewTextBoxEditingControl)
                {
                    this.EditingControl = (DataGridViewTextBoxEditingControl)e.Control;
                    //增加委托处理 
                    this.EditingControl.KeyDown += new KeyEventHandler(this.EditingControl_KeyDown);
                }
 
            }
        }
 
        private void grdCondition_CellEndEdit(object sender, DataGridViewCellEventArgs e)
        {
            if (this.EditingControl != null)
            {
                EditingControl.KeyDown -= new KeyEventHandler(this.EditingControl_KeyDown);
                this.EditingControl = null;
            }
        }
 
        private void grdCondition_KeyDown(object sender, KeyEventArgs e)
        {
            Sub_GridKeyView(e.KeyValue, grdCondition.CurrentRow.Index, grdCondition.CurrentCell.ColumnIndex, grdCondition, EditingControl);
        }
    }
}