繁体   English   中英

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

[英]How to get DataGridView cell value in messagebox?

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

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

我将其添加到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; // 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
    }

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM