增加 +1 的变量值
从下面的代码中,我从所选行获取一个单元格值到 dataGridView:
string Kategoria_ = dataGridView1.CurrentRow.Cells[1].Value.ToString();
然后我需要转换为 int:
int category = Convert.ToInt32(Kategoria_);
并将这个数字作为组合框的索引:
cmbCategory2.SelectedIndex = category;
问题是我正在使用这种查询:
SELECT '0' b, ' All' a union select kategori_id b, kategori_emri a from tbl_kategoria order by a
和我总是有+1索引,所以我没有得到真正的索引(或使用-1索引号),因为已经为“全部”值保留了0?!
因此,从组合框中选择的项目没有正确的索引号!
From the code below I am getting a cell value from the selected row to a dataGridView:
string Kategoria_ = dataGridView1.CurrentRow.Cells[1].Value.ToString();
Then I need to convert to int:
int category = Convert.ToInt32(Kategoria_);
And putting this number as an index to the combobox:
cmbCategory2.SelectedIndex = category;
Issue is that I'm using this kind of queries:
SELECT '0' b, ' All' a union select kategori_id b, kategori_emri a from tbl_kategoria order by a
and always I have +1 index, so I am not getting the true index (or to use -1 index number), because already 0 is reserved for 'All' value ?!
So, the selected item from the combobox has no right index number !!!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
只需使用 SelectedValue 而不是 SelectedIndex。这也将允许 Id 范围和其他排序中的间隙。
通过设置 ValueMember 和 DisplayMember(或 ASP.NET 中的 DataValueSomething)属性来配置 ComboBox。
Just use SelectedValue instead of SelectedIndex. That will allow for gaps in the Id-range ad other sortings as well.
Configure the ComboBox by setting ValueMember and DisplayMember (or DataValueSomething in ASP.NET) properties.
您的数据是否从 1 开始,每行递增 1?如果没有,您将永远无法使 SelectedIndex 对齐。您应该在数据绑定中设置 SelectedValue 属性并将值字段定义为“b”。
Does your data start with 1 and increment by 1 for each row? If not, you'll never get your SelectedIndex to line up. You should be setting the SelectedValue property and and defining the Value Field as "b" in your databinding.