【发布时间】:2011-04-04 10:29:02
【问题描述】:
就这么简单。如何获取DataGridView 的当前选定Row 的索引?我不想要 Row 对象,我想要索引 (0 .. n)。
【问题讨论】:
标签: c# .net winforms datagridview
就这么简单。如何获取DataGridView 的当前选定Row 的索引?我不想要 Row 对象,我想要索引 (0 .. n)。
【问题讨论】:
标签: c# .net winforms datagridview
dataGridView1.SelectedRows[0].Index;
或者,如果您想使用 LINQ 并获取所有选定行的索引,您可以这样做:
dataGridView1.SelectedRows.Select(r => r.Index);
【讨论】:
在 DGV 的 SelectedRows 集合中使用 Index 属性:
int index = yourDGV.SelectedRows[0].Index;
【讨论】:
if 包裹它
DataGridView 的CurrentCell 属性有RowIndex 属性。
datagridview.CurrentCell.RowIndex
如上处理SelectionChanged事件并找到选中行的索引。
【讨论】:
CurrentCell 返回“活动”单元格,这与“选定”不同。即使选择了多行,活动单元格也可能在其他地方,并且只能有一个活动单元格
【讨论】:
DataGridView.CurrentCellAddress.Y ... :)
试试这个它会工作...它会给你所选行索引的索引...
int rowindex = dataGridView1.CurrentRow.Index;
MessageBox.Show(rowindex.ToString());
【讨论】:
【讨论】:
试试这个
bool flag = dg1.CurrentRow.Selected;
if(flag)
{
/// datagridview row is selected in datagridview rowselect selection mode
}
else
{
/// no row is selected or last empty row is selected
}
【讨论】:
尝试以下方法:
int myIndex = MyDataGrid.SelectedIndex;
这将给出当前选择的行的索引。
希望对你有帮助
【讨论】:
我修改了@JayRiggs 的答案,这很有效。你需要if,因为有时候SelectedRows可能是空的,所以索引操作会抛出异常。
if (yourDGV.SelectedRows.Count>0){
int index = yourDGV.SelectedRows[0].Index;
}
【讨论】:
你可以试试这个代码:
int columnIndex = dataGridView.CurrentCell.ColumnIndex;
int rowIndex = dataGridView.CurrentCell.RowIndex;
【讨论】:
试试看:
int rc=dgvDataRc.CurrentCell.RowIndex;** //for find the row index number
MessageBox.Show("Current Row Index is = " + rc.ToString());
希望对你有帮助。
【讨论】:
datagridview.CurrentCell.RowIndex 的所选(也是最受好评的答案)重复。抱歉,我建议将这篇文章作为完整副本删除。
如果点击获取行值,我使用:
private void dataGridView_Product_CellClick(object sender, DataGridViewCellEventArgs e){
int rowIndex;
//rowIndex = e.RowIndex; //Option 1
//rowIndex= dataGridView_Product.CurrentCell.RowIndex; //Option 2
rowIndex = dataGridView_Product.CurrentRow.Index; //Option 3
}
【讨论】: