Visual Basic 2010 .NET,DataGridView,两列。当用户单击,选择焦点或将焦点放在第0列中时,要强制第1列为选定列。
If Me.DGV.Rows.Count > 0 Then
If Me.DGV.CurrentCell.ColumnIndex = 0 Then ' if ColumnIndex 0
Me.DGV.Item(1, Me.DGV.CurrentCell.RowIndex).Selected = True ' change ColumnIndex to 1
End If
End If
在不抛出异常的情况下,代码示例在任何Cell事件中都不起作用。它完美地适用于Button,但我需要这个代码在用户与DGV交互时运行
DGV的'CellClick'事件有点奏效。当我按住鼠标按钮时,将指针移动到上方或下方的行,并释放鼠标按钮,它会混乱
Private Sub DGV_CellClick(sender As Object, e As System.Windows.Forms.DataGridViewCellEventArgs) Handles DGV.CellClick
If Me.DGV.Rows.Count > 0 Then
If e.ColumnIndex = 0 Then
Me.DGV.Item(1, e.RowIndex).Selected = True
End If
End If
End Sub
DGV的“点击”事件按照我想要的方式运行,但更改列索引号时网格会闪烁?
Private Sub DGV_Click(sender As Object, e As System.EventArgs) Handles DGV.Click
If Me.DGV.Rows.Count > 0 Then
If Me.DGV.CurrentCell.ColumnIndex = 0 Then
Me.DGV.Item(1, Me.DGV.CurrentCell.RowIndex).Selected = True 'This line causes a subtle 'flicker'
End If
End If
End Sub
答案 0 :(得分:1)
以下示例代码段可以执行所需的操作(来源:http://msdn.microsoft.com/en-us/library/system.windows.forms.datagridview.cellclick.aspx)。请注意与您的问题相关的e.RowIndex
和e.ColumnIndex
属性:
private void dataGridView1_CellClick(object sender, DataGridViewCellEventArgs e)
{
if (turn.Text.Equals(gameOverString)) { return; }
DataGridViewImageCell cell = (DataGridViewImageCell)
dataGridView1.Rows[e.RowIndex].Cells[e.ColumnIndex];
if (cell.Value == blank)
{
if (IsOsTurn())
{
cell.Value = o;
}
else
{
cell.Value = x;
}
ToggleTurn();
}
if (IsAWin())
{
turn.Text = gameOverString;
}
}