1
yhj
2024-07-24 5e5d945e91568b973faa27d8ab0bcef99fc4a6c5
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
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
#region
 
using System;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Globalization;
using System.Reflection;
using System.Windows.Forms;
using CSFramework.DB;
using CSFrameworkV5.Common;
using CSFrameworkV5.Core;
using DevExpress.XtraEditors;
using DevExpress.XtraEditors.Controls;
using DevExpress.XtraEditors.Repository;
using DevExpress.XtraGrid;
using DevExpress.XtraGrid.Views.Grid;
using GSBase.BaseFrom;
using GSBase.Control;
 
#endregion
 
namespace CSFrameworkV5.Library.CommonClass
{
    /// <summary>
    ///     用于绑定输入控件的数据源
    /// </summary>
    public class DataBinder
    {
        public static DatabaseMSSQL mSSQL;
 
        /// <summary>
        ///     插入一条空记录到表的第一行记录。
        /// </summary>
        /// <param name="dt"></param>
        public static void AddEmptyRow(DataTable dt)
        {
            dt.Rows.InsertAt(dt.NewRow(), 0);
        }
 
        /// <summary>
        ///     绑定CheckedComboBoxEdit的数据源
        /// </summary>
        /// <param name="edit"></param>
        /// <param name="dataSource"></param>
        /// <param name="displayMember"></param>
        /// <param name="valueMember"></param>
        public static void BindingCheckedComboBoxSource(
            CheckedComboBoxEdit edit, DataTable dataSource,
            string displayMember, string valueMember)
        {
            edit.Properties.DisplayMember = displayMember;
            edit.Properties.ValueMember = valueMember;
            edit.Properties.DataSource = dataSource;
        }
 
        /// <summary>
        ///     绑定CheckEdit的数据源
        /// </summary>
        /// <param name="edit">CheckEdit</param>
        /// <param name="dataSource">数据源</param>
        /// <param name="bindField">取值字段</param>
        public static void BindingCheckEdit(CheckEdit edit, object dataSource,
            string bindField)
        {
            try
            {
                edit.DataBindings.Clear();
                var b = new Binding("EditValue", dataSource, bindField);
                b.NullValue = "N";
                edit.DataBindings.Add(b);
            }
            catch (Exception ex)
            {
                Msg.ShowException(ex);
            }
        }
 
        /// <summary>
        ///     绑定CheckedListBox的数据源
        /// </summary>
        /// <param name="control">CheckedListBox</param>
        /// <param name="dataSource">数据源</param>
        /// <param name="bindField">取值字段</param>
        public static void BindingCheckedListBox(Control control,
            object dataSource, string bindField)
        {
            try
            {
                control.DataBindings.Clear();
                var b = new Binding("EditValue", dataSource, bindField);
                control.DataBindings.Add(b);
            }
            catch (Exception ex)
            {
                Msg.ShowException(ex);
            }
        }
 
        /// <summary>
        ///     绑定CheckedListBoxControl的数据源
        /// </summary>
        /// <param name="edit"></param>
        /// <param name="dataSource"></param>
        /// <param name="displayMember"></param>
        /// <param name="valueMember"></param>
        public static void BindingCheckedListBoxSource(
            CheckedListBoxControl edit, DataTable dataSource,
            string displayMember, string valueMember)
        {
            edit.DisplayMember = displayMember;
            edit.ValueMember = valueMember;
            edit.DataSource = dataSource;
        }
 
        /// <summary>
        ///     绑定ComboBoxEdit的数据源
        /// </summary>
        /// <param name="edit">ComboBoxEdit</param>
        /// <param name="dataSource">数据源</param>
        /// <param name="bindField">取值字段</param>
        public static void BindingComboEdit(ComboBoxEdit edit,
            object dataSource, string bindField)
        {
            try
            {
                edit.DataBindings.Clear();
                var b = new Binding("EditValue", dataSource, bindField);
                edit.DataBindings.Add(b);
            }
            catch (Exception ex)
            {
                Msg.ShowException(ex);
            }
        }
 
        /// <summary>
        ///     绑定ComboBoxEdit的数据源
        /// </summary>
        /// <param name="edit">ComboBoxEdit</param>
        /// <param name="dataSource">数据源</param>
        /// <param name="bindField">取值字段</param>
        public static void BindingComboEditDataSource(ComboBoxEdit edit,
            DataTable dataSource, string bindField)
        {
            try
            {
                edit.Properties.Items.Clear();
                foreach (DataRow dr in dataSource.Rows)
                    edit.Properties.Items.Add(dr[bindField]);
            }
            catch (Exception ex)
            {
                Msg.ShowException(ex);
            }
        }
 
        /// <summary>
        ///     绑定ComboBoxEdit的数据源
        /// </summary>
        /// <param name="edit">ComboBoxEdit</param>
        /// <param name="dataSource">数据源</param>
        /// <param name="bindField">取值字段</param>
        public static void BindingComboEditDataSource(
            RepositoryItemComboBox edit, DataTable dataSource,
            string bindField)
        {
            try
            {
                edit.Items.Clear();
                foreach (DataRow dr in dataSource.Rows)
                    edit.Items.Add(dr[bindField]);
            }
            catch (Exception ex)
            {
                Msg.ShowException(ex);
            }
        }
 
        /// <summary>
        ///     绑定输入控件的数据源
        /// </summary>
        /// <param name="ctl">支持输入功能的控件</param>
        /// <param name="dataSource">数据源</param>
        /// <param name="bindField">取值字段</param>
        /// <param name="propertyName">控件的取值属性</param>
        public static void BindingControl(Control ctl, object dataSource,
            string bindField, string propertyName)
        {
            try
            {
                ctl.DataBindings.Clear();
                var b = new Binding(propertyName, dataSource, bindField);
                ctl.DataBindings.Add(b);
            }
            catch (Exception ex)
            {
                Msg.ShowException(ex);
            }
        }
 
        /// <summary>
        ///     绑定日期选择控件(DateEdit)的EditValueChanged事件.
        ///     原因请叁考OnDateEditValueChange方法描述.
        /// </summary>
        public static void BindingDateEditValueChangeEvent(DateEdit dateEdit)
        {
            dateEdit.EditValueChanged += OnDateEditValueChange;
        }
 
        /// <summary>
        ///     自动绑定编辑区域的数据源。规则:txt+字段名,或者chk+字段名,其它类型可以扩展
        /// </summary>
        /// <param name="editorPanel">Panel容器</param>
        /// <param name="dataSource">数据源</param>
        public static void BindingEditorPanel(Control editorPanel,
            DataTable dataSource)
        {
            var fieldName = "";
 
            try
            {
                for (var i = 0; i <= editorPanel.Controls.Count - 1; i++)
                {
                    if (editorPanel.Controls[i] is BaseEdit)
                    {
                        var edit = editorPanel.Controls[i] as BaseEdit;
                        if (edit.Name.Substring(0, 3) == "txt")
                        {
                            fieldName =
                                edit.Name.Substring(3, edit.Name.Length - 3);
                            BindingTextEditBase(edit, dataSource, fieldName);
                        }
                    }
 
                    if (editorPanel.Controls[i] is CheckEdit)
                    {
                        var edit = editorPanel.Controls[i] as CheckEdit;
                        if (edit.Name.Substring(0, 3) == "chk")
                        {
                            fieldName =
                                edit.Name.Substring(3, edit.Name.Length - 3);
                            BindingCheckEdit(edit, dataSource, fieldName);
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                LogUserOperate.Write(ex);
                Msg.Warning("字段:" + fieldName + "\r\n" + ex.Message);
            }
        }
 
        /// <summary>
        ///     自动绑定编辑区域的数据源。重载,增加子表项 2022-5-1 liujiao
        /// </summary>
        /// <param name="editorPanel">Panel容器</param>
        /// <param name="dataSource">数据源</param>
        public static void BindingEditorPanel(Control editorPanel,
            DataTable dataSource, string ChilddataName)
        {
            var fieldName = "";
 
            try
            {
                for (var i = 0; i <= editorPanel.Controls.Count - 1; i++)
                {
                    if (editorPanel.Controls[i] is BaseEdit)
                    {
                        var edit = editorPanel.Controls[i] as BaseEdit;
                        if (edit.Name.Substring(0, 3) == "txt")
                        {
                            fieldName =
                                edit.Name.Substring(3, edit.Name.Length - 3);
                            BindingTextEditBase(edit, dataSource, fieldName);
                        }
                    }
 
                    if (editorPanel.Controls[i] is GridControl)
                    {
                        DataTable Childdata = null;
                        var CdataColumnsName =
                            ChilddataName.Substring(ChilddataName.Length - 3) +
                            "001"; //子表主建
                        var edit = editorPanel.Controls[i] as GridControl;
                        foreach (DataRow dr in dataSource.Rows)
                            for (var j = 0; j < dataSource.Columns.Count; j++)
                            {
                                var str = dataSource.Columns[j].ColumnName;
                                if (str.Substring(str.Length - 3) == "001")
                                {
                                    var con =
                                        " Data Source=.;Initial Catalog=CSFrameworkV5_Normal;User ID=sa;Password =563593659liu;Persist Security Info=True;Connect Timeout=15;;Connection TimeOut=15;";
                                    mSSQL = new DatabaseMSSQL(con);
                                    var sql = string.Format(
                                        @" SELECT * FROM {0} WHERE {1}='{2}' ",
                                        ChilddataName, CdataColumnsName, dr[j]);
                                    Childdata =
                                        mSSQL.GetTable(sql, ChilddataName);
                                }
                            }
 
                        edit.DataSource = Childdata;
                    }
 
                    if (editorPanel.Controls[i] is AdvButtonText)
                    {
                        var ButtonText =
                            editorPanel.Controls[i] as AdvButtonText;
 
                        ButtonText.lbCaption.Text =
                            ButtonText.CaptionT; //"到货单号";
                        //ButtonText.ButtonClick += ButtonText_ButtonClick;
                        //ButtonText.TextValidated += ButtonText_TextValidated;
                        //ButtonText.textEdit.TextChanged -= new EventHandler(textEdit_TextChanged);
                        //ButtonText.textEdit.TextChanged += new EventHandler(textEdit_TextChanged);
                    }
 
                    if (editorPanel.Controls[i] is CheckEdit)
                    {
                        var edit = editorPanel.Controls[i] as CheckEdit;
                        if (edit.Name.Substring(0, 3) == "chk")
                        {
                            fieldName =
                                edit.Name.Substring(3, edit.Name.Length - 3);
                            BindingCheckEdit(edit, dataSource, fieldName);
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                LogUserOperate.Write(ex);
                Msg.Warning("字段:" + fieldName + "\r\n" + ex.Message);
            }
        }
 
        /// <summary>
        ///     绑定图像控件的数据源
        /// </summary>
        /// <param name="edit">PictureEdit</param>
        /// <param name="dataSource">数据源</param>
        /// <param name="bindField">取值字段</param>
        public static void BindingImageEdit(PictureEdit edit, object dataSource,
            string bindField)
        {
            try
            {
                edit.DataBindings.Clear();
                var b = new Binding("EditValue", dataSource, bindField);
                //b.Parse += new ConvertEventHandler(ParseImageToByteArray);
                //b.Format += new ConvertEventHandler(FormatByteArrayToImage);
                edit.DataBindings.Add(b);
            }
            catch (Exception ex)
            {
                Msg.ShowException(ex);
            }
        }
 
        /// <summary>
        ///     绑定参照字段的数据源
        /// </summary>
        /// <param name="edit">参照字段输入控件</param>
        /// <param name="dataSource">数据源</param>
        /// <param name="displayMember">显示字段</param>
        /// <param name="valueMember">取值字段</param>
        public static void BindingLookupEditDataSource(LookUpEdit edit,
            object dataSource, string displayMember,
            string valueMember)
        {
            BindingLookupEditDataSource(edit.Properties, dataSource,
                displayMember, valueMember);
        }
 
        /// <summary>
        ///     绑定表格内列参照字段的数据源
        /// </summary>
        /// <param name="edit">参照字段控件</param>
        /// <param name="dataSource">数据源</param>
        /// <param name="displayMember">显示字段</param>
        /// <param name="valueMember">取值字段</param>
        public static void BindingLookupEditDataSource(
            RepositoryItemLookUpEdit edit, object dataSource,
            string displayMember, string valueMember)
        {
            edit.DisplayMember = displayMember;
            edit.ValueMember = valueMember;
            edit.DataSource = dataSource;
        }
 
        /// <summary>
        ///     绑定RadioGroup组控件的数据源
        /// </summary>
        /// <param name="edit">RadioGroup组控件</param>
        /// <param name="dataSource">数据源</param>
        /// <param name="bindField">取值字段</param>
        public static void BindingRadioEdit(RadioGroup edit, object dataSource,
            string bindField)
        {
            try
            {
                edit.DataBindings.Clear();
 
                var b = new Binding("EditValue", dataSource, bindField);
                edit.DataBindings.Add(b);
            }
            catch (Exception ex)
            {
                Msg.ShowException(ex);
            }
        }
 
        /// <summary>
        ///     绑定RadioGroup组控件的数据源
        /// </summary>
        /// <param name="edit">RadioGroup组控件</param>
        /// <param name="dataSource">数据源</param>
        /// <param name="bindField">取值字段</param>
        public static void BindingRadioEdit(RadioGroup edit,
            DataTable dataSource, string displayMember,
            string valueMember)
        {
            foreach (DataRow dr in dataSource.Rows)
            {
                var strName = ConvertEx.ToString(dr[displayMember]); //获取名称
                var strVaule = ConvertEx.ToString(dr[valueMember]); //获取值
                var rgItem = new RadioGroupItem();
                rgItem.Description = strName;
                rgItem.Value = strVaule;
                edit.Properties.Items.Add(rgItem);
            }
        }
 
        /// <summary>
        ///     绑定输入控件的数据源
        /// </summary>
        /// <param name="edit">控件框</param>
        /// <param name="dataSource">数据源</param>
        /// <param name="bindField">取值字段</param>
        public static void BindingTextEdit(TextEdit edit, object dataSource,
            string bindField)
        {
            try
            {
                edit.DataBindings.Clear();
                var b = new Binding("EditValue", dataSource, bindField);
                edit.DataBindings.Add(b);
            }
            catch (Exception ex)
            {
                Msg.ShowException(ex);
            }
        }
 
        /// <summary>
        ///     绑定输入控件的数据源
        /// </summary>
        /// <param name="edit"></param>
        /// <param name="dataSource"></param>
        /// <param name="bindField"></param>
        /// <param name="rowPosition"></param>
        public static void BindingTextEdit(TextEdit edit, object dataSource,
            string bindField, int rowPosition)
        {
            try
            {
                edit.DataBindings.Clear();
                var b = new Binding("EditValue", dataSource, bindField);
                edit.DataBindings.Add(b);
 
                //指定资料行序号
                if (rowPosition >= 0)
                    edit.BindingContext[dataSource].Position = rowPosition;
            }
            catch (Exception ex)
            {
                Msg.ShowException(ex);
            }
        }
 
        /// <summary>
        ///     绑定金额输入控件的数据源
        /// </summary>
        /// <param name="edit">PictureEdit</param>
        /// <param name="dataSource">数据源</param>
        /// <param name="bindField">取值字段</param>
        public static void BindingTextEditAmount(TextEdit edit,
            object dataSource, string bindField)
        {
            try
            {
                edit.DataBindings.Clear();
                var b = new Binding("EditValue", dataSource, bindField);
                b.NullValue = null;
                b.Parse += CurrencyStringToDecimal;
                b.Format += DecimalToCurrencyString;
                edit.DataBindings.Add(b);
            }
            catch (Exception ex)
            {
                Msg.ShowException(ex);
            }
        }
 
        /// <summary>
        ///     绑定输入控件的数据源
        /// </summary>
        /// <param name="edit">控件框</param>
        /// <param name="dataSource">数据源</param>
        /// <param name="bindField">取值字段</param>
        public static void BindingTextEditBase(BaseEdit edit, object dataSource,
            string bindField)
        {
            try
            {
                edit.DataBindings.Clear();
                var b = new Binding("EditValue", dataSource, bindField);
                edit.DataBindings.Add(b);
                b.ReadValue();
            }
            catch (Exception ex)
            {
                Msg.ShowException(ex);
            }
        }
 
        /// <summary>
        ///     绑定日期输入控件的数据源
        /// </summary>
        /// <param name="edit">日期输入控件TimeEdit</param>
        /// <param name="dataSource">数据源</param>
        /// <param name="bindField">取值字段</param>
        public static void BindingTextEditDateTime(TimeEdit edit,
            object dataSource, string bindField)
        {
            try
            {
                edit.DataBindings.Clear();
                //不能绑定日期控件的DateTime属性. 只能为EditValue!
                var b = new Binding("EditValue", dataSource, bindField);
                b.NullValue = null;
                b.Parse += DateStringToDate;
                b.Format += DateToDateString;
                edit.DataBindings.Add(b);
            }
            catch (Exception ex)
            {
                Msg.ShowException(ex);
            }
        }
 
        /// <summary>
        ///     绑定日期输入控件的数据源
        /// </summary>
        /// <param name="edit">日期输入控件DateEdit</param>
        /// <param name="dataSource">数据源</param>
        /// <param name="bindField">取值字段</param>
        public static void BindingTextEditDateTime(DateEdit edit,
            object dataSource, string bindField)
        {
            try
            {
                edit.DataBindings.Clear();
                //不能绑定日期控件的DateTime属性. 只能为EditValue!
                var b = new Binding("EditValue", dataSource, bindField);
                b.NullValue = null;
                b.Parse += DateStringToDate;
                b.Format += DateToDateString;
                edit.DataBindings.Add(b);
            }
            catch (Exception ex)
            {
                Msg.ShowException(ex);
            }
        }
 
        /// <summary>
        ///     将INT类型转换为Boolean
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="cevent"></param>
        public static void BoolBitToBool(object sender, ConvertEventArgs cevent)
        {
            try
            {
                if (cevent.DesiredType != typeof(int)) return;
 
                cevent.Value = bool.Parse(cevent.Value.ToStringEx());
            }
            catch (Exception ex)
            {
                ShowError("数据转换错误!/n" + ex.Message, "错误");
            }
        }
 
        /// <summary>
        ///     将Bool转换为INT
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="cevent"></param>
        public static void BoolToBoolBit(object sender, ConvertEventArgs cevent)
        {
            try
            {
                if (cevent.DesiredType != typeof(bool)) return;
 
                if (cevent.Value.ToStringEx() == string.Empty)
                    cevent.Value = false;
 
                cevent.Value = (bool)cevent.Value;
            }
            catch (Exception ex)
            {
                ShowError("数据转换错误!/n" + ex.Message, "错误");
            }
        }
 
        /// <summary>
        ///     将字符串转换为数字
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="cevent"></param>
        public static void CurrencyStringToDecimal(object sender,
            ConvertEventArgs cevent)
        {
            try
            {
                if (cevent.DesiredType != typeof(decimal)) return;
 
                if (string.Empty == cevent.Value.ToStringEx())
                    cevent.Value = 0;
                else
                    cevent.Value = decimal.Parse(cevent.Value.ToStringEx(),
                        NumberStyles.Currency, null);
            }
            catch (Exception ex)
            {
                ShowError("数据转换错误!/n" + ex.Message, "错误");
            }
        }
 
        /// <summary>
        ///     日期字符串转换为日期类型
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="cevent"></param>
        public static void DateStringToDate(object sender,
            ConvertEventArgs cevent)
        {
            try
            {
                var type = cevent.DesiredType;
 
                //对泛型数据特殊处理.
                if (cevent.DesiredType.IsGenericType)
                    type = cevent.DesiredType.GetGenericArguments()[0];
 
                if (cevent.Value == null || type != typeof(DateTime) ||
                    cevent.Value.ToStringEx() == string.Empty)
                    cevent.Value = null;
                else
                    cevent.Value = DateTime.Parse(cevent.Value.ToStringEx(),
                        DateTimeFormatInfo.CurrentInfo);
            }
            catch (Exception ex)
            {
                ShowError("数据转换错误!/n" + ex.Message, "错误");
            }
        }
 
        /// <summary>
        ///     将日期类型转换为带格式的日期字符串
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="cevent"></param>
        public static void DateToDateString(object sender,
            ConvertEventArgs cevent)
        {
            try
            {
                if (cevent.Value == null ||
                    cevent.DesiredType != typeof(string) ||
                    cevent.Value.ToStringEx() == string.Empty)
                    cevent.Value = null;
                else
                    cevent.Value =
                        ((DateTime)cevent.Value).ToString(
                            Globals.DEF_DATE_FORMAT,
                            DateTimeFormatInfo.CurrentInfo);
            }
            catch (Exception ex)
            {
                ShowError("数据转换错误!/n" + ex.Message, "错误");
            }
        }
 
        /// <summary>
        ///     将数字类型转换为带格式的字符串
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="cevent"></param>
        public static void DecimalToCurrencyString(object sender,
            ConvertEventArgs cevent)
        {
            try
            {
                if (cevent != null && cevent.Value != null &&
                    cevent.DesiredType == typeof(string))
                    cevent.Value =
                        ((decimal)cevent.Value).ToString("n"); //10,446.56
            }
            catch (Exception ex)
            {
                ShowError("数据转换错误!/n" + ex.Message, "错误");
            }
        }
 
        /// <summary>
        ///     Byte[] to Image
        /// </summary>
        public static void FormatByteArrayToImage(object sender,
            ConvertEventArgs e)
        {
            try
            {
                if (e.Value != null)
                {
                    var img =
                        (Image)ZipTools.DecompressionObject((byte[])e.Value);
                    e.Value = img;
                }
                else
                {
                    e.Value = null;
                }
            }
            catch
            {
                e.Value = null;
            }
        }
 
        private void GetMapp(string v1, string v2, string val)
        {
            SetValueControlProperty(this, v1, v2, val);
        }
 
        public static void GetSelectT(string sql, string filt)
        {
            DataRow datarow = null;
            try
            {
                var SelectItemDlg = new SelectQueryDlg(sql, filt);
                if (SelectItemDlg.ShowDialog() == DialogResult.OK)
                    datarow = SelectItemDlg.ReturnRow == null
                        ? null
                        : SelectItemDlg.ReturnRow;
                else
                    datarow = null;
                //SelectItemDlg.ShowDialog();
                //datarow = Search.GetQueryRow(ButtonText.DatacatalogID, SqlFilterHelper(ButtonText.Filter));
            }
            catch (Exception ex)
            {
                //WriteLog(Format("调用出现错误:{0}", GetErrorMsg(ex)), true);
                //return;
            }
        }
 
        private static string[] GetSplitString(string strValue)
        {
            if (strValue == null) return null;
 
            if (strValue.Length == 0) return null;
 
            strValue = strValue.Replace("[", "").Replace("]", "");
            var strArray = strValue.Split(';', '=');
            return strArray;
        }
 
        /// <summary>
        ///     日期控件(DateEdit)的EditValueChanged事件.
        ///     对象内定义为Nullable
        ///     <DateTime>
        ///         数据类型的属性,进行数据绑定后不能将值
        ///         保存在对象内,所以实现这个方法做特殊处理
        /// </summary>
        public static void OnDateEditValueChange(object sender, EventArgs e)
        {
            try
            {
                var edit = (DateEdit)sender;
                if (edit.DataBindings.Count <= 0) return; //无绑定数据源.
 
                var bindingObj = edit.DataBindings[0].DataSource; //取绑定的对象.
                if (bindingObj != null)
                {
                    var bindingField = edit.DataBindings[0].BindingMemberInfo
                        .BindingField; //取绑定的成员字段.                
                    DataConverter.SetValueOfObject(bindingObj, bindingField,
                        edit.EditValue); //给对象的字段赋值
                }
            }
            catch (Exception ex)
            {
                Msg.ShowException(ex);
            }
        }
 
        /// <summary>
        ///     Image to byte[]
        /// </summary>
        public static void ParseImageToByteArray(object sender,
            ConvertEventArgs e)
        {
            try
            {
                if (e.Value != null)
                    e.Value = ZipTools.CompressionObject(e.Value as Image);
                else
                    e.Value = DBNull.Value;
            }
            catch
            {
                e.Value = DBNull.Value;
            }
        }
 
        /// <summary>
        ///     设置容器内所有可输入控件的状态.ReadOnly or Enable = false/true
        /// </summary>
        /// <param name="container">容器</param>
        /// <param name="value">false/true</param>
        public static void SetControlAccessable(Control container, bool value)
        {
            if (container is Label) return;
 
            if (container is LabelControl) return;
 
            if (container is UserControl) return;
 
            if (container.HasChildren == false) return; //没有子控件,不处理
 
            foreach (Control c in container.Controls)
                //最常用组件,首先处理
                if (c is BaseEdit) //输入框
                    (c as BaseEdit).Properties.ReadOnly = !value;
                else if (c is GridControl) //表格
                    ((c as GridControl).Views[0] as GridView).OptionsBehavior
                        .Editable = value;
                else
                    SetControlAccessableByProp(container,
                        value); //其它组件,反射属性设置状态
        }
 
        /// <summary>
        ///     设置一个控件的可用状态,通过反射ReadOnly,Properties属性
        /// </summary>
        /// <param name="control">控件</param>
        /// <param name="value">值</param>
        public static void SetControlAccessableByProp(Control control,
            bool value)
        {
            var type = control.GetType();
            var infos = type.GetProperties();
            foreach (var info in infos)
            {
                if (info.Name == "ReadOnly") //Properties.ReadOnly
                {
                    info.SetValue(control, !value, null);
                    return;
                }
 
                if (info.Name == "Properties")
                {
                    var o = info.GetValue(control, null);
 
                    //处理特殊组件
                    if (o is RepositoryItemButtonEdit &&
                        (o as RepositoryItemButtonEdit).Buttons.Count >
                        0) //ButtonEdit
                        (o as RepositoryItemButtonEdit).Buttons[0].Enabled =
                            value;
                    else if (o is RepositoryItemDateEdit &&
                             (o as RepositoryItemDateEdit).Buttons.Count >
                             0) //DateEdit
                        (o as RepositoryItemDateEdit).Buttons[0].Enabled =
                            value;
 
                    if (o is RepositoryItem)
                        (o as RepositoryItem).ReadOnly = !value;
 
                    return;
                }
            }
        }
 
        /// <summary>
        ///     设置容器内所有可输入控件的状态 .ReadOnly or Enable . (递归)循环控制
        /// </summary>
        /// <param name="container">容器</param>
        /// <param name="value">false/true</param>
        public static void SetControlAccessableCycle(Control container,
            bool value)
        {
            if (container.HasChildren)
                foreach (Control ctrl in container.Controls)
                    //DevExpress的内部(Inner)控件
                    if (ctrl.Name == string.Empty)
                        SetControlAccessable(container, value);
                    else
                        SetControlAccessableCycle(ctrl, value);
            else
                SetControlAccessable(container, value);
        }
 
        /// <summary>
        ///     设置容器内的控件可用状态, Control.Enable = false/true
        /// </summary>
        /// <param name="container">容器</param>
        /// <param name="value">false/true</param>
        public static void SetControlEnable(Control container, bool value)
        {
            if (container is Label) return;
 
            if (container is LabelControl) return;
 
            if (container.Name == "") return;
 
            if (container.Controls.Count > 0)
                foreach (Control c in container.Controls)
                {
                    c.Enabled = value;
                    SetControlEnable(c, value);
                }
        }
 
        /// <summary>
        ///     给绑定数据源的输入控件赋值
        /// </summary>
        public static void SetEditorBindingValue(Control bindingControl,
            object value)
        {
            SetEditorBindingValue(bindingControl, value, false);
        }
 
        public static void SetEditorBindingValue(Control bindingControl,
            object value, bool setEditorValue)
        {
            try
            {
                object temp = null;
                if (value != DBNull.Value) temp = value;
 
                if (bindingControl.DataBindings.Count > 0)
                {
                    var dataSource = bindingControl.DataBindings[0].DataSource;
                    var field = bindingControl.DataBindings[0].BindingMemberInfo
                        .BindingField;
                    if (dataSource is DataTable)
                        (dataSource as DataTable).Rows[0][field] = value;
                    else
                        DataConverter.SetValueOfObject(dataSource, field,
                            value);
                }
 
                if (setEditorValue)
                    DataConverter.SetValueOfObject(bindingControl, "EditValue",
                        value);
 
                bindingControl.Refresh();
            }
            catch
            {
            } //这里不用显示异常信息. 
        }
 
        /// <summary>
        ///     设置输入组件只读及背景色
        /// </summary>
        /// <param name="editor">输入组件</param>
        /// <param name="enable">可写/只读</param>
        /// <param name="setBackgroundColor">是否设置背景色</param>
        public static void SetEditorEnable(TextEdit editor, bool enable,
            bool setBackgroundColor)
        {
            if (enable && setBackgroundColor) editor.BackColor = Color.White;
 
            if (!enable && setBackgroundColor)
                editor.BackColor = SystemColors.ButtonFace;
 
            editor.Properties.ReadOnly = !enable;
        }
 
        //private  void GetMapp()
        //{
        //    SetValueControlProperty(this, mapping[0], mapping[1], val);
        //}
 
        public static void SetValueControlProperty(object ClassInstance,
            string ControlName, string PropertyName,
            object Value)
        {
            var myType = ClassInstance.GetType();
 
            var myFieldInfo = myType.GetField(ControlName,
                BindingFlags.NonPublic
                | BindingFlags.Instance);
 
            if (myFieldInfo == null) return;
 
            var properties =
                TypeDescriptor.GetProperties(myFieldInfo.FieldType);
            var myProperty =
                properties.Find(PropertyName, false); //这里设为True就不用区分大小写了
 
            if (myProperty == null) return;
 
            object ctr;
            ctr = myFieldInfo.GetValue(ClassInstance); //取得控件实例
 
            try
            {
                myProperty.SetValue(ctr, Value.ToString());
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }
 
        /// <summary>
        ///     显示错误
        /// </summary>
        /// <param name="msg">消息</param>
        /// <param name="title">标题</param>
        private static void ShowError(string msg, string title)
        {
            MessageBox.Show(msg, title, MessageBoxButtons.OK,
                MessageBoxIcon.Hand, MessageBoxDefaultButton.Button1);
        }
 
        public static DataRow TTT(object sender, EventArgs e)
        {
            var ButtonText = sender as AdvButtonText;
            ButtonText.lbCaption.Text = ButtonText.CaptionT;
            //if (_UpdateType != UpdateType.Add)
            //{
            //    Msg.Warning("请先打开新增状态!");
            //    return;
            //}
            //点选前检测
            //if (ButtonTextBeforeClickCheck(ButtonText) == false)
            //    return;
            //判断控件的SearchEntity是否是有赋值;
            //if (ButtonText.SearchEntity == null)
            //{
            //    WriteLog(Format("此控件的查询实体为空"), true);
            //    return;
            //}
 
            ////判断控件的SearchEntity是否是EntityClass,若是则将其查询实体赋值
            //if (ButtonText.SearchEntity is EntityClass)
            //{
            //    Search.SelectEntity = (EntityClass)ButtonText.SearchEntity;
            //}
 
            //查询数据.SqlFilterHelper负责进行将条件转换为实际的条件
            DataRow datarow = null;
            try
            {
                var SelectItemDlg = new SelectQueryDlg(ButtonText.DatacatalogID,
                    ButtonText.Filter);
                if (SelectItemDlg.ShowDialog() == DialogResult.OK)
                    datarow = SelectItemDlg.ReturnRow == null
                        ? null
                        : SelectItemDlg.ReturnRow;
                else
                    datarow = null;
                //SelectItemDlg.ShowDialog();
                //datarow = Search.GetQueryRow(ButtonText.DatacatalogID, SqlFilterHelper(ButtonText.Filter));
            }
            catch (Exception ex)
            {
                //WriteLog(Format("调用出现错误:{0}", GetErrorMsg(ex)), true);
                //return;
            }
 
            //若行为空则返回.
            if (datarow == null)
                //WriteLog(Format("没有找到合适的{0}",ButtonText.Caption), true);
                return null;
 
            //对文本框进行赋值
            //if (ButtonText.TextField.Length == 0)
            //{
            //    WriteLog(Format("请完善控件文本对应的字段编号"), true);
            //    return;
            //}
 
            ////对文本框中的字段编号进行判断,是否在查询表中存在
            //if (!datarow.Table.Columns.Contains(ButtonText.TextField))
            //{
            //    WriteLog(Format("文本对应字段在查询集中无法找到此列"), true);
            //    return;
            //}
 
            //对查询出来结果进行赋值
            ButtonText.textEdit.EditValue = datarow[ButtonText.TextField];
            //ControlHelper.SetValueControlProperty(this, ButtonText.Name, "EditValue", datarow[ButtonText.TextField].ToString());
 
            //对备注字段进行赋值,若备注字段为空、文本模式为BeRemark,且数据表中存在字段才执行
            if (ButtonText.TextType == ShowStyle.BeRemark &&
                ButtonText.RemarkField.Length > 0 &&
                datarow.Table.Columns.Contains(ButtonText.RemarkField))
                ButtonText.lbRemark.Text =
                    datarow[ButtonText.RemarkField].ToString();
            //ControlHelper.SetValueControlProperty(this, ButtonText.Name, "Remark", datarow[ButtonText.RemarkField].ToString());
 
            #region 点选结果返回变更
 
            ButtonText.Tag = datarow;
            //ButtonText.FireButtonDataChanged(sender, e);
 
            #endregion
 
            //通过函数将其他映射关系转换.
            var strMapping = GetSplitString(ButtonText.OtherMapping);
 
            try
            {
                if (strMapping == null) return datarow;
 
                //对其他映射关系进行赋值
                for (var i = 0; i < strMapping.Length; i += 2)
                {
                    if (!strMapping[i].Contains(".")) continue;
 
                    var mapping = strMapping[i].Split('.');
 
                    //转译DataRow的值之后设定给控件属性。Table.Columns不包设定列,则表示为用户设定了固定值。
                    //注意,例如:如果为DateTime控件,而用户设定的值为非日期格式,系统同样会提示出设值错误。
                    var val = datarow.Table.Columns.Contains(strMapping[i + 1])
                        ? datarow[strMapping[i + 1]].ToString()
                        : strMapping[i + 1];
 
                    //GetMapp(mapping[0], mapping[1], val);
                }
            }
            finally
            {
                // GridCheckError = "";
                //ResultManger.Clear(ButtonText.Caption);
                //ogf modify by 2012-05-26这样处理的原因是离开这个控件,以便控件把值提交到Datarow中,并触发列变化事件。
                ButtonText.strOldText = ButtonText.textEdit.Text;
                ButtonText.textEdit.Focus();
                //this.SelectNextControl(this.ActiveControl, true, false, true, true);
                ButtonText.textEdit.Focus();
            }
 
            return datarow;
        }
 
        public static DataRow LIANS(string SQL, string Filter)
        {
            DataRow datarow = null;
            try
            {
                var SelectItemDlg = new SelectQueryDlg(SQL, Filter);
                if (SelectItemDlg.ShowDialog() == DialogResult.OK)
                    datarow = SelectItemDlg.ReturnRow == null
                        ? null
                        : SelectItemDlg.ReturnRow;
                else
                    datarow = null;
                //SelectItemDlg.ShowDialog();
                //datarow = Search.GetQueryRow(ButtonText.DatacatalogID, SqlFilterHelper(ButtonText.Filter));
            }
            catch (Exception ex)
            {
                //WriteLog(Format("调用出现错误:{0}", GetErrorMsg(ex)), true);
                //return;
            }
 
            //若行为空则返回.
            if (datarow == null)
                //WriteLog(Format("没有找到合适的{0}",ButtonText.Caption), true);
                return null;
 
 
            #region 点选结果返回变更
 
            #endregion
 
            return datarow;
        }
    }
}