Search code examples
c#winformscomboboxselectedindex

increase a variable value for +1


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 !!!


Solution

  • 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.