728x90

combobox.Datasource = DataTable;

로 해서 클래스가 로드될 때 바로 selectedIndex를 주려고 하니 에러가 났다.

combobox.items.count 를 찍어보니 0이 자꾸나옴.

그래서 저 문장 아래에 combobox.BindingContext = new BindingContext()

해주니까 정상적으로 작동...

After binding a list to combobox, its dataSource.Count is 5 but, combobox item count is 0. how can it be?

I'm used to Web programming and this is in Windows Forms. So no combo.DataBind(); method exists.

The problem here is, I'm trying to set the selected item programmatically. Since I don't see the combo.Items collection filled, I cannot set the desired item.


Update

A total update is needed I guess:

  • datasource contains 7 items
  • when bound to combobox, DisplayMember and ValueMember are appropriately implemented
  • after databound, through the gui, I can clearly see the 7 items in the combobox
  • combobox.DataSource.Count = 7 and combobox.Items.Count = 0

So the problem is here; since after databound no items are there in the ItemCollection of combobox; I cannot search for one to match and set the appropriate one.

Here is a image for better understanding (But I'm pretty sure I'm missing sth simple)

screenshot

share|improve this question
1
Had you set the DataTextField and DataValueField accordingly?Andre Calil Aug 6 '12 at 14:22
Or DisplayMember and ValueMember. What datatype is in DataSource?Mr47 Aug 6 '12 at 14:29
Yes, maybe the question is not fully understandable. The databound event is successfully working, I can see my items in the combo through the gui. The problem is, in debug; after the datasource assignment the combobox does not contain something in the Items collection. Like after the render, all of the items are shown but before that nothing there.Beytan Kurt Aug 6 '12 at 20:28
In comboBox (windows form) there is no DataTextField and DataValueField properties. where you found these?Aminul Islam Aug 7 '12 at 9:57
@AminulIslam I updated my vocabulary habit problem and updated the image incase you don't believe me. ThanksBeytan Kurt Aug 7 '12 at 10:39
show 5 more comments

 

1 Answer

up vote 6 down vote accepted

After adding ddl.BindingContext = new BindingContext(); before the BindingSource assignment, everything worked fine.

share|improve this answer
Does not work for me...Nasser Jun 15 at 12:02
add comment
728x90

+ Recent posts