定位到指定行
dataGridView1.CurrentCell = row.Cells[ColPoCode.Name];
单选时,得到选择行的行索引
private int GetSelectedRowIndex(DataGridView dgv)
{
if (dgv.Rows.Count == 0)
{
return 0;
}
foreach (DataGridViewRow row in dgv.Rows)
{
if (row.Selected)
{
return row.Index;
}
}
return 0;
}
取消全部选择
this.dataGridView1.ClearSelection();
private void dataGridView1_MouseDown(object sender, MouseEventArgs e)
{
if (e.Button == MouseButtons.Right)
{
var hit = this.dataGridView1.HitTest(e.X, e.Y);
if (hit.RowIndex >= 0)
{
var rowView = this.dataGridView1.Rows[hit.RowIndex].DataBoundItem as DataRowView;
if (rowView == null)
return;
int factoryID = rowView.Row.Field<int>("FactoryID");
this.dataGridView1.Rows[hit.RowIndex].Selected = true;
}
}
}