Get current cell column index in DataGridView CurrentCellChanged Event

让人想犯罪 __ 提交于 2019-12-04 23:45:04

Use DataGridView.CurrentCell property ..

http://msdn.microsoft.com/en-us/library/system.windows.forms.datagridview.currentcell.aspx

int columnIndex = dataGridView.CurrentCell.ColumnIndex;
int rowIndex = dataGridView.CurrentCell.RowIndex;

http://msdn.microsoft.com/en-us/library/system.windows.forms.datagridviewcell.aspx

Use the DataGridView's CurrentCell property.

void dataGridView1_CurrentCellChanged(object sender, EventArgs e)
{
    MessageBox.Show(dataGridView1.CurrentCell.ColumnIndex.ToString());
    MessageBox.Show(dataGridView1.CurrentCell.RowIndex.ToString());
}

If you wanna check it with header of the column then

dataGridView.CurrentCell.Column.Header
XtraSimplicity

It's worth noting, that if someone is using WPF (with the DataGrid, rather than DataGridView), they can simply do:

DataGrid currentGrid = sender as DataGrid;

and then

currentGrid.CurrentColumn.DisplayIndex

or

currentGrid.CurrentCell.Column.DisplayIndex
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!