如何获取 DataGridView 中所有选定单元格的值?
我有一个具有 MultiSelect = true 的 DataGridView。用户从不同行选择不同单元格后,如何获取所有选定单元格的值?
I have a DataGridView that has MultiSelect = true. After the user selects different cells from different rows how can I get the value of all the selected cells?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您可以迭代 SelectedCells。
您只要求提供值,但您可能还想知道单元格的行和列,否则该值可能毫无意义。您也可以在单元对象上访问这些。
You can iterate over SelectedCells.
You asked only for the value, but you probably also want to know the row and the column of the cell otherwise the value could be meaningless. You can access these also on the cell object.
foreach -
有关 SelectedCells 属性的更多信息,请访问 http://msdn.microsoft.com/en-us/library/system.windows.forms.datagridview.selectedcells.aspx
对于 DataGridView 中的大量选择,SelectedCells 集合效率低下。您可以使用一种方法来获取所选单元格的计数。在此基础上进行迭代,速度会更快。
foreach -
More info on the SelectedCells Property can be found at http://msdn.microsoft.com/en-us/library/system.windows.forms.datagridview.selectedcells.aspx
The SelectedCells collection is inefficient with large selections in DataGridView. There is a method you can use to get the count of the selected cells. iterate based on that and it'll be faster.