2013-05-14 84 views
0

如何才能从选定的单元格DataGridView C#中获取值? 不是一个如何获取选定单元格的值DataGridView C#?

我需要多一个,所有我选择了DB

保存在windows窗体

我要检查在第一列的每一个细胞,然后获取其值“ID”

此列是一个组合框

+2

你的问题不完全是ASPNET,WinForm的,WPF?你需要添加更多的上下文到您的问题 – V4Vendetta 2013-05-14 08:54:14

+0

窗口 – 2013-05-14 09:27:20

+0

我想检查第一列中的每个单元格,然后得到它的值“ID” 此列是一个组合框 http://i.stack.imgur的.com/7cEjH.png – 2013-05-14 09:38:39

回答

-1
DataGridViewSelectedRowCollection t = this.theDataGridView.SelectedRows; 

object theValue = this.theDataGridView.Rows[t[0].Index].Cells[index].Value; 

循环DataGridView的 的foreach(的DataGridViewRow排在于此。 dataGridView1.Rows) {
foreach(DataGridViewCell cell in row.Cells) { Label lbl1 = new lable();

lbl1=(label) cell.findcontrol('lbl'); 

} 

}

找到所选行的rowIndex位置,并找到控制,你会得到所有选定行。

+0

这不会得到尽可能请求 – DaveHogan 2013-05-14 09:05:00

+0

我使用它,但存在错误 的foreach(在this.dgvFishStatis.Rows的DataGridViewRow行) { 的DataGridViewCell细胞= row.Cells所选单元[2] ; //注意指定列索引 if(cell.Value!= null) { MessageBox.Show(dgvFishStatis.Rows [dgvFishStatis.SelectedCells [0] .Value.ToString()]); } } – 2013-05-14 09:12:32

+0

你在这个theDataGridView.SelectedRows – rams 2013-05-14 09:13:04

1

您可以使用此代码

DataGridViewSelectedCellCollection DGV = this.dataGridView1.SelectedCells; 
    for (int i = 0; i <= DGV.Count - 1; i++) 
    { 
     MessageBox.Show(DGV [i].Value.ToString()); 
    } 
相关问题