It's that simple. How do I get the index of the currently selected Row
of a DataGridView
? I don't want the Row
object, I want the index (0 .. n).
就是这么简单。如何获得当前选定的DataGridView行的索引?我不想要行对象,我想要索引(0。n)。
12 个解决方案
#1
128
There is the RowIndex
property for the CurrentCell
property for the DataGridView.
DataGridView的CurrentCell属性有RowIndex属性。
datagridview.CurrentCell.RowIndex
Handle the SelectionChanged
event and find the index of the selected row as above.
处理SelectionChanged事件并找到如上所示所选行的索引。
#2
32
Use the Index property in your DGV's SelectedRows collection:
使用您的DGV的SelectedRows集合中的索引属性:
int index = yourDGV.SelectedRows[0].Index;
#3
7
dataGridView1.SelectedRows[0].Index;
Or if you wanted to use LINQ and get the index of all selected rows, you could do:
或者,如果您想使用LINQ获取所有选定行的索引,您可以这样做:
dataGridView1.SelectedRows.Select(r => r.Index);
#4
4
dataGridView1.SelectedRows[0].Index;
Here find all about datagridview C# datagridview tutorial
这里找到所有关于datagridview c# datagridview教程。
Lynda
琳达
#5
3
try this it will work...it will give you the index of selected row index...
试试这个就行了……它会给你选定的行索引…
int rowindex = dataGridView1.CurrentRow.Index;
MessageBox.Show(rowindex.ToString());
#6
2
try this
试试这个
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
}
#7
1
Try DataGridView.CurrentCellAddress.
DataGridView.CurrentCellAddress试试。
Returns: A Point that represents the row and column indexes of the currently active cell.
返回:表示当前活动单元格的行和列索引的点。
E.G. Select the first column and the fifth row, and you'll get back: Point( X=1, Y=5 )
例如,选择第一列和第五行,然后返回:Point(X=1, Y=5)
#8
1
Try it:
试一试:
int rc=dgvDataRc.CurrentCell.RowIndex;** //for find the row index number
MessageBox.Show("Current Row Index is = " + rc.ToString());
I hope it will help you.
我希望它能对你有所帮助。
#9
0
I modified @JayRiggs' answer, and this works. You need the if
because sometimes the SelectedRows may be empty, so the index operation will throw a exception.
我修改了@JayRiggs的回答。您需要if,因为有时SelectedRows可能是空的,因此索引操作将抛出异常。
if (yourDGV.SelectedRows.Count>0){
int index = yourDGV.SelectedRows[0].Index;
}
#10
0
You can try this code :
您可以试试下面的代码:
int columnIndex = dataGridView.CurrentCell.ColumnIndex;
int rowIndex = dataGridView.CurrentCell.RowIndex;
#11
0
I used if get row value is clicked:
我用if get行值被点击:
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
}
#12
-1
Try the following:
试试以下:
int myIndex = MyDataGrid.SelectedIndex;
This will give the index of the row which is currently selected.
这将给出当前选中的行的索引。
Hope this helps
希望这有助于
#1
128
There is the RowIndex
property for the CurrentCell
property for the DataGridView.
DataGridView的CurrentCell属性有RowIndex属性。
datagridview.CurrentCell.RowIndex
Handle the SelectionChanged
event and find the index of the selected row as above.
处理SelectionChanged事件并找到如上所示所选行的索引。
#2
32
Use the Index property in your DGV's SelectedRows collection:
使用您的DGV的SelectedRows集合中的索引属性:
int index = yourDGV.SelectedRows[0].Index;
#3
7
dataGridView1.SelectedRows[0].Index;
Or if you wanted to use LINQ and get the index of all selected rows, you could do:
或者,如果您想使用LINQ获取所有选定行的索引,您可以这样做:
dataGridView1.SelectedRows.Select(r => r.Index);
#4
4
dataGridView1.SelectedRows[0].Index;
Here find all about datagridview C# datagridview tutorial
这里找到所有关于datagridview c# datagridview教程。
Lynda
琳达
#5
3
try this it will work...it will give you the index of selected row index...
试试这个就行了……它会给你选定的行索引…
int rowindex = dataGridView1.CurrentRow.Index;
MessageBox.Show(rowindex.ToString());
#6
2
try this
试试这个
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
}
#7
1
Try DataGridView.CurrentCellAddress.
DataGridView.CurrentCellAddress试试。
Returns: A Point that represents the row and column indexes of the currently active cell.
返回:表示当前活动单元格的行和列索引的点。
E.G. Select the first column and the fifth row, and you'll get back: Point( X=1, Y=5 )
例如,选择第一列和第五行,然后返回:Point(X=1, Y=5)
#8
1
Try it:
试一试:
int rc=dgvDataRc.CurrentCell.RowIndex;** //for find the row index number
MessageBox.Show("Current Row Index is = " + rc.ToString());
I hope it will help you.
我希望它能对你有所帮助。
#9
0
I modified @JayRiggs' answer, and this works. You need the if
because sometimes the SelectedRows may be empty, so the index operation will throw a exception.
我修改了@JayRiggs的回答。您需要if,因为有时SelectedRows可能是空的,因此索引操作将抛出异常。
if (yourDGV.SelectedRows.Count>0){
int index = yourDGV.SelectedRows[0].Index;
}
#10
0
You can try this code :
您可以试试下面的代码:
int columnIndex = dataGridView.CurrentCell.ColumnIndex;
int rowIndex = dataGridView.CurrentCell.RowIndex;
#11
0
I used if get row value is clicked:
我用if get行值被点击:
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
}
#12
-1
Try the following:
试试以下:
int myIndex = MyDataGrid.SelectedIndex;
This will give the index of the row which is currently selected.
这将给出当前选中的行的索引。
Hope this helps
希望这有助于