问题
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