From bd87f7f6acc7a322f5225b841c434e124e9dcc65 Mon Sep 17 00:00:00 2001 From: Sunny Date: Tue, 31 Oct 2023 13:24:48 +0800 Subject: [PATCH] =?UTF-8?q?*=20UIEditForm:=20=E4=BB=A3=E7=A0=81=E7=94=9F?= =?UTF-8?q?=E6=88=90=E5=A2=9E=E5=8A=A0ComboDataGridView=E7=B1=BB=E5=9E=8B?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- SunnyUI/Forms/UIEditForm.cs | 40 +++++++++++++++++++++++++++++++ SunnyUI/Forms/UIEditFormHelper.cs | 26 +++++++++++++++++++- 2 files changed, 65 insertions(+), 1 deletion(-) diff --git a/SunnyUI/Forms/UIEditForm.cs b/SunnyUI/Forms/UIEditForm.cs index 0234cb7b..54499558 100644 --- a/SunnyUI/Forms/UIEditForm.cs +++ b/SunnyUI/Forms/UIEditForm.cs @@ -24,11 +24,13 @@ * 2022-04-18: V3.1.5 修改一处Show引起的无法获取控件值的问题 * 2023-04-23: V3.3.5 代码生成增加,Double类型增加小数点位数 * 2023-07-27: V3.4.1 默认提示弹窗TopMost为true + * 2023-10-31: V3.5.2 代码生成增加ComboDataGridView类型 ******************************************************************************/ using System; using System.Collections.Generic; using System.ComponentModel; +using System.Data; using System.Drawing; using System.Windows.Forms; @@ -213,6 +215,29 @@ namespace Sunny.UI } } + if (info.EditType == EditType.ComboDataGridView) + { + ctrl = new UIComboDataGridView(); + var edit = (UIComboDataGridView)ctrl; + edit.DataGridView.Init(); + edit.DataGridView.AutoGenerateColumns = true; + var obj = (DataTable)info.DataSource; + edit.DataGridView.DataSource = obj; + edit.DataGridView.SelectionMode = DataGridViewSelectionMode.FullRowSelect; + foreach (DataGridViewColumn item in edit.DataGridView.Columns) + { + item.ReadOnly = true; + } + + edit.SelectIndexChange += Edit_SelectIndexChange; + int index = (int)info.Value; + if (index >= 0) + { + edit.DataGridView.SelectedIndex = index; + edit.Text = obj.Rows[index][info.DisplayMember].ToString(); + } + } + if (ctrl != null) { ctrl.Left = Option.LabelWidth; @@ -246,6 +271,14 @@ namespace Sunny.UI btnOK.ShowFocusLine = btnCancel.ShowFocusLine = true; } + private void Edit_SelectIndexChange(object sender, int index) + { + UIComboDataGridView edit = (UIComboDataGridView)sender; + var info = Option.Dictionary[edit.Name.Replace("Edit_", "")]; + var obj = (DataTable)info.DataSource; + edit.Text = obj.Rows[index][info.DisplayMember].ToString(); + } + public UIEditForm(UIEditOption option) { InitializeComponent(); @@ -440,6 +473,13 @@ namespace Sunny.UI info.Value = result.ToArray(); } + + if (info.EditType == EditType.ComboDataGridView) + { + UIComboDataGridView edit = this.GetControl("Edit_" + info.DataPropertyName); + if (edit == null) continue; + info.Value = edit.DataGridView.SelectedIndex; + } } } diff --git a/SunnyUI/Forms/UIEditFormHelper.cs b/SunnyUI/Forms/UIEditFormHelper.cs index 4d7973d2..f7b754cc 100644 --- a/SunnyUI/Forms/UIEditFormHelper.cs +++ b/SunnyUI/Forms/UIEditFormHelper.cs @@ -39,7 +39,8 @@ namespace Sunny.UI Combobox, Switch, ComboTreeView, - ComboCheckedListBox + ComboCheckedListBox, + ComboDataGridView } public class ComboCheckedListBoxItem @@ -353,5 +354,28 @@ namespace Sunny.UI Infos.Add(info); Dictionary.TryAdd(info.DataPropertyName, info); } + + public void AddComboDataGridView(string dataPropertyName, string text, DataTable dataTable, string displayMember, + string valueMember, int selectedIndex = -1, bool enabled = true, bool halfWidth = false) + { + if (Dictionary.ContainsKey(dataPropertyName)) + throw new DuplicateNameException(dataPropertyName + ": 已经存在"); + + EditInfo info = new EditInfo() + { + DataPropertyName = dataPropertyName, + EditType = EditType.ComboDataGridView, + Text = text, + Value = selectedIndex, + DisplayMember = displayMember, + ValueMember = valueMember, + Enabled = enabled, + HalfWidth = halfWidth, + DataSource = dataTable + }; + + Infos.Add(info); + Dictionary.TryAdd(info.DataPropertyName, info); + } } }