2011-04-06 36 views

回答

12

您可以使用DataGridViewCell.Value屬性檢索存儲在特定單元格中的值。

所以檢索「第一」選擇小區和值顯示在MessageBox中,您可以:

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

可能上面是不是正是你需要做什麼。如果您提供更多詳情,我們可以提供更好的幫助。

+0

謝謝!但它足夠了,你告訴我,解決了我的問題:) – Brezhnews 2011-04-06 19:54:23

+0

MessageBox.Show(dataGridView1.SelectedCells [0] .Value?.ToString()); – Xarylem 2017-10-16 15:44:54

10
MessageBox.Show(" Value at 0,0" + DataGridView1.Rows[0].Cells[0].Value); 
+0

感謝!偉大工程! :) – Brezhnews 2011-04-06 19:45:03

2
private void dataGridView1_CellEndEdit(object sender, DataGridViewCellEventArgs e) 
    { 
     MessageBox.Show(Convert.ToString(dataGridView1.CurrentCell.Value)); 
    } 

有點遲,但希望它有助於

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

我加入這一個DataGrid的按鈕來獲得該行的用戶點擊該單元格的值:


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

其中X是要檢查的單元格。在我的情況下,Datagrid列計數從1開始,而不是0。不知道它是數據網格的默認值,還是因爲我使用SQL來填充信息。

1

總和所有細胞

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

Hi @سعدالضبي你可以在這個代碼塊中添加一些評論嗎? – 2017-11-27 10:22:07

+0

代碼只有答案對社區沒有用處 – JimHawkins 2017-11-27 11:09:13

相關問題