【问题标题】:c# dataGridView current cell in some functionc# dataGridView当前单元格中的某个函数
【发布时间】:2013-06-20 07:01:15
【问题描述】:

你好,对不起我的英语。

dataGridView1_CellClick(object sender, DataGridViewCellEventArgs e)

这个函数有参数DataGridViewCellEventArgs e,在它的帮助下我可以找到点击的单元格:

dataGridView1.Rows[e.RowIndex].Cells[0].Value.ToString()

但我正在为 Word 导出编写函数:

private void WordExport_Click(object sender, EventArgs e)

当我点击按钮时会起作用。在这个函数中,我需要知道当前单元格,与 dataGridView1_CellClick 函数——dataGridView1.Rows[e.RowIndex].Cells[0].Value.ToString()

我怎样才能得到它?

【问题讨论】:

  • 在这种情况下,current 单元格是什么?
  • 尝试gridView.CurrentCell 属性或gridView.SelectedCells 用于多选模式。如果我没听错……

标签: c# datagridview


【解决方案1】:

DataGridView 具有属性CurrentCell,它是对当前选定单元格的引用(可以为空!)。因此,要在您的单词导出事件中使用它,请执行以下操作:

private void WordExport_Click(object sender, EventArgs e)
{
    if (dataGridView1.CurrentCell == null) //no cell is selected.
    {
         return;
    }

    var value = dataGridVIew1.CurrentCell.Value.ToString();
    //or if you always want a value from cell in first column
    var value = dataGridVIew1.CurrentCell.OwningRow.Cells[0].Value.ToString()    
}

希望对您有所帮助。祝你好运:)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多