ColumnControl.cs 31 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776
  1. using System;
  2. using System.Collections.Generic;
  3. using System.ComponentModel;
  4. using System.Drawing;
  5. using System.Data;
  6. using System.Linq;
  7. using System.Text;
  8. using System.Windows.Forms;
  9. using ComponentFactory.Krypton.Toolkit;
  10. using System.Reflection;
  11. namespace NXWMS.Client.FrmCustom
  12. {
  13. /// <summary>
  14. /// DataGridView列操作控件
  15. /// </summary>
  16. [ToolboxBitmap(typeof(System.Windows.Forms.Panel))]
  17. public partial class ColumnControl : UserControl
  18. {
  19. /// <summary>
  20. /// 字段值
  21. /// </summary>
  22. [Description("字段操作对象")]
  23. public class ControlFieldValue
  24. {
  25. public string Code { get; set; }
  26. public string Name { get; set; }
  27. }
  28. [Description("被绑定的数据视图")]
  29. public DataGridView ToDataGridView { get; set; }
  30. [Description("数据视图中指定操作的列")]
  31. public DataGridViewColumn ItemColumn { get; set; }
  32. [Description("当前操作单元格矩形长宽坐标")]
  33. public Rectangle CellRectangle { get; set; }
  34. [Description("系统屏幕矩形长宽坐标")]
  35. public Rectangle ScreenRectangle { get; set; }
  36. [Description("选择按钮")]
  37. public KryptonButton ButtonSelect { get; set; }
  38. [Description("控件标题")]
  39. public string Title { get; set; } = "";
  40. [Description("绑定控件对象")]
  41. public Control ItemControl { get; set; }
  42. [Description("是否允许输入(针对选择控件)")]
  43. public bool IsAllowInput { get; set; } = false;
  44. [Description("绑定控件对象类型")]
  45. public ControlType ItemControlType { get; set; }
  46. [Description("绑定控件对象数据")]
  47. public DataTable ItemControlData { get; set; }
  48. [Description("数据表格中查询显示列名称")]
  49. public Dictionary<string, string> ItemColumnDic { get; set; }
  50. [Description("数据表格对应筛选字段")]
  51. public List<ConditionColumnMode> ConditionColumnList { get; set; }
  52. [Description("弹窗数据视图条件")]
  53. public string ConditionColumnCode { get; set; }
  54. [Description("显示数据视图字段")]
  55. public string ShowColumns { get; set; }
  56. [Description("选择返回的字典编码")]
  57. public string ResultColumn { get; set; }
  58. [Description("设定弹出框位置(针对数据对象)")]
  59. public LocationType SetLocationType { get; set; }
  60. [Description("输入控件小数点长度(只针对浮点控件)")]
  61. public int InputDecimalLength { get; set; } = 0;
  62. [Description("输入控件最大值限制(只针对浮点控件)")]
  63. public int InputDecimalMax { get; set; } = 999999;
  64. [Description("输入控件最小值限制(只针对浮点控件)")]
  65. public int InputDecimalMin { get; set; } = 0;
  66. [Description("弹窗对象")]
  67. public Form ShowForm { get; set; }
  68. [Description("弹窗对象相对于屏幕显示的比例")]
  69. public double ShowFormProportion { get; set; } = 0.5;
  70. [Description("绑定控件对象类型")]
  71. public enum ControlType
  72. {
  73. ComboBox,
  74. Text,
  75. NumericUpDown,
  76. CheckBox,
  77. DataTime,
  78. Source,
  79. }
  80. public enum LocationType
  81. {
  82. Cell,
  83. ScreenCenter,
  84. }
  85. public enum OperationDataType
  86. {
  87. Selectd,
  88. Input,
  89. Other
  90. }
  91. /// <summary>
  92. /// 条件字段模型
  93. /// </summary>
  94. public class ConditionColumnMode
  95. {
  96. public string Name { get; set; }
  97. public string Describe { get; set; }
  98. }
  99. /// <summary>
  100. /// 是否新增了选择列
  101. /// </summary>
  102. private bool IsAddCheck { get; set; }
  103. /// <summary>
  104. /// 选择行索引列表
  105. /// </summary>
  106. [Description("选择行索引列表")]
  107. public List<int> CheckRowIndexList;
  108. [Description("最终操作是否未拖动")]
  109. public bool IsNoScroll = false;
  110. [Description("当前正在操作的单元格")]
  111. public DataGridViewCell CurrentDataGridViewCell;
  112. /// <summary>
  113. /// 数据视图控件模型
  114. /// </summary>
  115. public class ControlDataViewModel
  116. {
  117. /// <summary>
  118. /// 数据控件名称
  119. /// </summary>
  120. public string Name { get; set; } = Guid.NewGuid().ToString();
  121. /// <summary>
  122. /// 数据控件描述
  123. /// </summary>
  124. public string Describe { get; set; }
  125. /// <summary>
  126. /// 数据控件绑定对象
  127. /// </summary>
  128. public DataGridView DataGridView { get; set; }
  129. /// <summary>
  130. /// 数据控件绑定对象中的列
  131. /// </summary>
  132. public DataGridViewColumn DataGridViewColumn { get; set; }
  133. /// <summary>
  134. /// 数据控件绑定类型
  135. /// </summary>
  136. public ControlType ControlType { get; set; } = ControlType.Text;
  137. /// <summary>
  138. /// 数据控件数据
  139. /// </summary>
  140. public DataTable ControlData { get; set; }
  141. /// <summary>
  142. /// 数据视图筛选条件
  143. /// </summary>
  144. public List<ConditionColumnMode> ConditionColumnList { get; set; }
  145. /// <summary>
  146. /// 选择字段单元格显示
  147. /// </summary>
  148. public string CellColumn { get; set; }
  149. /// <summary>
  150. /// 显示列
  151. /// </summary>
  152. public Dictionary<string, string> ItemColumnDic { get; set; }
  153. /// <summary>
  154. /// 结果字段
  155. /// </summary>
  156. public string ResultColumn { get; set; }
  157. }
  158. /// <summary>
  159. /// 控件数据事件
  160. /// </summary>
  161. /// <param name="column">操作列</param>
  162. /// <param name="inData">对应列数据</param>
  163. /// <param name="operationDataType">该控件操作数据类型</param>
  164. /// <returns></returns>
  165. public delegate string ControlGetValueHandler(DataGridViewCell dataGridCell, object inData, OperationDataType operationDataType);
  166. public event ControlGetValueHandler ControlGetValueEvent;
  167. /// <summary>
  168. /// 按钮选择事件
  169. /// </summary>
  170. /// <param name="selectColumn">选择列</param>
  171. /// <param name="selectRow">选择行</param>
  172. /// <param name="resultColumnName">结果列名</param>
  173. /// <returns></returns>
  174. public delegate string ButtonSelectHandler(DataGridViewColumn selectColumn, DataGridViewRow selectRow, string resultColumnName);
  175. public event ButtonSelectHandler ButtonSelectEvent;
  176. /// <summary>
  177. /// 当有选择列时,单独选择事件
  178. /// </summary>
  179. /// <param name="selectColumn">选择列</param>
  180. /// <param name="selectRow">选择行</param>
  181. /// <param name="resultColumnName">结果列名</param>
  182. /// <returns></returns>
  183. public delegate string ControlCheckHandler(DataGridViewColumn selectColumn, DataGridViewRow selectRow, string resultColumnName);
  184. public event ControlCheckHandler ControlCheckEvent;
  185. /// <summary>
  186. /// 当有选择列时,去除选择事件
  187. /// </summary>
  188. /// <param name="selectColumn">选择列</param>
  189. /// <param name="selectRow">选择行</param>
  190. /// <param name="resultColumnName">结果列名</param>
  191. /// <returns></returns>
  192. public delegate string ControlNoCheckHandler(DataGridViewColumn selectColumn, DataGridViewRow selectRow, string resultColumnName);
  193. public event ControlNoCheckHandler ControlNoCheckEvent;
  194. public event EventHandler ButtonExitEvent;
  195. public ColumnControl()
  196. {
  197. InitializeComponent();
  198. Visible = false;
  199. LostFocus += new EventHandler(ColumnControl_LostFocus);
  200. this.Text = Title;
  201. }
  202. /// <summary>
  203. /// 组装
  204. /// </summary>
  205. /// <param name="controlDataViewModel">控件数据模型</param>
  206. public void Build(ControlDataViewModel controlDataViewModel)
  207. {
  208. ConditionColumnList = controlDataViewModel.ConditionColumnList; //条件列表
  209. ConditionColumnCode = controlDataViewModel.CellColumn; //单元格显示
  210. ResultColumn = controlDataViewModel.ResultColumn;
  211. ItemControlData = controlDataViewModel.ControlData;
  212. ItemControlType = controlDataViewModel.ControlType;
  213. ToDataGridView = controlDataViewModel.DataGridView;
  214. ItemColumn = controlDataViewModel.DataGridViewColumn;
  215. ItemColumnDic = controlDataViewModel.ItemColumnDic;
  216. Build();
  217. }
  218. /// <summary>
  219. /// 组装
  220. /// </summary>
  221. private void Build()
  222. {
  223. ToDataGridView.CurrentCellChanged += new EventHandler(this.ToDataGridView_CurrentCellChanged);
  224. ToDataGridView.Scroll += new ScrollEventHandler(this.ToDataGridView_Scroll);
  225. ToDataGridView.ColumnWidthChanged += new DataGridViewColumnEventHandler(this.ToDataGridView_ColumnWidthChanged);
  226. ToDataGridView.CurrentCellDirtyStateChanged += new System.EventHandler(this.TodataGridView_CurrentCellDirtyStateChanged);
  227. ToDataGridView.SizeChanged += new System.EventHandler(this.ToDataGridView_SizeChanged);
  228. switch (ItemControlType)
  229. {
  230. case ControlType.CheckBox:
  231. ItemControl = new KryptonCheckBox();
  232. ItemControl.KeyDown += new KeyEventHandler(ItemControl_KeyDown);
  233. ItemControl.Visible = false;
  234. break;
  235. case ControlType.ComboBox:
  236. ItemControl = new KryptonComboBox();
  237. ItemControl.KeyDown += new KeyEventHandler(ItemControl_KeyDown);
  238. ItemControl.Visible = false;
  239. var fieldList = new List<ControlFieldValue>();
  240. foreach (DataRow item in ItemControlData.Rows)
  241. {
  242. fieldList.Add(new ControlFieldValue
  243. {
  244. Code = item[ResultColumn].ToString(),
  245. Name = item[ConditionColumnCode].ToString()
  246. });
  247. }
  248. (ItemControl as KryptonComboBox).AutoCompleteCustomSource.AddRange(fieldList.Select(s => s.Name).ToArray());
  249. (ItemControl as KryptonComboBox).AutoCompleteSource = AutoCompleteSource.ListItems;
  250. (ItemControl as KryptonComboBox).AutoCompleteMode = AutoCompleteMode.SuggestAppend;
  251. (ItemControl as KryptonComboBox).DataSource = fieldList;
  252. (ItemControl as KryptonComboBox).DisplayMember = "Name";
  253. (ItemControl as KryptonComboBox).ValueMember = "Code";
  254. (ItemControl as KryptonComboBox).SelectedIndex = -1;
  255. if (!IsAllowInput)
  256. (ItemControl as KryptonComboBox).DropDownStyle = ComboBoxStyle.DropDownList;
  257. else
  258. (ItemControl as KryptonComboBox).DropDownStyle = ComboBoxStyle.DropDown;
  259. break;
  260. case ControlType.DataTime:
  261. ItemControl = new KryptonDateTimePicker();
  262. ItemControl.KeyDown += new KeyEventHandler(ItemControl_KeyDown);
  263. ItemControl.Visible = false;
  264. break;
  265. case ControlType.NumericUpDown:
  266. ItemControl = new KryptonNumericUpDown();
  267. ItemControl.KeyDown += new KeyEventHandler(ItemControl_KeyDown);
  268. ItemControl.Visible = false;
  269. (ItemControl as KryptonNumericUpDown).DecimalPlaces = InputDecimalLength;
  270. break;
  271. case ControlType.Text:
  272. ItemControl = new KryptonTextBox();
  273. ItemControl.KeyDown += new KeyEventHandler(ItemControl_KeyDown);
  274. ItemControl.Visible = false;
  275. break;
  276. case ControlType.Source:
  277. ItemControl = this;
  278. ButtonSelect = new KryptonButton();
  279. ButtonSelect.Visible = false;
  280. SetPanelCondition(ConditionColumnList);
  281. ButtonSelect.Click += new EventHandler(ShowPanelSelect_Click);
  282. ToDataGridView.Controls.Add(ButtonSelect);
  283. break;
  284. }
  285. ToDataGridView.Controls.Add(ItemControl);
  286. ToDataGridView.Controls.SetChildIndex(ItemControl, 99999);
  287. }
  288. private void ItemControl_KeyDown(object sender, KeyEventArgs e)
  289. {
  290. if (e.KeyValue == 13)
  291. {
  292. ItemControl_LostFocus(null, null);
  293. }
  294. }
  295. private void SetPanelCondition(List<ConditionColumnMode> conditionColumnList)
  296. {
  297. //建立条件框
  298. if (conditionColumnList.Any())
  299. {
  300. tableLayoutPanelCondtion.Visible = true;
  301. for (int i = 0; i < conditionColumnList.Count; i++)
  302. {
  303. var lbCol = tableLayoutPanelCondtion.Controls.Find("lbCondition" + (i + 1).ToString(), true)[0];
  304. if (lbCol != null)
  305. {
  306. var col = lbCol as KryptonLabel;
  307. col.Text = conditionColumnList[i].Describe;
  308. col.Visible = true;
  309. }
  310. var txtCol = tableLayoutPanelCondtion.Controls.Find("txtCondition" + (i + 1).ToString(), true)[0];
  311. if (txtCol != null)
  312. {
  313. var col = txtCol as KryptonTextBox;
  314. col.Tag = conditionColumnList[i].Name;
  315. txtCol.TextChanged += new EventHandler(Condition_TextChanged);
  316. col.Visible = true;
  317. }
  318. }
  319. }
  320. }
  321. private void ToDataGridView_SizeChanged(object sender, EventArgs e)
  322. {
  323. ToDataGridView_CurrentCellChanged(null, null);
  324. }
  325. private void ToDataGridView_Scroll(object sender, ScrollEventArgs e)
  326. {
  327. if (ItemControlType == ControlType.Source)
  328. {
  329. Visible = false;
  330. ButtonSelect.Visible = false;
  331. }
  332. else
  333. {
  334. ItemControl.Visible = false;
  335. }
  336. ToDataGridView.ClearSelection();
  337. IsNoScroll = true;
  338. }
  339. private void ToDataGridView_CellClick(object sender, DataGridViewCellEventArgs e)
  340. {
  341. if (IsNoScroll)
  342. {
  343. ToDataGridView_CurrentCellChanged(null, null);
  344. }
  345. }
  346. public void ToDataGridView_ColumnWidthChanged(object sender, DataGridViewColumnEventArgs e)
  347. {
  348. if (ItemControlType == ControlType.Source)
  349. {
  350. ToDataGridView_CurrentCellChanged(null, null);
  351. }
  352. }
  353. private void ToDataGridView_CurrentCellChanged(object sender, EventArgs e)
  354. {
  355. CurrentDataGridViewCell = ToDataGridView.CurrentCell;
  356. if (CurrentDataGridViewCell == null)
  357. return;
  358. var value = string.Empty;
  359. if (CurrentDataGridViewCell.Value == null)
  360. value = "";
  361. else
  362. value = CurrentDataGridViewCell.Value.ToString();
  363. if (CurrentDataGridViewCell != null && CurrentDataGridViewCell.RowIndex < ToDataGridView.Rows.Count && CurrentDataGridViewCell.OwningColumn.Name == ItemColumn.Name)
  364. {
  365. IsNoScroll = false;
  366. CellRectangle = ToDataGridView.GetCellDisplayRectangle(CurrentDataGridViewCell.ColumnIndex, CurrentDataGridViewCell.RowIndex, true);
  367. switch (ItemControlType)
  368. {
  369. case ControlType.CheckBox:
  370. ItemControl.Visible = true;
  371. (ItemControl as KryptonCheckBox).Text = ItemColumn.HeaderText;
  372. (ItemControl as KryptonCheckBox).Checked = Convert.ToBoolean(string.IsNullOrWhiteSpace(value) ? "FALSE" : value);
  373. break;
  374. case ControlType.ComboBox:
  375. (ItemControl as KryptonComboBox).Text = value;
  376. (ItemControl as KryptonComboBox).Focus();
  377. break;
  378. case ControlType.DataTime:
  379. ItemControl.Visible = true;
  380. if (string.IsNullOrWhiteSpace(value))
  381. {
  382. (ItemControl as KryptonDateTimePicker).Checked = false;
  383. }
  384. else
  385. {
  386. (ItemControl as KryptonDateTimePicker).Checked = true;
  387. (ItemControl as KryptonDateTimePicker).Value = DateTime.Parse(value);
  388. }
  389. (ItemControl as KryptonDateTimePicker).Focus();
  390. break;
  391. case ControlType.NumericUpDown:
  392. ItemControl.Visible = true;
  393. if (string.IsNullOrWhiteSpace(value))
  394. {
  395. (ItemControl as KryptonNumericUpDown).Value = 0;
  396. }
  397. else
  398. {
  399. (ItemControl as KryptonNumericUpDown).Value = Convert.ToDecimal(value);
  400. }
  401. (ItemControl as KryptonNumericUpDown).Maximum = InputDecimalMax;
  402. (ItemControl as KryptonNumericUpDown).Minimum = InputDecimalMin;
  403. (ItemControl as KryptonNumericUpDown).Focus();
  404. break;
  405. case ControlType.Text:
  406. ItemControl.Visible = true;
  407. (ItemControl as KryptonTextBox).Text = Convert.ToString(value);
  408. (ItemControl as KryptonTextBox).Focus();
  409. break;
  410. case ControlType.Source:
  411. ButtonSelect.Top = CellRectangle.Top;
  412. ButtonSelect.StateCommon.Content.ShortText.Font = new Font("宋体", 7.5f);
  413. ButtonSelect.Text = "..";
  414. ButtonSelect.Left = CellRectangle.Left + CellRectangle.Size.Width - 30;
  415. ButtonSelect.Size = new Size(30, CellRectangle.Size.Height);
  416. ButtonSelect.Visible = true;
  417. return;
  418. }
  419. ItemControl.Size = CellRectangle.Size;
  420. ItemControl.Top = CellRectangle.Top;
  421. ItemControl.Left = CellRectangle.Left;
  422. ItemControl.Visible = true;
  423. ItemControl.LostFocus += new EventHandler(ItemControl_LostFocus);
  424. ItemControl.GotFocus += new EventHandler(ItemControl_GostFocus);
  425. }
  426. else
  427. {
  428. if (ButtonSelect != null)
  429. {
  430. ButtonSelect.Visible = false;
  431. }
  432. ItemControl.Visible = false;
  433. }
  434. }
  435. public void AddMultipleCheck(int index)
  436. {
  437. if (ItemControlType == ControlType.Source)
  438. {
  439. IsAddCheck = true;
  440. ItemControlData.Columns.Add("#SELECTED", typeof(bool));
  441. }
  442. }
  443. private void TodataGridView_CurrentCellDirtyStateChanged(object sender, EventArgs e)
  444. {
  445. CurrentDataGridViewCell.Value = "";
  446. }
  447. private void dataGridViewSelect_CellClick(object sender, DataGridViewCellEventArgs e)
  448. {
  449. if (e.RowIndex >= 0 && e.ColumnIndex == 0)
  450. {
  451. if (this.dataGridViewSelect.Rows[e.RowIndex].Cells[e.ColumnIndex].Value == null)
  452. {
  453. if (!CheckRowIndexList.Where(s => s == e.RowIndex).Any())
  454. {
  455. CheckRowIndexList.Add(e.RowIndex);
  456. }
  457. ControlCheckEvent?.Invoke(ItemColumn, dataGridViewSelect.SelectedRows[0], ResultColumn);
  458. this.dataGridViewSelect.Rows[e.RowIndex].Cells[e.ColumnIndex].Value = 1;
  459. }
  460. else if (dataGridViewSelect.Rows[e.RowIndex].Cells[e.ColumnIndex].Value.ToString() == "1")
  461. {
  462. if (CheckRowIndexList.Where(s => s == e.RowIndex).Any())
  463. {
  464. CheckRowIndexList.Remove(e.RowIndex);
  465. }
  466. ControlNoCheckEvent?.Invoke(ItemColumn, dataGridViewSelect.SelectedRows[0], ResultColumn);
  467. this.dataGridViewSelect.Rows[e.RowIndex].Cells[e.ColumnIndex].Value = 0;
  468. }
  469. else
  470. {
  471. if (!CheckRowIndexList.Where(s => s == e.RowIndex).Any())
  472. {
  473. CheckRowIndexList.Add(e.RowIndex);
  474. }
  475. ControlCheckEvent?.Invoke(ItemColumn, dataGridViewSelect.SelectedRows[0], ResultColumn);
  476. this.dataGridViewSelect.Rows[e.RowIndex].Cells[e.ColumnIndex].Value = 1;
  477. }
  478. }
  479. }
  480. private void ShowPanelSelect_Click(object sender, EventArgs e)
  481. {
  482. if (dataGridViewSelect.Columns.Count == 0)
  483. {
  484. dataGridViewSelect.DataSource = ItemControlData;
  485. if (ConditionColumnList.Any())
  486. {
  487. foreach (DataGridViewColumn item in dataGridViewSelect.Columns)
  488. {
  489. item.Visible = false;
  490. }
  491. foreach (var item in ConditionColumnList)
  492. {
  493. if (dataGridViewSelect.Columns.Contains(item.Name))
  494. {
  495. dataGridViewSelect.Columns[item.Name].Visible = true;
  496. dataGridViewSelect.Columns[item.Name].HeaderText = item.Describe;
  497. dataGridViewSelect.Columns[item.Name].ReadOnly = true;
  498. }
  499. }
  500. }
  501. if (IsAddCheck)
  502. {
  503. dataGridViewSelect.CellClick += new DataGridViewCellEventHandler(dataGridViewSelect_CellClick);
  504. dataGridViewSelect.Columns["#SELECTED"].DisplayIndex = 0;
  505. dataGridViewSelect.Columns["#SELECTED"].Visible = true;
  506. dataGridViewSelect.Columns["#SELECTED"].ReadOnly = false;
  507. dataGridViewSelect.Columns["#SELECTED"].HeaderText = "选择";
  508. dataGridViewSelect.Columns["#SELECTED"].Resizable = DataGridViewTriState.False;
  509. dataGridViewSelect.Columns["#SELECTED"].Frozen = false;
  510. //dataGridViewSelect.Columns["#SELECTED"].Width = 20;
  511. dataGridViewSelect.Columns["#SELECTED"].ReadOnly = false;
  512. dataGridViewSelect.Columns["#SELECTED"].DefaultCellStyle.ForeColor = Color.Red;
  513. }
  514. else
  515. {
  516. ToDataGridView.CellDoubleClick += new DataGridViewCellEventHandler(this.dataGridViewSelect_CellDoubleClick);
  517. }
  518. }
  519. var cellHeight = ToDataGridView.GetCellDisplayRectangle(ToDataGridView.CurrentCell.ColumnIndex,
  520. ToDataGridView.CurrentCell.RowIndex, true).Height;
  521. this.Size = new Size(497, 337);
  522. switch (SetLocationType)
  523. {
  524. case LocationType.ScreenCenter:
  525. ScreenRectangle = Screen.GetWorkingArea(this);
  526. this.Location = new Point((ScreenRectangle.Width - Width) / 2,
  527. (ScreenRectangle.Height - Height) / 2);
  528. break;
  529. default:
  530. ItemControl.Top = CellRectangle.Top + cellHeight;
  531. ItemControl.Left = CellRectangle.Left;
  532. break;
  533. }
  534. if (SetLocationType == LocationType.Cell)
  535. {
  536. ToDataGridView.Controls.Add(ButtonSelect);
  537. this.Visible = true;
  538. }
  539. else
  540. {
  541. this.Visible = true;
  542. AddFormShow();
  543. ShowForm.ShowDialog();
  544. }
  545. }
  546. private void AddFormShow()
  547. {
  548. this.Visible = true;
  549. this.Dock = DockStyle.Fill;
  550. var newUserControl = this;
  551. newUserControl.Dock = DockStyle.Fill;
  552. ShowForm = new Form();
  553. ShowForm.Name = "ShowDiagion";
  554. ShowForm.StartPosition = FormStartPosition.CenterScreen;
  555. ShowForm.MaximizeBox = true;
  556. ShowForm.MinimizeBox = false;
  557. ShowForm.Location = newUserControl.Location;
  558. ShowForm.Size = new Size
  559. {
  560. Height = Convert.ToInt32(ScreenRectangle.Height * ShowFormProportion),
  561. Width = Convert.ToInt32(ScreenRectangle.Width * ShowFormProportion),
  562. };
  563. ShowForm.Text = newUserControl.Title;
  564. ShowForm.Controls.Add(newUserControl);
  565. }
  566. private void ItemControl_LostFocus(object sender, EventArgs e)
  567. {
  568. OperationDataType operationTemp;
  569. object value = null;
  570. switch (ItemControlType)
  571. {
  572. case ControlType.CheckBox:
  573. CurrentDataGridViewCell.Style.ForeColor = Color.Red;
  574. CurrentDataGridViewCell.Value = (ItemControl as KryptonCheckBox).Checked;
  575. ControlGetValueEvent?.Invoke(CurrentDataGridViewCell, (ItemControl as KryptonCheckBox).Checked, OperationDataType.Selectd);
  576. break;
  577. case ControlType.ComboBox:
  578. CurrentDataGridViewCell.Style.ForeColor = Color.Red;
  579. if ((ItemControl as KryptonComboBox).SelectedValue == null)
  580. {
  581. value = (ItemControl as KryptonComboBox).Text;
  582. operationTemp = OperationDataType.Input;
  583. }
  584. else
  585. {
  586. value = (ItemControl as KryptonComboBox).SelectedValue.ToString();
  587. operationTemp = OperationDataType.Selectd;
  588. }
  589. CurrentDataGridViewCell.Value = (ItemControl as KryptonComboBox).Text;
  590. CurrentDataGridViewCell.Tag = value;
  591. ControlGetValueEvent?.Invoke(CurrentDataGridViewCell, value, operationTemp);
  592. break;
  593. case ControlType.DataTime:
  594. CurrentDataGridViewCell.Style.ForeColor = Color.Red;
  595. if ((ItemControl as KryptonDateTimePicker).Checked)
  596. {
  597. value = (ItemControl as KryptonDateTimePicker).Value;
  598. CurrentDataGridViewCell.Value = value;
  599. }
  600. operationTemp = OperationDataType.Input;
  601. ControlGetValueEvent?.Invoke(CurrentDataGridViewCell, value, OperationDataType.Input);
  602. break;
  603. case ControlType.NumericUpDown:
  604. CurrentDataGridViewCell.Style.ForeColor = Color.Red;
  605. CurrentDataGridViewCell.Value = (ItemControl as KryptonNumericUpDown).Value;
  606. ControlGetValueEvent?.Invoke(CurrentDataGridViewCell, (ItemControl as KryptonNumericUpDown).Value, OperationDataType.Input);
  607. break;
  608. case ControlType.Text:
  609. CurrentDataGridViewCell.Style.ForeColor = Color.Red;
  610. CurrentDataGridViewCell.Value = (ItemControl as KryptonTextBox).Text;
  611. ControlGetValueEvent?.Invoke(CurrentDataGridViewCell, (ItemControl as KryptonTextBox).Text, OperationDataType.Input);
  612. break;
  613. case ControlType.Source:
  614. kryptonButtonSelect_Click(null, null);
  615. break;
  616. }
  617. }
  618. private void ItemControl_GostFocus(object sender, EventArgs e)
  619. {
  620. }
  621. private void ColumnControl_LostFocus(object sender, EventArgs e)
  622. {
  623. if (this.Visible)
  624. {
  625. this.Visible = false;
  626. ItemControl_LostFocus(null, null);
  627. }
  628. }
  629. private void kryptonButtonSelect_Click(object sender, EventArgs e)
  630. {
  631. if (dataGridViewSelect.SelectedRows.Count == 0)
  632. {
  633. return;
  634. }
  635. var selectIndex = dataGridViewSelect.SelectedRows[0].Index;
  636. if (selectIndex >= 0)
  637. {
  638. CurrentDataGridViewCell.Value = ItemControlData.Rows[selectIndex][ResultColumn];
  639. CurrentDataGridViewCell.Tag = string.IsNullOrWhiteSpace(ConditionColumnCode) ?
  640. ItemControlData.Rows[selectIndex][ConditionColumnList.FirstOrDefault().Name] :
  641. ItemControlData.Rows[selectIndex][ResultColumn];
  642. ButtonSelect.Visible = false;
  643. ButtonSelectEvent?.Invoke(ItemColumn, dataGridViewSelect.SelectedRows[0], ResultColumn);
  644. ShowForm.Hide();
  645. }
  646. else
  647. {
  648. KryptonMessageBox.Show($"请选择!", "提示信息", MessageBoxButtons.OK, MessageBoxIcon.Information);
  649. }
  650. }
  651. private void kryptonButtonExit_Click(object sender, EventArgs e)
  652. {
  653. if (ItemControlType == ControlType.Source)
  654. {
  655. this.Visible = false;
  656. ButtonSelect.Visible = false;
  657. ShowForm.Close();
  658. }
  659. else
  660. {
  661. ItemControl.Visible = false;
  662. }
  663. ButtonExitEvent?.Invoke(sender, e);
  664. }
  665. private void dataGridViewSelect_CellDoubleClick(object sender, DataGridViewCellEventArgs e)
  666. {
  667. if (ShowForm.Visible)
  668. {
  669. kryptonButtonSelect_Click(null, null);
  670. }
  671. }
  672. private void ColumnControl_Resize(object sender, EventArgs e)
  673. {
  674. }
  675. private void Condition_TextChanged(object sender, EventArgs e)
  676. {
  677. var txtCol = sender as KryptonTextBox;
  678. var likeSql = $"`{txtCol.Tag}` like '%{txtCol.Text}%'";
  679. DataRow[] dataRows = ItemControlData.Select(likeSql);
  680. if (dataRows == null || !dataRows.Any())
  681. {
  682. var dataTemp = ItemControlData.Copy();
  683. dataTemp.Rows.Clear();
  684. dataGridViewSelect.DataSource = dataTemp;
  685. }
  686. else
  687. {
  688. var result = dataRows.CopyToDataTable();
  689. dataGridViewSelect.DataSource = result;
  690. }
  691. }
  692. }
  693. }