将所选dataGridView中的数据转换为字符串(Getting data from selected dataGridView to string)

编程入门 行业动态 更新时间:2024-10-28 03:22:05
将所选dataGridView中的数据转换为字符串(Getting data from selected dataGridView to string)

所以我想从dataGridView中提取一个选定的数据到字符串变量,但我似乎在代码中的某个地方出了问题。 我该怎么做? 以下是我写的内容,但似乎不对。

if ( dataGridView1.SelectedRows.Count != null) { string itemid = dataGridView1.SelectedRows[0].Cells[0].Value.ToString(); string name = dataGridView1.SelectedRows[0].Cells[1].Value.ToString(); string description = dataGridView1.SelectedRows[0].Cells[2].Value.ToString(); }

So I'd like to extract a selected data from dataGridView to string variable but I seem to have gone wrong somewhere in the code. How do I go about this? Below is what I'd written but it doesn't seem to be right.

if ( dataGridView1.SelectedRows.Count != null) { string itemid = dataGridView1.SelectedRows[0].Cells[0].Value.ToString(); string name = dataGridView1.SelectedRows[0].Cells[1].Value.ToString(); string description = dataGridView1.SelectedRows[0].Cells[2].Value.ToString(); }

最满意答案

试试这种方式。 确保更改属性Multiselect = false和SelectionMode = FullRowSelect以确保只获得一个选择。

SelectedRows.Count始终为0,直到您选择一个

if ( dataGridView1.SelectedRows.Count > 0) { string itemid = dataGridView1.SelectedRows[0].Cells[0].Value.ToString(); string name = dataGridView1.SelectedRows[0].Cells[1].Value.ToString(); string description = dataGridView1.SelectedRows[0].Cells[2].Value.ToString(); }

Try this way. Make sure change the property Multiselect = false and SelectionMode = FullRowSelect to ensure gets only one selection.

SelectedRows.Count is always 0 until you select one

if ( dataGridView1.SelectedRows.Count > 0) { string itemid = dataGridView1.SelectedRows[0].Cells[0].Value.ToString(); string name = dataGridView1.SelectedRows[0].Cells[1].Value.ToString(); string description = dataGridView1.SelectedRows[0].Cells[2].Value.ToString(); }

更多推荐

本文发布于:2023-08-04 21:29:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1421425.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:字符串   转换为   所选   数据   dataGridView

发布评论

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

>www.elefans.com

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