PC Review


Reply
Thread Tools Rate Thread

ComboBox Items

 
 
Wayne Wengert
Guest
Posts: n/a
 
      5th Aug 2003
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 Removed)


 
Reply With Quote
 
 
 
 
Jeffrey Tan[MSFT]
Guest
Posts: n/a
 
      6th Aug 2003

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" <(E-Mail Removed)>
| 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: <(E-Mail Removed)>
| 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 Removed)
|
|
|

 
Reply With Quote
 
Wayne Wengert
Guest
Posts: n/a
 
      6th Aug 2003
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

"Jeffrey Tan[MSFT]" <v-(E-Mail Removed)> wrote in message
news:8VeVIs%(E-Mail Removed)...
>
> 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" <(E-Mail Removed)>
> | 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: <(E-Mail Removed)>
> | 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 Removed)
> |
> |
> |
>



 
Reply With Quote
 
Jeffrey Tan[MSFT]
Guest
Posts: n/a
 
      7th Aug 2003

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" <(E-Mail Removed)>
| References: <(E-Mail Removed)>
<8VeVIs#(E-Mail Removed)>
| 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: <(E-Mail Removed)>
| 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
|
| "Jeffrey Tan[MSFT]" <v-(E-Mail Removed)> wrote in message
| news:8VeVIs%(E-Mail Removed)...
| >
| > 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" <(E-Mail Removed)>
| > | 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: <(E-Mail Removed)>
| > | 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 Removed)
| > |
| > |
| > |
| >
|
|
|

 
Reply With Quote
 
 
 
Reply

Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Combobox items based on choice in previous combobox Slim Slender Microsoft Excel Programming 0 2nd Aug 2011 03:52 AM
ComboBox Items Marius Horak Microsoft Dot NET Framework Forms 5 5th Apr 2007 09:42 AM
ComboBox.Items.Clear() causes the selection of Text inside the edit of the ComboBox Nomasnd Microsoft Dot NET Framework Forms 1 27th Sep 2006 06:32 PM
Combobox items determined by the selection in another combobox =?Utf-8?B?QWxlcmlvbg==?= Microsoft Excel Programming 2 13th Sep 2006 01:07 PM
combobox items Emil Jivishov Microsoft Dot NET Framework Forms 0 22nd Jul 2003 07:22 PM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 01:45 AM.