如何在messagebox中获取DataGridView单元格值?

如何在C#中的MessageBox中编写DataGridView单元格值?

您可以使用DataGridViewCell.Value属性来检索存储在特定单元格中的值。

因此,要检索“第一个”选定单元格的值并在MessageBox中显示,您可以:

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

以上可能并不完全是你需要做的。 如果您提供更多细节,我们可以提供更好的帮助。

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

有点晚但希望它有所帮助

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

我将其添加到datagrid的Button中以获取用户单击的行中的单元格的值:


 string DGCell = dataGridView1.Rows[e.RowIndex].Cells[X].Value.ToString(); 

其中X是您要检查的单元格。 在我的情况下,Datagrid列计数从1开始,而不是0。 不确定它是否是数据网格的默认值,或者因为我使用SQL来填充信息。

求和所有细胞

  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()); } } } 
 private void dataGridView1_CellClick(object sender, DataGridViewCellEventArgs e) { int rowIndex = e.RowIndex; DataGridViewRow row = dataGridView1.Rows[rowIndex]; MessageBox.Show(row.Cells[rowIndex].Value.ToString()); }