Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/299.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 使用C在DataGridView中的当前选定值#_C#_Winforms_Datagridview - Fatal编程技术网

C# 使用C在DataGridView中的当前选定值#

C# 使用C在DataGridView中的当前选定值#,c#,winforms,datagridview,C#,Winforms,Datagridview,我正在尝试从DataGridView获取当前选定的值 MessageBox.Show(""+dataGridView1.SelectedCells.ToString()+"") 但它从不显示选定的值 它表明 System.Windows.Forms.DataGridViewSelectedCellCollection 你应该这样做 MessageBox.Show(dataGrdiView1.SelectedCells[0].Value.ToString()); 尝试访问单个单元格的值或文本,

我正在尝试从DataGridView获取当前选定的值

MessageBox.Show(""+dataGridView1.SelectedCells.ToString()+"")
但它从不显示选定的值

它表明

System.Windows.Forms.DataGridViewSelectedCellCollection


你应该这样做

MessageBox.Show(dataGrdiView1.SelectedCells[0].Value.ToString());
尝试访问单个单元格的值或文本,而不是整个集合

 string text;
 foreach (DataGridViewCell cell in dataGridView1.SelectedCells)
 {
     //MessageBox.Show(cell.Value.ToString());
     text +=cell.Value.ToString();
 }
 MessageBox.Show(text);
您还可以遍历整个
SelectedCells
集合

 string text;
 foreach (DataGridViewCell cell in dataGridView1.SelectedCells)
 {
     //MessageBox.Show(cell.Value.ToString());
     text +=cell.Value.ToString();
 }
 MessageBox.Show(text);

如果总是选择一个单元格,您可以使用它。dataGridView1.SelectedCells[0]。tostring。获取类名ia的原因是,您获取的是所选单元格的集合,因为它们一次可以选择多个单元格。

winforms中的SelectedCells是DataGridViewCell的集合,并且它确实具有.Value属性。我也写过。但是你确定WinForms也是这样吗?我只是在WPF里查的,你说得对
DataGridCellInfo
确实没有
属性。