gpt4 book ai didi

C#如何从datagridview中获取选中列的ID值

转载 作者:行者123 更新时间:2023-11-30 21:52:31 25 4
gpt4 key购买 nike

我有一个 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"获取所选行的索引。

关于C#如何从datagridview中获取选中列的ID值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34704959/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com