lixiaojun
2025-01-25 1a2bcaa7bec4f0bc681e55d1ccc61b14427c98ce
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
namespace Yw.WinFrmUI
{
    /// <summary>
    /// 
    /// </summary>
    public partial class SetParasGridCtrl : DevExpress.XtraEditors.XtraUserControl
    {
        /// <summary>
        /// 
        /// </summary>
        public SetParasGridCtrl()
        {
            InitializeComponent();
            this.gridView1.SetBindingLimitEditView();
        }
 
        private BindingList<ParasItemViewModel> _allBindingList = null;
 
        /// <summary>
        /// 绑定数据
        /// </summary>
        public void SetBindingData(Dictionary<string, string> dict)
        {
            _allBindingList = new BindingList<ParasItemViewModel>();
            if (dict != null && dict.Count > 0)
            {
                foreach (var item in dict)
                {
                    _allBindingList.Add(new ParasItemViewModel()
                    {
                        PropName = item.Key,
                        PropValue = item.Value
                    });
                }
            }
            this.parasItemViewModelBindingSource1.DataSource = _allBindingList;
            this.parasItemViewModelBindingSource1.ResetBindings(false);
        }
 
        /// <summary>
        /// 获取参数
        /// </summary>
        public Dictionary<string, string> GetParas()
        {
            var dict = new Dictionary<string, string>();
            if (_allBindingList != null && _allBindingList.Count > 0)
            {
                foreach (var item in _allBindingList)
                {
                    if (!string.IsNullOrEmpty(item.PropName))
                    {
                        if (!dict.ContainsKey(item.PropName))
                        {
                            dict.Add(item.PropName, item.PropValue);
                        }
                    }
                }
            }
            return dict;
        }
 
        //自定义下拉框
        private void gridView1_CustomRowCellEdit(object sender, DevExpress.XtraGrid.Views.Grid.CustomRowCellEditEventArgs e)
        {
            if (e.Column == this.colPropName)
            {
 
            }
        }
 
        //初始化
        private void gridView1_InitNewRow(object sender, DevExpress.XtraGrid.Views.Grid.InitNewRowEventArgs e)
        {
            if (_allBindingList == null)
            {
                return;
            }
            var row = this.gridView1.GetRow(e.RowHandle) as ParasItemViewModel;
            if (row != null)
            {
                row.PropValue = string.Empty;
            }
        }
 
        //编辑框显示
        private void gridView1_ShowingEditor(object sender, CancelEventArgs e)
        {
            if (_allBindingList == null)
            {
                e.Cancel = true;
                return;
            }
            var row = this.gridView1.GetFocusedRow() as ParasItemViewModel;
            if (row == null)
            {
                var col = this.gridView1.FocusedColumn;
                if (col != this.colPropName)
                {
                    e.Cancel = true;
                    return;
                }
            }
        }
 
        //删除
        private void gridView1_RowCellClick(object sender, DevExpress.XtraGrid.Views.Grid.RowCellClickEventArgs e)
        {
            if (e.Column == this.colDelete)
            {
                var row = this.gridView1.GetRow(e.RowHandle) as ParasItemViewModel;
                if (row != null)
                {
                    _allBindingList?.Remove(row);
                }
            }
        }
 
        //验证
        private void gridView1_ValidatingEditor(object sender, DevExpress.XtraEditors.Controls.BaseContainerValidateEditorEventArgs e)
        {
            if (_allBindingList == null)
            {
                return;
            }
            var row = this.gridView1.GetFocusedRow() as ParasItemViewModel;
            if (row != null)
            {
                var col = this.gridView1.FocusedColumn;
                if (col == this.colPropName)
                {
                    if (e.Value != null)
                    {
                        var propName = e.Value.ToString();
                        var count = _allBindingList.Count(x => x.PropName == propName && x != row);
                        if (count > 0)
                        {
                            e.Valid = false;
                            e.ErrorText = "重复";
                        }
                    }
                }
            }
 
        }
 
 
    }
}