我有一个带有DataGridView的表单和一个带有toolStripButton的BindingNavigator。 因此,我需要在选择任何单元格时禁用BindingNavigator的ToolStripButton,并在选择至少一个单元格时启用它。 我已尝试使用CellClick事件和SelectionChanged事件,但我无法解决此问题。
private void myDataGridView_CellEnter(object sender, DataGridViewCellEventArgs e)
{
if (myGridView.Columns[e.ColumnIndex].Name == "findValueDataGridViewTextBoxColumn"
|| myDataGridView.Columns[e.ColumnIndex].Name == "replaceValueDataGridViewTextBoxColumn")
{
myDropDownButton.Enabled = true;
}
else
{
myDropDownButton.Enabled = false;
}
}
知道怎么做吗?
答案 0 :(得分:0)
使用index而不是name和DataGridViewCellCancelEventArgs而不是DataGridViewCellEventArgs。
示例
private void dataGridView1_CellBeginEdit(object sender, DataGridViewCellCancelEventArgs e)
{
string msg = String.Format("Editing Cell at ({0}, {1})",
e.ColumnIndex, e.RowIndex);
//You go to your datagrid and identify the index of the replaceValueDataGridViewTextBoxColumn
//e.g Int ColIndex1 = replaceValueDataGridViewTextBoxColumn index 0 fro the first , 1 for second etc.
if(e.ColumnIndex = ColIndex1 || e.ColumnIndex = ColIndex2)
{
myDropDownButton.Enabled = true;
}
// this.Text = msg;
}
在你的情况下
private void myDataGridView_CellEnter(object sender, DataGridViewCellCancelEventArgs e)
{
//get the column index of your columns
//Here you dont need to call grid name just e is enough
//Note you can try e.Name may be , but i didn't
if (e.ColumnIndex == colIndex1 || e.ColumnIndex == colIndex2)
{
myDropDownButton.Enabled = true;
}
else
{
myDropDownButton.Enabled = false;
}
}
或尝试如下,设置断点并查看colName的值。
private void dataGridView1_CellMouseEnter(object sender,
DataGridViewCellEventArgs e)
{
string colName= (string)dataGridView1.Columns[e.ColumnIndex].Name
}
答案 1 :(得分:0)
您可以使用SelectedCells
的{{1}}属性:
DataGridView