访问单元格 datagridview Winform C# 的内容

5

你好,我成功地获取了鼠标所选中的行。但是我不知道如何访问它的内容。

例如:我选择了第25行,我可以知道它选择了第25行,但我不知道如何访问它的内容并将其放入文本框中。有人能帮忙吗?


请发布您尝试过的代码。 - Brissles
1
theGrid[j, i].Value.ToString() 可能也会访问它,我认为 theGrid[j, i] 相当简洁。 - barlop
dataGridView1[columnIndex, rowIndex].Value.ToString() 只是为了澄清索引的位置。 - Luis Almeida
1个回答

7
您可以使用value属性按如下方式访问单元格的内容。
// the content of the cell
var x = dataGridView1.Rows[0].Cells[0].Value;

// this gets the content of the first selected cell 
dataGridView1.SelectedCells[0].Value;

// you can set the cells the user is able to select using this 
dataGridView1.SelectionMode= SelectionMode. //intellisense gives you some options here

要做到您所要求的,类似以下内容应该就足够了。
System.Text.StringBuilder t = new System.Text.StringBuilder();
String delimiter = ",";

foreach(DataGridViewCell c in dataGridView1.SelectedRows[0].Cells)
{
    t.Append(c.Value);
    t.Append(delimiter);
}

textBox1.Text = t.ToString();

非常感谢您,先生!我会使用这个并且如果它有效的话会向您更新。谢谢:)) - rj tubera
1
非常感谢您!这真的帮了我很多。我很欣赏您先给出示例而不是直接给出答案,这样我就可以更容易地理解它。谢谢:)) - rj tubera
如何访问dataGridView1变量?你是否在某处声明了它? - Lightsout

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接