// 取得当前单元的行 Index
Console.WriteLine(DataGridView1.CurrentCell.RowIndex);
另外,使用 DataGridView.CurrentCellAddress 属性(而不是直接访问单元)来确定单元所在的行:
DataGridView.CurrentCellAddress.Y 和列: DataGridView.CurrentCellAddress.X 。这对于避免取消共享行的共享非常有用。
当前的单元可以通过设定 DataGridView 对象的 CurrentCell 来改变。可以通过 CurrentCell 来设定
DataGridView 的激活单元。将 CurrentCell 设为 Nothing(null) 可以取消激活的单元。
// 设定 (0, 0) 为当前单元
DataGridView1.CurrentCell = DataGridView1[0, 0];
在整行选中模式开启时,你也可以通过 CurrentCell 来设定选定行。
/// <summary>
/// 向下遍历
/// </summary>
/// <param ></param>
/// <param ></param>
private void button4_Click(object sender, EventArgs e)
...{
int row = this.dataGridView1.CurrentRow.Index 1;
if (row > this.dataGridView1.RowCount - 1)
row = 0;
this.dataGridView1.CurrentCell = this.dataGridView1[0, row];
}
/// <summary>
/// 向上遍历
/// </summary>
/// <param ></param>
/// <param ></param>
private void button5_Click(object sender, EventArgs e)
...{
int row = this.dataGridView1.CurrentRow.Index - 1;
if (row < 0)
row = this.dataGridView1.RowCount - 1;
this.dataGridView1.CurrentCell = this.dataGridView1[0, row];
}
* 注意: this.dataGridView 的索引器的参数是: columnIndex, rowIndex 或是 columnName, rowIndex
这与习惯不同。datagridview重叠
********DataGridView 设定单元只读:
1) 使用 ReadOnly 属性
// 设置 DataGridView1 为只读
DataGridView1.ReadOnly = true;此时,用户的新增行操作和删除行操作也被屏蔽了。
// 设置 DataGridView1 的第2列整列单元为只读
DataGridView1.Columns[1].ReadOnly = true;
本文来自电脑杂谈,转载请注明本文网址:
http://www.pc-fly.com/a/tongxinshuyu/article-50070-4.html
大陆才真正屹立于强国之林
朋友前几天买的6Splus也是很耗电
我认为都不能