How can I get DataGridView cell value to be written in the MessageBox in C#?
This question is related to
c#
datagridview
messagebox
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.
private void dataGridView1_CellClick(object sender, DataGridViewCellEventArgs e)
{
int rowIndex = e.RowIndex; // Get the order of the current row
DataGridViewRow row = dataGridView1.Rows[rowIndex];//Store the value of the current row in a variable
MessageBox.Show(row.Cells[rowIndex].Value.ToString());//show message for current row
}
MessageBox.Show(" Value at 0,0" + DataGridView1.Rows[0].Cells[0].Value );
private void dataGridView1_CellEndEdit(object sender, DataGridViewCellEventArgs e)
{
MessageBox.Show(Convert.ToString(dataGridView1.CurrentCell.Value));
}
a bit late but hope it helps
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());
}
}
I added this to the Button of a datagrid to get the values of the cells in the row that the user is clicking:
string DGCell = dataGridView1.Rows[e.RowIndex].Cells[X].Value.ToString();
where X is the cell you want to check. Datagrid column count starts at 1 not 0 in my case. Not sure if it is default of a datagrid or because I am using SQL to populate the info.
try
{
for (int rows = 0; rows < dataGridView1.Rows.Count; rows++)
{
for (int col = 0; col < dataGridView1.Rows[rows].Cells.Count; col++)
{
s1 = dataGridView1.Rows[0].Cells[0].Value.ToString();
label20.Text = s1;
}
}
}
catch (Exception ex)
{
MessageBox.Show("try again"+ex);
}
Sum all cells
double X=0;
if (datagrid.Rows.Count-1 > 0)
{
for(int i = 0; i < datagrid.Rows.Count-1; i++)
{
for(int j = 0; j < datagrid.Rows.Count-1; j++)
{
X+=Convert.ToDouble(datagrid.Rows[i].Cells[j].Value.ToString());
}
}
}
Source: Stackoverflow.com