c# - 将变量值增加+1

标签 c# winforms combobox selectedindex

从下面的代码中,我从所选行获取一个单元格值到 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?!

所以,从组合框中选择的项目没有正确的索引号!!!

最佳答案

只需使用 SelectedValue 而不是 SelectedIndex。这也将允许 ID 范围广告与其他排序之间存在间隙。

通过设置 ValueMember 和 DisplayMember(或 ASP.NET 中的 DataValueSomething)属性来配置 ComboBox。

关于c# - 将变量值增加+1,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2345078/

相关文章:

winforms - Visual Studio 设计器向我的表单添加了边距和填充

php - 如何在编辑表单中填充组合框

python - 复选框/组合框的 Tkinter 下拉列表

C# 计算目录大小

.net - 将.NET 1.1 WinForm/Service升级到什么?

c# - MongoDb C# GeoNear 查询构造

c# - 如何使用 WinForms 中的 Web 浏览器控件访问 HTML 文档的 <HEAD> 元素?

extjs - Rally App SDK 2.0:初始创建后无法修改QueryFilter对象

C# 林奇 : What is the difference between a Pull model and a Push model?

c# - 镜像堆积柱形图 - Javascript/C#?