C# 增加+;1.

C# 增加+;1.,c#,winforms,combobox,selectedindex,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,

从下面的代码中,我获得了从选定行到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索引号),因为已经为'All'值保留了0


因此,组合框中的选定项没有正确的索引号

数据是否从1开始,每行递增1?否则,您将永远无法让您的SelectedIndex对齐。您应该设置SelectedValue属性,并在数据绑定中将值字段定义为“b”。

只需使用SelectedValue而不是SelectedIndex。这将允许Id范围和其他排序之间存在间隙


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

请重新设置代码部分的格式,使其更具可读性。