C#如何从datagridview获取选定的列ID值 [英] C# how to get the selected column ID value from datagridview
本文介绍了C#如何从datagridview获取选定的列ID值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我尝试过的几行
int id = Convert.ToInt32(dataGridView1.SelectedRows [0] .Cells [0] .Value);
int id = Convert.ToInt32(dataGridView1.SelectedCells [0]);
int id = Convert.ToInt32(dataGridView1.Rows [0] .Selected);
解决方案
Int id = Convert.ToInt32(dataGridView1.Rows [dataGridView1.CurrentRow.Index] .Cells [0] .Value);
datagridview1.CurrentRow.Index获取所选行的索引。
I have a DataGridView and users can select columns. I want that selected column to pass the value of the ID attribute. I tried quite a few different ways, but always came back with error. Usual error message - "Index was out of range". Must be non-negative and less than the size of the collection...
few of the lines I tried
int id = Convert.ToInt32(dataGridView1.SelectedRows[0].Cells[0].Value);
int id = Convert.ToInt32(dataGridView1.SelectedCells[0]);
int id = Convert.ToInt32(dataGridView1.Rows[0].Selected);
解决方案
Int id= Convert.ToInt32(dataGridView1.Rows[dataGridView1.CurrentRow.Index].Cells[0].Value);
"datagridview1.CurrentRow.Index" gets the index of selected row.
这篇关于C#如何从datagridview获取选定的列ID值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文