I tried to use modeless picklist for DataGridView custom ComboBoxColumn
without success.
Steps to reproduce issue:
1. Run code
2. Enter some character
3. Press Tab
Observed:
1. Next row receives focus
2. Current form remains active
Expected:
1. Focus should remain in current combobox
2. Lookup form must become active form
How to fix ?
Andrus.
using System;
using System.Windows.Forms;
class ComboBoxColumn : DataGridViewComboBoxColumn {
}
class ComboBoxCell : DataGridViewComboBoxCell {
public override Type EditType {
get {
return typeof(ComboBoxEditingControl);
}
}
}
class ComboBoxEditingControl : MyComboBox, IDataGridViewEditingControl {
protected int rowIndex;
protected DataGridView dataGridView;
protected bool valueChanged = false;
protected override void OnTextChanged(EventArgs e) {
base.OnTextChanged(e);
NotifyDataGridViewOfValueChange();
}
protected virtual void NotifyDataGridViewOfValueChange() {
this.valueChanged = true;
if (this.dataGridView != null) {
this.dataGridView.NotifyCurrentCellDirty(true);
}
}
public Cursor EditingPanelCursor {
get {
return Cursors.IBeam;
}
}
public DataGridView EditingControlDataGridView {
get {
return this.dataGridView;
}
set {
this.dataGridView = value;
}
}
public object EditingControlFormattedValue {
set {
if (value.ToString() != this.Text) {
this.Text = value.ToString();
NotifyDataGridViewOfValueChange();
}
}
get {
return this.Text;
}
}
public object GetEditingControlFormattedValue(DataGridViewDataEr rorContexts
context) {
return this.Text;
}
public void PrepareEditingControlForEdit(bool selectAll) {
}
public bool RepositionEditingControlOnValueChange {
get {
return false;
}
}
public int EditingControlRowIndex {
get {
return rowIndex;
}
set {
this.rowIndex = value;
}
}
public void ApplyCellStyleToEditingControl(DataGridViewCellSty le
dataGridViewCellStyle) {
DropDownStyle = ComboBoxStyle.DropDown;
}
public bool EditingControlWantsInputKey(Keys keyData, bool
dataGridViewWantsInputKey) {
return !dataGridViewWantsInputKey;
}
public bool EditingControlValueChanged {
get {
return valueChanged;
}
set {
valueChanged = value;
}
}
public class Form1 : Form {
private DataGridView dataGridView1 = new myDataGridView();
[STAThread]
public static void Main() {
try {
Application.Run(new Form1());
}
catch (Exception e) {
MessageBox.Show(e.ToString());
}
}
public Form1() {
this.Controls.Add(this.dataGridView1);
this.Load += new EventHandler(Form1_Load);
}
private void Form1_Load(object sender, EventArgs e) {
ComboBoxColumn comboBoxColumn = new ComboBoxColumn();
ComboBoxCell ComboBoxCell = new ComboBoxCell();
comboBoxColumn.CellTemplate = ComboBoxCell;
dataGridView1.Columns.Add(comboBoxColumn);
}
}
}
public class MyComboBox : ComboBox {
protected override void OnValidating(System.ComponentModel.CancelEventArgs
e) {
e.Cancel = true;
base.OnValidating(e);
Form f = new Form();
f.Text = "Modeless lookup form";
f.Show();
}
}
public class myDataGridView : DataGridView {
protected override void OnDataError(bool displayErrorDialogIfNoHandler,
DataGridViewDataErrorEventArgs e) {
e.Cancel = false;
}
}