-
Notifications
You must be signed in to change notification settings - Fork 0
/
MainForm.cs
53 lines (43 loc) · 1.66 KB
/
MainForm.cs
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
namespace DataGridViewComboExample
{
using System.Windows.Forms;
public class MainForm {
public static readonly string[] ListBoxItems = { "Single", "Married" };
public MainForm()
{
this.Form = new MainFormView();
this.Form.FormClosed += (sender, e) => Application.Exit();
this.Form.Table.EditingControlShowing +=
(sender, e) => this.OnEditingControlShowing( e );
}
void OnEditingControlShowing(DataGridViewEditingControlShowingEventArgs e)
{
if ( e.Control is ComboBox comboEdited ) {
// Can also be set in the column, globally for all combo boxes
comboEdited.DataSource = ListBoxItems;
comboEdited.AutoCompleteMode = AutoCompleteMode.Append;
comboEdited.AutoCompleteSource = AutoCompleteSource.ListItems;
// Attach event handler
comboEdited.SelectedValueChanged +=
(sender, evt) => this.OnComboSelectedValueChanged( sender );
}
return;
}
void OnComboSelectedValueChanged(object sender)
{
string selectedValue;
ComboBox comboBox = (ComboBox) sender;
int selectedIndex = comboBox.SelectedIndex;
if ( selectedIndex >= 0 ) {
selectedValue = ListBoxItems[ selectedIndex ];
} else {
selectedValue = comboBox.Text;
}
this.Form.EdSelected.Text = selectedValue;
}
// The view of the form
public MainFormView Form {
get; private set;
}
}
}