2011-04-06 57 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

相关问题