【发布时间】:2011-04-06 19:33:53
【问题描述】:
如何在 C# 中将 DataGridView 单元格值写入 MessageBox?
【问题讨论】:
标签: c# datagridview messagebox
如何在 C# 中将 DataGridView 单元格值写入 MessageBox?
【问题讨论】:
标签: c# datagridview messagebox
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());
}
}
【讨论】:
您可以使用DataGridViewCell.Value 属性来检索存储在特定单元格中的值。
因此,要检索“第一个”选定单元格的值并显示在 MessageBox 中,您可以:
MessageBox.Show(dataGridView1.SelectedCells[0].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);
}
【讨论】:
我将此添加到数据网格的按钮中,以获取用户单击的行中单元格的值:
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; // 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
}
【讨论】: