增加 +1 的变量值

发布于 2024-08-23 07:14:34 字数 556 浏览 3 评论 0原文

从下面的代码中,我从所选行获取一个单元格值到 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

节枝 2024-08-30 07:14:34

只需使用 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.

命比纸薄 2024-08-30 07:14:34

您的数据是否从 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文