views:

102

answers:

2

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

+2  A: 

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.

Payton Byrd
useful answer...Thank you!
gaponte69
+2  A: 

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.

Henk Holterman
perfect and simple :)Thank you...
gaponte69