Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get DataGridView cell value in messagebox?

How can I get DataGridView cell value to be written in the MessageBox in C#?

like image 453
Brezhnews Avatar asked Apr 06 '11 19:04

Brezhnews


People also ask

What is cell in DataGridView?

The DataGridViewCell class represents an individual cell in a DataGridView control. You can retrieve cells through the Cells collection of a DataGridViewRow. The row and column for a DataGridViewCell identify the cell's location in the DataGridView.


2 Answers

private void dataGridView1_CellClick(object sender, DataGridViewCellEventArgs e)
{
    if (dataGridView1.Rows[e.RowIndex].Cells[e.ColumnIndex].Value != null)
    {
       MessageBox.Show(dataGridView1.Rows[e.RowIndex].Cells[e.ColumnIndex].Value.ToString());
    }
}
like image 199
Mohsen Safari Avatar answered Oct 06 '22 18:10

Mohsen Safari


You can use the DataGridViewCell.Value Property to retrieve the value stored in a particular cell.

So to retrieve the value of the 'first' selected Cell and display in a MessageBox, you can:

MessageBox.Show(dataGridView1.SelectedCells[0].Value.ToString());

The above probably isn't exactly what you need to do. If you provide more details we can provide better help.

like image 36
Jay Riggs Avatar answered Oct 06 '22 17:10

Jay Riggs