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