C#如何从datagridview获取选定的列ID值 [英] C# how to get the selected column ID value from datagridview

查看:985
本文介绍了C#如何从datagridview获取选定的列ID值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆