将所选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(); }更多推荐
发布评论