user2254511
user2254511

Reputation: 123

c# dataGridView current cell in some function

Hello and sorry for my English.

dataGridView1_CellClick(object sender, DataGridViewCellEventArgs e)

This function has parameter DataGridViewCellEventArgs e, with whose help i can find out clicked cell:

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

But im writing function for Word Export:

private void WordExport_Click(object sender, EventArgs e)

Which work when i click on button. In this function i need to know current cell, same as in dataGridView1_CellClick function - dataGridView1.Rows[e.RowIndex].Cells[0].Value.ToString()

How can i get it?

Upvotes: 0

Views: 1841

Answers (1)

gzaxx
gzaxx

Reputation: 17590

DataGridView has property CurrentCell which is a reference to currently selected cell (can be null!). So to use it in your word export event do following:

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

hope that helps you out. Good luck :)

Upvotes: 0

Related Questions