数据网格视图选择单元格值如何获取它

编程入门 行业动态 更新时间:2024-10-11 07:25:13
本文介绍了数据网格视图选择单元格值如何获取它的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我想在Click事件中选择DataGridView Cell值我使用此代码会产生错误

i Want to get DataGridView selected Cell value on click event i use this code its generates a error

DataGridViewRow dgvrows = TrGrid.SelectedRows; string c = dgvrows.Cells("Late_Time").value.toString();

推荐答案

DataGridView有一个 CurrentCell属性 [ ^ 。我认为这就是选定单元格的含义。如果你想查看所有选中的单元格,它有一个可枚举的SelectedCells属性来给出单元格对象。 DataGridView has a CurrentCell property[^]. I think that is what you mean by 'selected cell'. If you want to look at all the selected cells, it has a SelectedCells property which is enumerable to give cell objects.

下面这段代码可以帮到你: The following piece of code will do the trick for you: private void dataGridView1_CellClick(object sender, DataGridViewCellEventArgs e) { if (dataGridView1.Rows[e.RowIndex].Cells[e.ColumnIndex].Value != null) { MessageBox.Show(dataGridView1.Rows[e.RowIndex].Cells[e.ColumnIndex].Value.ToString()); } }

希望这能帮到你。

Hope this will help you out.

SelectedRows 将返回 DataGridViewSelectedRowCollection 类型的行集合,而不是 DataGridViewRow 。 所以你可以像这样使用它: SelectedRows will return a collection of rows of type DataGridViewSelectedRowCollection and not DataGridViewRow. So you can use it like this : DataGridViewSelectedRowCollection rows = dataGridView1.SelectedRows; string val =(string)rows[2].Cells["Late_Time"].Value; //I have specified rowIndex as 2 as an example

更多推荐

数据网格视图选择单元格值如何获取它

本文发布于:2023-11-23 04:14:39,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1620047.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:网格   视图   单元格   数据

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!