簡體   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