单击后如何使 DataGridViewCell 中的 ComboBox 下拉?
How to get a ComboBox in a DataGridViewCell to drop down after a single click?
我有一个 DataGridView,它的第一列样式设置为 ComboBox 而不是默认的 TextBox。由于 DataGridView 中的行数在启动时不固定,因此在添加新行时我无法将数据加载到每行的 ComboBoxes 中。所以我尝试加载用户向 DataGridView 添加行的事件:
public void myDataGridView_UserAddedRow(object sender, DataGridViewRowEventArgs e)
{
// Identifiers used are:
var myTableAdapter = new databaseTableAdapters.myTableTableAdapter();
var myDataTable = myTableAdapter.GetData();
int rowIndex = myDataGridView.CurrentcellAddress.Y;
var comboBoxCell = (DataGridViewComboBoxCell)myDataGridView.Rows[rowIndex].Cells[0];
string itemToAdd;
// Load in the data from the data table
foreach (System.Data.DataRow row in myDataTable.Rows)
{
// Get the current item to be added
itemToAdd = row[0].ToString();
// Make sure there are no duplicates
if (!comboBoxCell.Items.Contains(itemToAdd))
{
comboBoxCell.Items.Add(itemToAdd)
}
}
}
但这只会让用户在点击 秒 之后看到下拉选项。我希望能够让用户只单击一次组合框并查看选项,而不是不太直观的双击。如何做到这一点?
单元格必须获得焦点才能出现下拉菜单,因此 双击 实际上是单击一次以获得对该单元格的焦点 和 第二次点击是导致下拉发生的原因。所以在this link之后看看如何改变焦点。我能够用一行代码修改代码
public void myDataGridView_UserAddedRow(object sender, DataGridViewRowEventArgs e)
{
// Identifiers used are:
var myTableAdapter = new databaseTableAdapters.myTableTableAdapter();
var myDataTable = myTableAdapter.GetData();
int rowIndex = myDataGridView.CurrentcellAddress.Y;
var comboBoxCell = (DataGridViewComboBoxCell)myDataGridView.Rows[rowIndex].Cells[0];
string itemToAdd;
// Load in the data from the data table
foreach (System.Data.DataRow row in myDataTable.Rows)
{
// Get the current item to be added
itemToAdd = row[0].ToString();
// Make sure there are no duplicates
if (!comboBoxCell.Items.Contains(itemToAdd))
{
comboBoxCell.Items.Add(itemToAdd)
}
}
// Send the focus to the next combo box (removes need for a double click)
myDataGridView.CurrentCell = myDataGridView.Rows[rowIndex + 1].Cells[0]; // <--- HERE
}
我有一个 DataGridView,它的第一列样式设置为 ComboBox 而不是默认的 TextBox。由于 DataGridView 中的行数在启动时不固定,因此在添加新行时我无法将数据加载到每行的 ComboBoxes 中。所以我尝试加载用户向 DataGridView 添加行的事件:
public void myDataGridView_UserAddedRow(object sender, DataGridViewRowEventArgs e)
{
// Identifiers used are:
var myTableAdapter = new databaseTableAdapters.myTableTableAdapter();
var myDataTable = myTableAdapter.GetData();
int rowIndex = myDataGridView.CurrentcellAddress.Y;
var comboBoxCell = (DataGridViewComboBoxCell)myDataGridView.Rows[rowIndex].Cells[0];
string itemToAdd;
// Load in the data from the data table
foreach (System.Data.DataRow row in myDataTable.Rows)
{
// Get the current item to be added
itemToAdd = row[0].ToString();
// Make sure there are no duplicates
if (!comboBoxCell.Items.Contains(itemToAdd))
{
comboBoxCell.Items.Add(itemToAdd)
}
}
}
但这只会让用户在点击 秒 之后看到下拉选项。我希望能够让用户只单击一次组合框并查看选项,而不是不太直观的双击。如何做到这一点?
单元格必须获得焦点才能出现下拉菜单,因此 双击 实际上是单击一次以获得对该单元格的焦点 和 第二次点击是导致下拉发生的原因。所以在this link之后看看如何改变焦点。我能够用一行代码修改代码
public void myDataGridView_UserAddedRow(object sender, DataGridViewRowEventArgs e)
{
// Identifiers used are:
var myTableAdapter = new databaseTableAdapters.myTableTableAdapter();
var myDataTable = myTableAdapter.GetData();
int rowIndex = myDataGridView.CurrentcellAddress.Y;
var comboBoxCell = (DataGridViewComboBoxCell)myDataGridView.Rows[rowIndex].Cells[0];
string itemToAdd;
// Load in the data from the data table
foreach (System.Data.DataRow row in myDataTable.Rows)
{
// Get the current item to be added
itemToAdd = row[0].ToString();
// Make sure there are no duplicates
if (!comboBoxCell.Items.Contains(itemToAdd))
{
comboBoxCell.Items.Add(itemToAdd)
}
}
// Send the focus to the next combo box (removes need for a double click)
myDataGridView.CurrentCell = myDataGridView.Rows[rowIndex + 1].Cells[0]; // <--- HERE
}