我如何使用SelectedRows数据网格视图所选行的数据? [英] How do I get the selected row data from a data grid view using SelectedRows?
问题描述
我有我的数据网格视图控件正在显示的表。用户选择从控制和presses一个按钮的单个行。我需要检索从该行的细胞,并将它们存储为字符串。
I have a table that I am displaying in a data grid view control. The user selects a single row from the control and presses a button. I need to retrieve the cells from that row and store them as strings.
到底我怎么用SelectedRow方法中的数据?我一直在这方面的工作了几个小时,我在我的穷途末路。下面是一些我已经试过一个例子:
Exactly how do I get the data using the SelectedRow method? I've been working on this for several hours and I'm at the end of my rope. Here's an example of something I've tried:
DataGridViewCellCollection selRowData = dataGridView1.SelectedRows[0].Cells;
如果我尝试访问selRowData [X],返回值不包含我的数据。
If I try to access selRowData[x], the return value does not contain my data.
推荐答案
您已经接近 - 你需要引用每个细胞
通过其索引并返回值
属性:
You're close - you need to reference each Cell
through its index and return its Value
property:
string firstCellValue = dataGridView1.SelectedRows[0].Cells[0].Value;
string secondCellValue = dataGridView1.SelectedRows[0].Cells[1].Value;
等。
这篇关于我如何使用SelectedRows数据网格视图所选行的数据?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!