开发者

increase a variable value for +1

开发者 https://www.devze.com 2022-12-21 21:31 出处:网络
From the code below I am getting a cell value from the selected开发者_开发问答 row to a dataGridView:

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


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.


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消