ns = new DataGridViewColumnEntity[0];
129 LeftDataSource = new object[0];
130 RightDataSource = new object[0];
131 }
132
133 /// <summary>
134 /// Handles the BtnClick event of the btnRight control.
135 /// </summary>
136 /// <param name="sender">The source of the event.</param>
137 /// <param name="e">The <see cref="EventArgs"/> instance containing the event data.</param>
138 /// <exception cref="System.Exception">
139 /// 左右数据源列表不能为空
140 /// or
141 /// 左右数据源列表类型不一致,无法进行操作
142 /// </exception>
143 private void btnRight_BtnClick(object sender, EventArgs e)
144 {
145 if (LeftDataSource == null || RightDataSource == null)
146 {
147 throw new Exception("左右数据源列表不能为空");
148 }
149 if (LeftDataSource.GetType() != RightDataSource.GetType())
150 {
151 throw new Exception("左右数据源列表类型不一致,无法进行操作");
152 }
153 if (dgvLeft.SelectRows == null || dgvLeft.SelectRows.Count <= 0)
154 return;
155 List<object> lst = new List<object>();
156 dgvLeft.SelectRows.ForEach(p =>
157 {
158 lst.Add(p.DataSource);
159 p.IsChecked = false;
160 });
161 var lstRight = RightDataSource.ToList();
162 lstRight.AddRange(lst);
163 var lstLeft = LeftDataSource.ToList();
164 lstLeft.RemoveAll(p => lst.Contains(p));
165 RightDataSource = lstRight.ToArray();
166 LeftDataSource = lstLeft.ToArray();
167 if (Transfered != null)
168 {
169 Transfered(this, new TransferEventArgs() { TransferDataSource = lst.ToArray(), ToRightOrLeft = true });
170 }
171 }
172
173 /// <summary>
174 /// Handles the BtnClick event of the btnLeft control.
175 /// </summary>
176 /// <param name="sender">The source of the event.</param>
177 /// <param name="e">The <see cref="EventArgs"/> instance containing the event data.</param>
178 /// <exception cref="System.Exception">
179 /// 左右数据源列表不能为空
180 /// or
181 /// 左右数据源列表类型不一致,无法进行操作
182 /// </exception>
183 private void btnLeft_BtnClick(object sender, EventArgs e)
184 {
185 if (LeftDataSource == null || RightDataSource == null)
186 {
187 throw new Exception("左右数据源列表不能为空");
188 }
189 if (LeftDataSource.GetType() != RightDataSource.GetType())
190 {
191 throw new Exception("左右数据源列表类型不一致,无法进行操作");
192 }
193 if (dgvRight.SelectRows == null || dgvRight.SelectRows.Count <= 0)
194 return;
195 List<object> lst = new List<object>();
196 dgvRight.SelectRows.ForEach(p =>
197 {
198 lst.Add(p.DataSource);
199 p.IsChecked = false;
200 });
201 var lstLeft = LeftDataSource.ToList();
202 lstLeft.AddRange(lst);
203 var lstRight = RightDataSource.ToList();
204 lstRight.RemoveAll(p => lst.Contains(p));
205 RightDataSource = lstRight.ToArray();
206 LeftDataSource = lstLeft.ToArray();
207 if (Transfered != null)
208 {
209 Transfered(this, new TransferEventArgs() { TransferDataSource = lst.ToArray(), ToRightOrLeft = false });
210 }
211 }
212 }
213 }
最后的话
如果你喜欢的话,请到 https://gitee.com/kwwwvagaa/net_winform_custom_control 点个星星吧
|