將「EditOnEnter」設置爲true後,如果我沒有單擊組合框的向下箭頭部分,DataGridViewComboBoxCell
仍然需要兩次點擊才能打開。如何讓DataGridView組合框在單擊時顯示其下拉列表?
任何人都有任何線索如何解決這個問題?我有我自己使用的DataGridView
類,所以我可以通過一些我希望的智能事件處理程序輕鬆地解決系統範圍內的這個問題。
謝謝。
將「EditOnEnter」設置爲true後,如果我沒有單擊組合框的向下箭頭部分,DataGridViewComboBoxCell
仍然需要兩次點擊才能打開。如何讓DataGridView組合框在單擊時顯示其下拉列表?
任何人都有任何線索如何解決這個問題?我有我自己使用的DataGridView
類,所以我可以通過一些我希望的智能事件處理程序輕鬆地解決系統範圍內的這個問題。
謝謝。
既然你已經有DataGridView
的EditMode
屬性設置爲‘EditOnEnter’,你可以重寫其OnEditingControlShowing
方法,以確保作爲一個組合框獲得焦點儘快所示的下拉列表:
public class myDataGridView : DataGridView
{
protected override void OnEditingControlShowing(DataGridViewEditingControlShowingEventArgs e)
{
base.OnEditingControlShowing(e);
if (e.Control is ComboBox) {
SendKeys.Send("{F4}");
}
}
}
只要您的DataGridView
控件中的編輯控件獲得輸入焦點,上面的代碼就會檢查它是否是組合框。如果是這樣,那麼會導致下拉部分展開的F4鍵(當任何組合框具有焦點時嘗試它)。這有點破解,但它像一個魅力。
要避免的SendKeys問題,從Open dropdown(in a datagrid view) items on a single click嘗試的解決方案。實質上,在OnEditingControlShowing鉤子組合框的Enter事件中,在Enter事件處理程序中,設置ComboBox.DroppedDown = true。這似乎有同樣的效果,但沒有副作用@Cody Gray提到。
我用這個解決方案,因爲它避免了發送鍵擊:
覆蓋的OnCellClick方法(如果你繼承)或訂閱CellClick事件(如果你從另一個對象,而不是作爲一個改變DGV子類)。
protected override void OnCellClick(DataGridViewCellEventArgs e)
{
// Normally the user would need to click a combo box cell once to
// activate it and then again to drop the list down--this is annoying for
// our purposes so let the user activate the drop-down with a single click.
if (e.ColumnIndex == this.Columns["YourDropDownColumnName"].Index
&& e.RowIndex >= 0
&& e.RowIndex <= this.Rows.Count)
{
this.CurrentCell = this[e.ColumnIndex, e.RowIndex];
this.BeginEdit(false);
ComboBox comboBox = this.EditingControl as ComboBox;
if (comboBox != null)
{
comboBox.DroppedDown = true;
}
}
base.OnCellContentClick(e);
}
protected override void OnEditingControlShowing(DataGridViewEditingControlShowingEventArgs e)
{
base.OnEditingControlShowing(e);
DataGridViewComboBoxEditingControl dataGridViewComboBoxEditingControl = e.Control as DataGridViewComboBoxEditingControl;
if (dataGridViewComboBoxEditingControl != null)
{
dataGridViewComboBoxEditingControl.GotFocus += this.DataGridViewComboBoxEditingControl_GotFocus;
dataGridViewComboBoxEditingControl.Disposed += this.DataGridViewComboBoxEditingControl_Disposed;
}
}
private void DataGridViewComboBoxEditingControl_GotFocus(object sender, EventArgs e)
{
ComboBox comboBox = sender as ComboBox;
if (comboBox != null)
{
if (!comboBox.DroppedDown)
{
comboBox.DroppedDown = true;
}
}
}
private void DataGridViewComboBoxEditingControl_Disposed(object sender, EventArgs e)
{
Control control = sender as Control;
if (control != null)
{
control.GotFocus -= this.DataGridViewComboBoxEditingControl_GotFocus;
control.Disposed -= this.DataGridViewComboBoxEditingControl_Disposed;
}
}
我已經得到了很多在datagridview的類礦山的「黑客」的。還有一件事不會受到傷害。我今天會試試這個。 – 2010-11-28 17:07:03
以上是我的貓! – 2010-11-28 17:11:26
的確很有魅力! – 2010-11-28 18:29:00