Combo Box first record

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

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.
 
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.
 
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 = "";
}
}
 
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.
 
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;
}
}
}

}
 
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.
 
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.
 
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.dgPrivileges)).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.AppSettings["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]" said:
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.
 
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.
 
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.
 

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

Similar Threads


Back
Top