473,386 Members | 1,674 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

Combo Box first record

This is for a Win form.

Currently, the first record is shown in the combo box.

I want the combo box to be blank and when the user clicks on the down arrow
to choose from the list and clicks on a item in the list then that item is
shown in the combo box.

Nov 17 '05 #1
10 4264
Set the selectedIndex to -1
"Mike L" <Ca***@nospam.nospam> wrote in message
news:B5**********************************@microsof t.com...
This is for a Win form.

Currently, the first record is shown in the combo box.

I want the combo box to be blank and when the user clicks on the down
arrow
to choose from the list and clicks on a item in the list then that item is
shown in the combo box.

Nov 17 '05 #2
Hi Cadel,

Thanks for your post.

Can you provide me some code snippet to reproduce our your problem? Based
on my test, defaultly, combobox.SelectedIndex property will have value of
-1. And when the applicaiton startup, combobox will not display the first
record, it will just display what we set in the combobox.Text property.

We can just set combobox.Text to empty string to show up blank in combobox
when application startup.

Hope this helps.

Best regards,
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.

Nov 17 '05 #3
I set the combo box text to empty string, but same problem. Here is my code.

string sProc = "prGet_LicenseCode";
using (SqlConnection oCn = new SqlConnection(sConnString))
{
using (SqlCommand oCmd = new SqlCommand(sProc, oCn))
{
oCn.Open();
oCmd.CommandType = CommandType.StoredProcedure;

oCmd.Parameters.Add("@sLicenseYear", SqlDbType.NChar, 6);
oCmd.Parameters["@sLicenseYear"].Value = txtLicYear.Text;

SqlDataAdapter oDa = new SqlDataAdapter();

oDa.SelectCommand = oCmd;
DataSet ds=new DataSet();
oDa.Fill(ds);

int numTables = ds.Tables.Count;
//No table no records.
if (numTables < 1)
{
MessageBox.Show("No License Codes found for that year.", "No record
found", MessageBoxButtons.OK);
}
else
{
cboPrivilege.DataSource = ds.Tables[0];
cboPrivilege.DisplayMember = "LICENSE_CODE";
cboPrivilege.Text = "";
}
}
""Jeffrey Tan[MSFT]"" wrote:
Hi Cadel,

Thanks for your post.

Can you provide me some code snippet to reproduce our your problem? Based
on my test, defaultly, combobox.SelectedIndex property will have value of
-1. And when the applicaiton startup, combobox will not display the first
record, it will just display what we set in the combobox.Text property.

We can just set combobox.Text to empty string to show up blank in combobox
when application startup.

Hope this helps.

Best regards,
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.

Nov 17 '05 #4
Hi Cadel,

Thanks for your feedback.

Oh, I am not aware of that your combobox is doing databinding. In
databinding, when you set datasource, the winform databinding code will
internally set ComboBox.SelectedIndex to 0, that is display the first item
in the list. So to workaround this behavior, we should take "W.G. Ryan
MVP"'s suggestion to explicitly set ComboBox.SelectedIndex to -1 after the
databinding related code. Sample code like this:

private void Form1_Load(object sender, System.EventArgs e)
{
DataTable dt=new DataTable();

dt.Columns.Add(new DataColumn("column1", typeof(int)));
dt.Columns.Add(new DataColumn("column2", typeof(string)));
dt.Columns.Add(new DataColumn("column3", typeof(bool)));
for(int i=0;i<5;i++)
{
DataRow dr=dt.NewRow();
dr["column1"]=i;
dr["column2"]="item"+i.ToString();
dr["column3"]=i%2==0?true:false;
dt.Rows.Add(dr);
}

this.comboBox1.DataSource=dt;
this.comboBox1.DisplayMember="column2";
this.comboBox1.ValueMember="column1";
this.comboBox1.SelectedIndex=-1;
}

This works well on my side. Hope this helps.

Best regards,
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.

Nov 17 '05 #5
My combo box is databinded. cboPrivilege.DataSource = ds.Tables[0];
Still shows first record for me. I'm using a SP, I think that is why your
cboPrivilege.SelectedIndex = -1; doesn't work for me but works for you
because you create a table.

Here is my current code.
private void txtLicYear_TextChanged(object sender, System.EventArgs e)
{
string sProc = "prGet_LicenseCode";
using (SqlConnection oCn = new SqlConnection(sConnString))
{
using (SqlCommand oCmd = new SqlCommand(sProc, oCn))
{
oCn.Open();
oCmd.CommandType = CommandType.StoredProcedure;

oCmd.Parameters.Add("@sLicenseYear", SqlDbType.NChar, 6);
oCmd.Parameters["@sLicenseYear"].Value = txtLicYear.Text;

SqlDataAdapter oDa = new SqlDataAdapter();

oDa.SelectCommand = oCmd;
DataSet ds=new DataSet();
oDa.Fill(ds);

int numTables = ds.Tables.Count;
//No table no records.
if (numTables < 1)
{
MessageBox.Show("No License Codes found for that year.", "No record
found", MessageBoxButtons.OK);
}
else
{
cboPrivilege.DataSource = ds.Tables[0];
cboPrivilege.DisplayMember = "LICENSE_CODE";
cboPrivilege.ValueMember = "SALES_REV_KEY";
cboPrivilege.SelectedIndex = -1;
}
}
}

}

""Jeffrey Tan[MSFT]"" wrote:
Hi Cadel,

Thanks for your feedback.

Oh, I am not aware of that your combobox is doing databinding. In
databinding, when you set datasource, the winform databinding code will
internally set ComboBox.SelectedIndex to 0, that is display the first item
in the list. So to workaround this behavior, we should take "W.G. Ryan
MVP"'s suggestion to explicitly set ComboBox.SelectedIndex to -1 after the
databinding related code. Sample code like this:

private void Form1_Load(object sender, System.EventArgs e)
{
DataTable dt=new DataTable();

dt.Columns.Add(new DataColumn("column1", typeof(int)));
dt.Columns.Add(new DataColumn("column2", typeof(string)));
dt.Columns.Add(new DataColumn("column3", typeof(bool)));
for(int i=0;i<5;i++)
{
DataRow dr=dt.NewRow();
dr["column1"]=i;
dr["column2"]="item"+i.ToString();
dr["column3"]=i%2==0?true:false;
dt.Rows.Add(dr);
}

this.comboBox1.DataSource=dt;
this.comboBox1.DisplayMember="column2";
this.comboBox1.ValueMember="column1";
this.comboBox1.SelectedIndex=-1;
}

This works well on my side. Hope this helps.

Best regards,
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.

Nov 17 '05 #6
If the combo box is the first object with focus then it works. Put a text
box on the win form, and the text box to have first focus. When the form
loads the focus will be on the text box and the combo box will show the first
record.

""Jeffrey Tan[MSFT]"" wrote:
Hi Cadel,

Thanks for your feedback.

No, this should not take any difference. Our databinding problem occurs
only with ComboBox and DataSet/DataTable, it should have nothing to do with
the database side. Also, I have created a sample with a customized stored
procedure "mytest", and based on my test it works well on my side. Code
listed below:

//mytest stored procedure
CREATE PROCEDURE dbo.mytest AS select * from employees
GO

//.Net code
private void Form1_Load(object sender, System.EventArgs e)
{
string sProc = "mytest";
string sConnString="server=localhost;database=northwind;u id=sa;pwd=test";
using(SqlConnection oCn = new SqlConnection(sConnString))
{
using(SqlCommand oCmd = new SqlCommand(sProc, oCn))
{
oCn.Open();
oCmd.CommandType = CommandType.StoredProcedure;

SqlDataAdapter oDa = new SqlDataAdapter();

oDa.SelectCommand = oCmd;
DataSet ds=new DataSet();
oDa.Fill(ds);

int numTables = ds.Tables.Count;
//No table no records.
if (numTables < 1)
{
MessageBox.Show("No License Codes found for that year.", "No record
found", MessageBoxButtons.OK);
}
else
{
cboPrivilege.DataSource = ds.Tables[0];
cboPrivilege.DisplayMember = "LastName";
cboPrivilege.ValueMember = "EmployeeID";
cboPrivilege.SelectedIndex = -1;
}
}
}
}

I have also attached the sample project in this reply. Hope this helps.

Best regards,
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

Nov 17 '05 #7
Hi Cadel,

Thanks for your feedback.

I am not sure how to get this conclusion. Have you tried the sample project
I attached in last reply? In that sample project, I try to add a new
TextBox, and set the TabIndex so that it receive the focus first. When
running the project, my combobox will not display the first record at all,
it will display a empty entry.

Can you provide some more information to help us reproduce your problem?
Thanks

Best regards,
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.

Nov 17 '05 #8
I didn't see any attachments on your first 9/22/05 post.

Here is my SP and code.

CREATE PROCEDURE dbo.mytest AS select top 10 * from sportsman
GO

// frmDataEntry
//
this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
this.BackColor = System.Drawing.Color.LightSteelBlue;
this.ClientSize = new System.Drawing.Size(800, 816);
this.ControlBox = false;
this.Controls.Add(this.cmdClear);
this.Controls.Add(this.cmdCompleted);
this.Controls.Add(this.GroupBox3);
this.Controls.Add(this.grpSportsman);
this.Controls.Add(this.grpSearchDealerNum);
this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.None;
this.Icon = ((System.Drawing.Icon)(resources.GetObject("$this. Icon")));
this.MaximizeBox = false;
this.MinimizeBox = false;
this.Name = "frmDataEntry";
this.Text = "Dealer Sales";
this.Load += new System.EventHandler(this.frmDataEntry_Load);
this.GroupBox3.ResumeLayout(false);
((System.ComponentModel.ISupportInitialize)(this.d gPrivileges)).EndInit();
this.grpSportsman.ResumeLayout(false);
this.GroupBox2.ResumeLayout(false);
this.grpSearchDealerNum.ResumeLayout(false);
this.ResumeLayout(false);

private void txtLicYear_TextChanged(object sender, System.EventArgs e)
{
string sConnString =
System.Configuration.ConfigurationSettings.AppSett ings["dsn"];
string sProc = "prGet_LicenseCode";
using (SqlConnection oCn = new SqlConnection(sConnString))
{
using (SqlCommand oCmd = new SqlCommand(sProc, oCn))
{
oCn.Open();
oCmd.CommandType = CommandType.StoredProcedure;

oCmd.Parameters.Add("@sLicenseYear", SqlDbType.NChar, 6);
oCmd.Parameters["@sLicenseYear"].Value = txtLicYear.Text;

SqlDataAdapter oDa = new SqlDataAdapter();

oDa.SelectCommand = oCmd;
DataSet ds=new DataSet();
oDa.Fill(ds);

int numTables = ds.Tables.Count;
//No table no records.
if (numTables < 1)
{
MessageBox.Show("No License Codes found for that year.", "No record
found", MessageBoxButtons.OK);
}
else
{
cboPrivilege.DataSource = ds.Tables[0];
cboPrivilege.DisplayMember = "LICENSE_CODE";
cboPrivilege.ValueMember = "SALES_REV_KEY";
cboPrivilege.SelectedIndex = -1;

DataTable dtGrid = ds.Tables[0].Clone();
dgPrivileges.DataSource = dtGrid;

}
}
}

}



""Jeffrey Tan[MSFT]"" wrote:
Hi Cadel,

Thanks for your feedback.

I am not sure how to get this conclusion. Have you tried the sample project
I attached in last reply? In that sample project, I try to add a new
TextBox, and set the TabIndex so that it receive the focus first. When
running the project, my combobox will not display the first record at all,
it will display a empty entry.

Can you provide some more information to help us reproduce your problem?
Thanks

Best regards,
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.

Nov 17 '05 #9
I don't know why the combo box always shows the first record, but I'm going
to live with it for now.

Thanks for trying.
""Jeffrey Tan[MSFT]"" wrote:
Hi Cadel,

Thanks for your feedback.

I can see the attachment in my 9/22/05 post without any problem. We should
can get it from Outlook Express(can not get it from IE).

Also, I have created another sample project to try your new sample code.
First, you only pasted part of the controls code on the form, it missed the
creation code for cmdClear, cmdCompleted, GroupBox3, grpSportsman etc...
Anyway, it seems these controls are not crtical for the project, so I just
ignore your controls code, just drag txtLicYear(TextBox),
cboPrivilege(ComboBox), and dgPrivileges(DataGrid) to the form for
databinding test.

Second, your stored procedure code created a SP named "mytest", but the
ADO.net code invoked the SP "prGet_LicenseCode", can you be more carefully
or give more explanation to your posting? This will save us a lot of time.
Thanks

At last, based on my test, this code also works well on my side with an
emptry entry in the cboPrivilege. IMO, this code should have no much
difference from the code I provided in 9/22/05, yes? I can not see any key
diffference. Also, I have attached my further sample project in this reply,
for your information.(Note: because there is no "sportsman" table in my
northwindow database, I just change it to "employees" table, like this:
CREATE PROCEDURE dbo.mytest AS select top 10 * from employees
GO)

Also, I am not sure why you call DataTable.Clone method in the code. This
method only copies the schema information, not the actual datatable
content. So the datagrid will display an empty grid.

DataTable dtGrid = ds.Tables[0].Clone();
dgPrivileges.DataSource = dtGrid;
================================================== =======
Thank you for your patience and cooperation. If you have any questions or
concerns, please feel free to post it in the group. I am standing by to be
of assistance.

Best regards,
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

Nov 17 '05 #10
Hi Cadel,

Have you tried my sample project? It runs well on my side. If it does not
work on your side, I suggest you try it on another machine, maybe this
issue is machine-specific.

Thanks

Best regards,
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.

Nov 17 '05 #11

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

7
by: Megan | last post by:
Hi everybody- I inherited a database that somehow uses a bound combo box as a record selector. Let me give you some background. The form is based on data from 2 tables. The first table, Person,...
3
by: Karl Roes | last post by:
Hi All, I have a form with a standard combo box and 5 tabs in A2003. On Form Open, I set Me.Detail.Visible = False and in the after update event of the combo set Me.Detail.Visible = True,( to...
8
by: Lyn | last post by:
Hi, Can anyone tell me how the initial value displayed in Combo Box is determined when a form is opened? I am loading the dropdown from one field ("CategoryName") of a table, with "ORDER BY ". ...
1
by: James | last post by:
I am used to VB6 but need to develop something in .Net. I need to create several bound combo-boxes which will use lookup tables to get their values. I created a form using the dataform wizard....
14
by: Kevin | last post by:
A couple of easy questions here hopefully. I've been working on two different database projects which make use of multiple forms. 1. Where's the best/recommended placement for command buttons...
9
by: Edwinah63 | last post by:
Hi everyone, Please let there be someone out there who can help. I have two BOUND combo boxes on a continuous form, the second being dependent on the first. I have no problem getting the...
2
by: jim | last post by:
I have created a Table that has 13 fields and 2 Primary Keys, e.g. 60 1, 60 2, ... 60 28, 61 1, 61 2, ... 61 28, etc... I want to create a Form where I can input the Primary Key values to query...
8
by: AA Arens | last post by:
Hi I do have a products table and products-parts table in my Access 2003 database and log all services into a form. I do have at least the following two combo boxes on my form: - Choose...
2
by: lottaviano | last post by:
I have a form with two combo boxes. The value chosen in the first combo box (cbo1) is supposed to change the values that appear in the second combo box (cbo2). This currently works great for the...
2
by: dancole42 | last post by:
So I have a subform that lists the various items on a particular invoice. First, the user selects a product class from the Class dropdown. This is bound to the Class field in the InvioceLines...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.