简体   繁体   English

在DataGridView CurrentCellChanged事件中获取当前单元格列索引

[英]Get current cell column index in DataGridView CurrentCellChanged Event

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. 我有DataGridViewCurrentCellChanged事件处理程序,并且我希望能够从事件处理程序访问当前选定的单元格列索引。

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. 我曾经在CellClick处理程序中使用代码,该处理程序具有DataGridViewCellEventArgs作为参数,因此我能够从事件args参数获取列索引,但是CurrentCellChanged事件具有EventArgs作为参数,我认为这暗示着没有数据可用于这个事件。

Is there a way to access the new currently selected cells column index? 有没有办法访问当前新选择的单元格列索引?

Use the DataGridView's CurrentCell property. 使用DataGridView的CurrentCell属性。

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

It's worth noting, that if someone is using WPF (with the DataGrid , rather than DataGridView), they can simply do: 值得注意的是,如果有人使用WPF (使用DataGrid而不是DataGridView),他们可以简单地执行以下操作:

DataGrid currentGrid = sender as DataGrid;

and then 然后

currentGrid.CurrentColumn.DisplayIndex

or 要么

currentGrid.CurrentCell.Column.DisplayIndex

如果您想使用列标题进行检查,则

dataGridView.CurrentCell.Column.Header

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM