ComboBox Items

  • Thread starter Thread starter Wayne Wengert
  • Start date Start date
W

Wayne Wengert

I am trying to populate a combobox with a lisy of items. I want the 1st item
to be "Make a Selection" and the following items will be names from a table
in my DB (See code below).

When I run the project, the combox box initially displays an empty
selection. When I click on the drop down arrow I see the "Make a Selection"
followed by the other choices.

How can I move everything up so that the "Make a Selection" is the default
value in the combo box?

Wayne

============= code ================
Dim cn As New OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;Data
Source=c:\Program Files\MyApp\MyData.mdb")

strSQL = "SELECT Name FROM Table1 WHERE (EQ = Yes) Order By Name"

cn.Open() 'Open the connection

Dim cmd As New OleDbCommand(strSQL, cn)

Dim rdr As OleDbDataReader = cmd.ExecuteReader

cboSample.Items.Clear()

cboSample.Items.Add("Make a Selection")

While (rdr.Read())

cboSample.Items.Add(rdr.GetString(0)) ' Name is the only field returned

End While

rdr.Close()
 
Hi Wayne,

The item the combobox initially displays should be set by the combobox's
text property.

Hope this helps.

Jeffrey Tan
Microsoft Online Partner Support
Get Secure! - www.microsoft.com/security
This posting is provided "as is" with no warranties and confers no rights.

--------------------
| From: "Wayne Wengert" <[email protected]>
| Subject: ComboBox Items
| Date: Tue, 5 Aug 2003 15:49:31 -0600
| Lines: 44
| X-Priority: 3
| X-MSMail-Priority: Normal
| X-Newsreader: Microsoft Outlook Express 6.00.2800.1158
| X-MIMEOLE: Produced By Microsoft MimeOLE V6.00.2800.1165
| Message-ID: <[email protected]>
| Newsgroups: microsoft.public.dotnet.general
| NNTP-Posting-Host: skybeam143.skybeam.frii.net 216.17.229.143
| Path:
cpmsftngxa06.phx.gbl!cpmsftngxa10.phx.gbl!TK2MSFTNGXA05.phx.gbl!TK2MSFTNGP08
phx.gbl!TK2MSFTNGP12.phx.gbl
| Xref: cpmsftngxa06.phx.gbl microsoft.public.dotnet.general:103592
| X-Tomcat-NG: microsoft.public.dotnet.general
|
| I am trying to populate a combobox with a lisy of items. I want the 1st
item
| to be "Make a Selection" and the following items will be names from a
table
| in my DB (See code below).
|
| When I run the project, the combox box initially displays an empty
| selection. When I click on the drop down arrow I see the "Make a
Selection"
| followed by the other choices.
|
| How can I move everything up so that the "Make a Selection" is the default
| value in the combo box?
|
| Wayne
|
| ============= code ================
| Dim cn As New OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;Data
| Source=c:\Program Files\MyApp\MyData.mdb")
|
| strSQL = "SELECT Name FROM Table1 WHERE (EQ = Yes) Order By Name"
|
| cn.Open() 'Open the connection
|
| Dim cmd As New OleDbCommand(strSQL, cn)
|
| Dim rdr As OleDbDataReader = cmd.ExecuteReader
|
| cboSample.Items.Clear()
|
| cboSample.Items.Add("Make a Selection")
|
| While (rdr.Read())
|
| cboSample.Items.Add(rdr.GetString(0)) ' Name is the only field returned
|
| End While
|
| rdr.Close()
|
|
| --
| ------------------------------------
| Wayne Wengert
| (e-mail address removed)
|
|
|
 
I gather this does not apply when you bind the combobox to a dataset? The
1st item in the dataset shows in the text?

Wayne
 
Hi Wayne,

I do not quite understand your meanning, but I think you can do like this:

using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
using System.Data .SqlClient;

namespace testcombobox
{
public class Form1 : System.Windows.Forms.Form
{
private System.Windows.Forms.ComboBox comboBox1;
private System.ComponentModel.Container components = null;

public Form1()
{
InitializeComponent();
}


protected override void Dispose( bool disposing )
{
if( disposing )
{
if (components != null)
{
components.Dispose();
}
}
base.Dispose( disposing );
}

#region Windows Form Designer generated code

private void InitializeComponent()
{
this.comboBox1 = new System.Windows.Forms.ComboBox();
this.SuspendLayout();
//
// comboBox1
//
this.comboBox1.Location = new System.Drawing.Point(64, 152);
this.comboBox1.Name = "comboBox1";
this.comboBox1.Size = new System.Drawing.Size(152, 21);
this.comboBox1.TabIndex = 0;
this.comboBox1.Text = "comboBox1";
//
// Form1
//
this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
this.ClientSize = new System.Drawing.Size(292, 266);
this.Controls.Add(this.comboBox1);
this.Name = "Form1";
this.Text = "Form1";
this.Load += new System.EventHandler(this.Form1_Load);
this.ResumeLayout(false);

}
#endregion
static void Main()
{
Application.Run(new Form1());
}

private void Form1_Load(object sender, System.EventArgs e)
{
SqlDataAdapter adapter=new SqlDataAdapter ("select * from titles where
price!=0","server=localhost;database=pubs;uid=sa;pwd=");

DataSet ds=new DataSet ();
adapter.Fill (ds);
comboBox1.Text ="Please select a item";
comboBox1.Items.Clear ();
foreach(DataRow row in ds.Tables [0].Rows)
{
comboBox1.Items .Add (row.ItemArray [0]);
}
}
}
}

I use sqlserver to add data to comboBox and it works well on my machine.

Hope it helps

Jeffrey Tan
Microsoft Online Partner Support
Get Secure! - www.microsoft.com/security
This posting is provided "as is" with no warranties and confers no rights.

--------------------
| From: "Wayne Wengert" <[email protected]>
| References: <[email protected]>
<8VeVIs#[email protected]>
| Subject: Re: ComboBox Items
| Date: Wed, 6 Aug 2003 05:01:45 -0600
| Lines: 91
| X-Priority: 3
| X-MSMail-Priority: Normal
| X-Newsreader: Microsoft Outlook Express 6.00.2800.1158
| X-MIMEOLE: Produced By Microsoft MimeOLE V6.00.2800.1165
| Message-ID: <[email protected]>
| Newsgroups: microsoft.public.dotnet.general
| NNTP-Posting-Host: skybeam143.skybeam.frii.net 216.17.229.143
| Path: cpmsftngxa06.phx.gbl!TK2MSFTNGP08.phx.gbl!TK2MSFTNGP12.phx.gbl
| Xref: cpmsftngxa06.phx.gbl microsoft.public.dotnet.general:103674
| X-Tomcat-NG: microsoft.public.dotnet.general
|
| I gather this does not apply when you bind the combobox to a dataset? The
| 1st item in the dataset shows in the text?
|
| Wayne
|
| | >
| > Hi Wayne,
| >
| > The item the combobox initially displays should be set by the combobox's
| > text property.
| >
| > Hope this helps.
| >
| > Jeffrey Tan
| > Microsoft Online Partner Support
| > Get Secure! - www.microsoft.com/security
| > This posting is provided "as is" with no warranties and confers no
rights.
| >
| > --------------------
| > | From: "Wayne Wengert" <[email protected]>
| > | Subject: ComboBox Items
| > | Date: Tue, 5 Aug 2003 15:49:31 -0600
| > | Lines: 44
| > | X-Priority: 3
| > | X-MSMail-Priority: Normal
| > | X-Newsreader: Microsoft Outlook Express 6.00.2800.1158
| > | X-MIMEOLE: Produced By Microsoft MimeOLE V6.00.2800.1165
| > | Message-ID: <[email protected]>
| > | Newsgroups: microsoft.public.dotnet.general
| > | NNTP-Posting-Host: skybeam143.skybeam.frii.net 216.17.229.143
| > | Path:
| >
|
cpmsftngxa06.phx.gbl!cpmsftngxa10.phx.gbl!TK2MSFTNGXA05.phx.gbl!TK2MSFTNGP08
| > phx.gbl!TK2MSFTNGP12.phx.gbl
| > | Xref: cpmsftngxa06.phx.gbl microsoft.public.dotnet.general:103592
| > | X-Tomcat-NG: microsoft.public.dotnet.general
| > |
| > | I am trying to populate a combobox with a lisy of items. I want the
1st
| > item
| > | to be "Make a Selection" and the following items will be names from a
| > table
| > | in my DB (See code below).
| > |
| > | When I run the project, the combox box initially displays an empty
| > | selection. When I click on the drop down arrow I see the "Make a
| > Selection"
| > | followed by the other choices.
| > |
| > | How can I move everything up so that the "Make a Selection" is the
| default
| > | value in the combo box?
| > |
| > | Wayne
| > |
| > | ============= code ================
| > | Dim cn As New OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;Data
| > | Source=c:\Program Files\MyApp\MyData.mdb")
| > |
| > | strSQL = "SELECT Name FROM Table1 WHERE (EQ = Yes) Order By Name"
| > |
| > | cn.Open() 'Open the connection
| > |
| > | Dim cmd As New OleDbCommand(strSQL, cn)
| > |
| > | Dim rdr As OleDbDataReader = cmd.ExecuteReader
| > |
| > | cboSample.Items.Clear()
| > |
| > | cboSample.Items.Add("Make a Selection")
| > |
| > | While (rdr.Read())
| > |
| > | cboSample.Items.Add(rdr.GetString(0)) ' Name is the only field
returned
| > |
| > | End While
| > |
| > | rdr.Close()
| > |
| > |
| > | --
| > | ------------------------------------
| > | Wayne Wengert
| > | (e-mail address removed)
| > |
| > |
| > |
| >
|
|
|
 
Back
Top