Combobox issue.

S

Support

Hi All,
I am working in VB.NET.
I have a data bound control (combobox) which I want to fill with a certain
field of my table from database ( SQL as backend).
The issue is, I want these values to fill the combo at the index specified
by me, ie, not the usual 0,1,2.... by default. Also, not dataset, but I am
using dataReader.
Can anyone send me the sample code for the same?
Thanks for your help.
 
C

Cor Ligthert

Hi Support,

When you want a *databound to the SQL backend* you need in my opinion the
Datatable and the dataset.

If you want to use the datareader you can use an Ilist class object however
than it is not databound, just an ordianary object bounded to the combobox.

The index of a combobox start always on -1, which means empty and than 0, 1
etc. to its length.

You can of course build your own class that implements ICollection and with
that search to the index, however that is something totally different.

http://support.microsoft.com/default.aspx?scid=kb;en-us;306961

I hope I did understand you somehow?

Cor
 
W

William Ryan eMVP

http://www.devbuzz.com/content/zinc_personal_media_center_pg1.asp
Support said:
Hi All,
I am working in VB.NET.
I have a data bound control (combobox) which I want to fill with a certain
field of my table from database ( SQL as backend).

First off, is this a desktop application or a web application? If it's a
desktop application, then your control won't actually be 'bound' to the
data.
The issue is, I want these values to fill the combo at the index specified
by me, ie, not the usual 0,1,2.... by default.
Use the .Insert Method as opposed to the Add method to stick them somewhere
other than the beginning. In order to do this, you won't be able to use a
DataTable and the traditional DisplayMember, ValueMember approach, you are
going to have to use the dataReader or at least, if you use a DataTable,
iterate through it using Insert where applicable. Insert takes two
parameters, the index's position where you want it to appear and the item
itself. So, if you wanted to start filling it at index 6
//Doing it with a reader
int i = 6;
while rdr.Read(){
comboBox1.Items.Insert(i, rdr.GetString[0]);
i++;
}
//Doing it with a datatable, note you can't use the traditional
DisplayMember, ValueMember approach.
int i = 6;
foreach(DataRow dro in myDataSet.Tables[TableIndex].Rows){
comboBox1.Iterms.Insert(i, dro[0].ToString());
i++;
}

Also, not dataset, but I am
using dataReader.

Good, considering what you are doing, it's the best way. If you don't need
a datatable for exclusive binding reasons and you aren't going to send it
back, you don't need to take the overhead hit associated with a datatable
Can anyone send me the sample code for the same?
See Above. Let me know if you have any questions
For documentation on Insert vs. add
http://msdn.microsoft.com/library/d...wsformscomboboxobjectcollectionclasstopic.asp
Thanks for your help.

--

W.G. Ryan, eMVP

http://forums.devbuzz.com/
http://www.knowdotnet.com/williamryan.html
http://www.msmvps.com/WilliamRyan/
 
S

Support

Hi Cor and William,
Thanks a ton for taking interest in the problem.
I have got the answer to my questions, through your valued opinion.
Regards.


William Ryan eMVP said:
http://www.devbuzz.com/content/zinc_personal_media_center_pg1.asp
Support said:
Hi All,
I am working in VB.NET.
I have a data bound control (combobox) which I want to fill with a certain
field of my table from database ( SQL as backend).

First off, is this a desktop application or a web application? If it's a
desktop application, then your control won't actually be 'bound' to the
data.
The issue is, I want these values to fill the combo at the index specified
by me, ie, not the usual 0,1,2.... by default.
Use the .Insert Method as opposed to the Add method to stick them somewhere
other than the beginning. In order to do this, you won't be able to use a
DataTable and the traditional DisplayMember, ValueMember approach, you are
going to have to use the dataReader or at least, if you use a DataTable,
iterate through it using Insert where applicable. Insert takes two
parameters, the index's position where you want it to appear and the item
itself. So, if you wanted to start filling it at index 6
//Doing it with a reader
int i = 6;
while rdr.Read(){
comboBox1.Items.Insert(i, rdr.GetString[0]);
i++;
}
//Doing it with a datatable, note you can't use the traditional
DisplayMember, ValueMember approach.
int i = 6;
foreach(DataRow dro in myDataSet.Tables[TableIndex].Rows){
comboBox1.Iterms.Insert(i, dro[0].ToString());
i++;
}

Also, not dataset, but I am
using dataReader.

Good, considering what you are doing, it's the best way. If you don't need
a datatable for exclusive binding reasons and you aren't going to send it
back, you don't need to take the overhead hit associated with a datatable
Can anyone send me the sample code for the same?
See Above. Let me know if you have any questions
For documentation on Insert vs. add
http://msdn.microsoft.com/library/d...wsformscomboboxobjectcollectionclasstopic.asp
Thanks for your help.

--

W.G. Ryan, eMVP

http://forums.devbuzz.com/
http://www.knowdotnet.com/williamryan.html
http://www.msmvps.com/WilliamRyan/
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top