Newby: Filling ComboBox?

  • Thread starter Thread starter Mr. B
  • Start date Start date
M

Mr. B

I'm 'verrrry' new to DB's... never have used them in the past. And I'm a
week-end programmer (ie: for 'fun' rather than work).

But now I'm trying to develope a rather complicated Windows Form using
MSAccess db files. Our company inputs our weekly time sheets into a MSA db...
it then gets imported to another application to process.

Our current DB User interface sucks (no one likes it). So I'm trying to make
one that is more user friendly.

I'm able to connect to the MSA db. And I'm now starting to work on my skills
to get into the DB and all...

What I'm stuck at currently is getting the information out of a column and
into a ComboBox. I've the MS ADO.net book by David Sceppa, but it's a bit
over my head at times.

The information I'd like to get would be (say) project numbers (of course I'll
want the others). But one step at a time.

The DataSet1 has the main Table:
MASTER_JCM_JOB_1
with the following columns
DESCRIPTION <--- Project Name
JOB <--- Project Number
STATUS <--- Project Active (yes/no)

I've a Combox and have done the DataBinding to MASTER_JCM_JOB_1+JOB.

But of course when I run the app, I just get the first line.

What's the trick to fill the combox with JOB? I can get the COUNT of JOB.
But the ComboBox1.Items.Add(NewInfo) has be stumped.

Much appreciated on any help...

(no doubt I'll be back with more <grin>)

Regards,

Bruce
 
I'm not sure I understand the problem....are you getting any values in the
combobox? Also, are you using Databindings or are you iterating through the
table ? If you are binding the to the table, you don't need to mess with
the AddNew unless you want to add something other than what's in the
database.

Anyway, let's say you have a DataTable with each of the fields that you
mention below.

Now, let's just say that the DataSet's name is MyDataSet and the DataTable's
name is myDataTable...

Ok, do the following:

myDataSet.Tables["MyDataTable"].Columns.Add("WhateverName", typeof(string));
myDataSet.Tables["MyDataTable"].Columns["WhateverName"].Expression =
"Master_JC_Job_1 + Description +Job+Status";

//Now you probably want to format the concatenation, but you get the idea.

Ok, now, assuming your combobox's name is myComboBox....do the following

myComboBox.DataSource = myDataSet;
myComboBox.DisplayMember = "MyDataTable.WhateverName";
myComboBox.ValueMember = "MyDataTable.Master_JC_Job_1";


ON the other hand, you could iterate through it using

foreach(DataRow dr in MyDataTable.Rows){
myCombBox.Items.Add(dr(WhateverName).ToString());
}

If you post the code though, we can figure it out for you.

HTH,

Bill
 
William Ryan said:
I'm not sure I understand the problem....are you getting any values in the
combobox? Also, are you using Databindings or are you iterating through the

Yup. I said I only got the first row info.
table ? If you are binding the to the table, you don't need to mess with
the AddNew unless you want to add something other than what's in the

I got the db bind right (I think)
If you post the code though, we can figure it out for you.

Well... it's not much (:

I'll play with your info... probably has what I need (if not...). I don't
mind 100% help. But sometimes, getting me pointed in the right direction is
good enough (sometimes I always look in the wrong places for the answer).

Thanks!

Bruce
 
Are you positive that your query is returning more than one row? You can
determine that by doing something like Debug.Assert(myTable.Rows.Count >
1) ---- if you don't get a big annoying warning message, than you have more
than one row.

So you are definitely using the DataBindings and not the combox.Items.Add?
I know you said you used bindings, I'm just wondering where the add comes
into place. Post your code snippet though even if it's small, and I'll do
my best to help you with it.
 
William Ryan said:
Are you positive that your query is returning more than one row? You can

Nope... only the first row.
determine that by doing something like Debug.Assert(myTable.Rows.Count >
1) ---- if you don't get a big annoying warning message, than you have more
than one row.

BTW.... sorry, but I should have said earlier that I'm using VB.net
(standard)... not C# (:
So you are definitely using the DataBindings and not the combox.Items.Add?
I know you said you used bindings, I'm just wondering where the add comes

Well... that's the point. I'm not sure what I'm doing (: I DO have
databindings to the Combobox. Maybe I shouldn't? And then I'm trying the
Combo.Items.Add stuff... hmmm...
into place. Post your code snippet though even if it's small, and I'll do
my best to help you with it.

Okay... I'll try (hard as I'm playing first with Test Bench code - just so I
can understand things before I put it in my final project...

Got the standard:
OleDbDataAdapter1
OleDbConnection1
DataSet11

Then my DB file (TestDB.mdb). This is a copy of the actual db file (one of 2)
I'm using.
Table name is MASTER_JCM_JOB_1
3 Columns: JOB, DESCRIPTION, STATUS

Then COMBOBOX1

Code wise... (again... VB.net)

(form load)
OleDbDataAdapter1.Fill(DataSet11)

That's about it 'cause nothing I've done yet has worked. But I've tried this
w/o success (looked too simple to work):

Dim Jobcnt As Integer
Dim cntr As Integer
Dim NewInfo As String

Jobcnt = Me.BindingContext(DataSet11, "MASTER_JCM_JOB_1").Count
For cntr = 0 To ttlcnt
NewInfo = Me.BindingContext(DataSet11, "MASTER_JCM_JOB_1").Position = cntr
ComboBox1.Items.Add(NewInfo)
Next

Regards,

Bruce
 
William Ryan said:
Are you positive that your query is returning more than one row? You can
determine that by doing something like Debug.Assert(myTable.Rows.Count >
1) ---- if you don't get a big annoying warning message, than you have more
than one row.

Well William...

Finally figured it out (isn't that always the case... yell for help... the
figure it all out) (:

By simply using the ComboBox Properties (DataSource and DisplayMember), the
Combobox gets loaded up.

I even found out (much to my surprise) that if you have a 2nd Combobox (ie:
Box #1 is Project Numbers... Box #2 is Project Names)... and that they are
both from the same DataSource... they are in Synch with each other (bonus).

But... what would be 'nice' is to know how to filter out some lines in the
Combo boxes (not critical... but would be a nice touch).

There is a column (STATUS) defines if a project is active or not. ie: the
items in this column have either CLOSED, IN PROGRESS or UNSTARTED.

So IF I can only show those having IN PROGRESS status would be nice. Gotta
see if there is something on FILTERS in the millions of Help books I have (:

Regards,

Bruce
 
Back
Top