* UIEditForm: 代码生成增加ComboDataGridView类型

This commit is contained in:
Sunny 2023-10-31 13:24:48 +08:00
parent e93d84147f
commit bd87f7f6ac
2 changed files with 65 additions and 1 deletions

View File

@ -24,11 +24,13 @@
* 2022-04-18: V3.1.5 Show引起的无法获取控件值的问题 * 2022-04-18: V3.1.5 Show引起的无法获取控件值的问题
* 2023-04-23: V3.3.5 Double类型增加小数点位数 * 2023-04-23: V3.3.5 Double类型增加小数点位数
* 2023-07-27: V3.4.1 TopMost为true * 2023-07-27: V3.4.1 TopMost为true
* 2023-10-31: V3.5.2 ComboDataGridView类型
******************************************************************************/ ******************************************************************************/
using System; using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.ComponentModel; using System.ComponentModel;
using System.Data;
using System.Drawing; using System.Drawing;
using System.Windows.Forms; 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) if (ctrl != null)
{ {
ctrl.Left = Option.LabelWidth; ctrl.Left = Option.LabelWidth;
@ -246,6 +271,14 @@ namespace Sunny.UI
btnOK.ShowFocusLine = btnCancel.ShowFocusLine = true; 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) public UIEditForm(UIEditOption option)
{ {
InitializeComponent(); InitializeComponent();
@ -440,6 +473,13 @@ namespace Sunny.UI
info.Value = result.ToArray(); info.Value = result.ToArray();
} }
if (info.EditType == EditType.ComboDataGridView)
{
UIComboDataGridView edit = this.GetControl<UIComboDataGridView>("Edit_" + info.DataPropertyName);
if (edit == null) continue;
info.Value = edit.DataGridView.SelectedIndex;
}
} }
} }

View File

@ -39,7 +39,8 @@ namespace Sunny.UI
Combobox, Combobox,
Switch, Switch,
ComboTreeView, ComboTreeView,
ComboCheckedListBox ComboCheckedListBox,
ComboDataGridView
} }
public class ComboCheckedListBoxItem public class ComboCheckedListBoxItem
@ -353,5 +354,28 @@ namespace Sunny.UI
Infos.Add(info); Infos.Add(info);
Dictionary.TryAdd(info.DataPropertyName, 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);
}
} }
} }