我想在datagridview組合框中輸入值。但它不允許。該怎麼辦?如何讓用戶手動輸入c#中的datagridview組合框
5
A
回答
8
private void GridStockItemEntry_EditingControlShowing(object sender, DataGridViewEditingControlShowingEventArgs e)
{
DataGridViewRow row = GridStockItemEntry.CurrentRow;
DataGridViewCell cell = GridStockItemEntry.CurrentCell;
if (e.Control.GetType() == typeof(DataGridViewComboBoxEditingControl))
{
if (cell == row.Cells["ItemName"] && Convert.ToString(row.Cells["Type"].Value) == "Raw Material")
{
DataGridViewComboBoxEditingControl cbo = e.Control as DataGridViewComboBoxEditingControl;
cbo.DropDownStyle = ComboBoxStyle.DropDown;
cbo.Validating += new CancelEventHandler(cbo_Validating);
}
}
}
void cbo_Validating(object sender, CancelEventArgs e)
{
DataGridViewComboBoxEditingControl cbo = sender as DataGridViewComboBoxEditingControl;
DataGridView grid = cbo.EditingControlDataGridView;
object value = cbo.Text;
// Add value to list if not there
if (cbo.Items.IndexOf(value) == -1)
{
DataGridViewComboBoxCell cboCol = (DataGridViewComboBoxCell)grid.CurrentCell;
// Must add to both the current combobox as well as the template, to avoid duplicate entries...
cbo.Items.Add(value);
cboCol.Items.Add(value);
grid.CurrentCell.Value = value;
}
}
-1
確保DataGridView
的EditMode
屬性設置爲EditOnKeystrokeOrF2
同時,驗證ReadOnly
屬性設置爲False
。
1
也許,這個例子是更好的可讀性:
private void datagridview_EditingControlShowing(object sender, DataGridViewEditingControlShowingEventArgs e) {
DataGridView dgv = (DataGridView)sender;
if(dgv.CurrentCell.ColumnIndex==dgv.Columns["ColumnName"].Index) {
ComboBox cbx = (ComboBox)e.Control;
cbx.DropDownStyle = ComboBoxStyle.DropDown;
cbx.AutoCompleteSource = AutoCompleteSource.ListItems;
cbx.AutoCompleteMode = System.Windows.Forms.AutoCompleteMode.Suggest;
}
}
相關問題
- 1. 如何防止手動輸入到C#組合框中
- 2. 如何手動在datagridview中設置組合框中的項目?
- 3. datagridview中的c#組合框
- 4. 如何讓用戶手動輸入主鍵
- 5. 如何限制用戶輸入一個組合框,好讓你只能輸入是集合內的話嗎?
- 6. 如何在C++中使用組合作爲用戶輸入「cin」
- 7. DataGridView中的動態組合框
- 8. datagridview中的可移動組合框
- 9. C#:如何調整用戶在DataGridView中輸入的值?
- 10. DataGridView組合框
- 11. 如何創建組合框來自動填充,而用戶鍵入組合框中的拼圖在c#
- 12. 如何在用戶輸入值後獲取組合框文本
- 13. C#的DataGridView十進制組合框柱
- 14. DataGridView的組合框格室C#
- 15. 如何讓用戶輸入HTML中的表單框?
- 16. 如何在c函數中手動輸入數組?
- 17. dataGridView與組合框
- 18. 如何讓用戶控制數組中的輸入?
- 19. 記住用戶輸入到組合框中的值
- 20. C#DataGridView組合框綁定問題
- 21. C#Datagridview組合框選擇價值棒
- 22. DataGridView限制用戶輸入
- 23. 如何在datagridview中自動下拉組合框
- 24. 2組合框在datagridview的
- 25. 如何讓用戶無限期地輸入到C#列表中
- 26. 如何讓用戶在c#中輸入3次密碼?
- 27. C# - 使用SQL來源填充datagridview中的組合框
- 28. 如何讓組合框不接受Excel-Vba中的用戶輸入並允許vba.value更改?
- 29. 如何讓組合框的標籤在Flex中淡入淡出
- 30. 如何在C++中使用用戶輸入的數組
它亙古不變的允許組合框 – Even 2011-02-11 11:29:38
什麼不允許的組合框?我只是嘗試了一個帶有這些屬性的gridview和一列中的組合框。 – tzup 2011-02-11 13:15:57