Get current cell column index in DataGridView CurrentCellChanged Event

浪子不回头ぞ 提交于 2020-01-02 01:08:10

问题


I have the CurrentCellChanged event handler of a DataGridView and i want to be able to access the current selected cells column index from the event handler.

I used to have the code in the CellClick handler which has DataGridViewCellEventArgs as a parameter so i was able to get the column index from the event args parameter but the CurrentCellChanged event has EventArgs as parameters which i believe is supposed to imply that theres no data for this event.

Is there a way to access the new currently selected cells column index?


回答1:


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




回答2:


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());
}



回答3:


If you wanna check it with header of the column then

dataGridView.CurrentCell.Column.Header



回答4:


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


来源:https://stackoverflow.com/questions/5749747/get-current-cell-column-index-in-datagridview-currentcellchanged-event

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