PC Review


Reply
Thread Tools Rate Thread

Combo Box first record

 
 
=?Utf-8?B?TWlrZSBM?=
Guest
Posts: n/a
 
      19th Sep 2005
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.

 
Reply With Quote
 
 
 
 
W.G. Ryan MVP
Guest
Posts: n/a
 
      19th Sep 2005
Set the selectedIndex to -1
"Mike L" <(E-Mail Removed)> wrote in message
news:B52DAE48-B798-444C-A1E1-(E-Mail Removed)...
> 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.
>



 
Reply With Quote
 
Jeffrey Tan[MSFT]
Guest
Posts: n/a
 
      20th Sep 2005
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.

 
Reply With Quote
 
=?Utf-8?B?TWlrZSBM?=
Guest
Posts: n/a
 
      20th Sep 2005
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.
>
>

 
Reply With Quote
 
Jeffrey Tan[MSFT]
Guest
Posts: n/a
 
      21st Sep 2005
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.

 
Reply With Quote
 
=?Utf-8?B?TWlrZSBM?=
Guest
Posts: n/a
 
      21st Sep 2005
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.
>
>

 
Reply With Quote
 
=?Utf-8?B?TWlrZSBM?=
Guest
Posts: n/a
 
      22nd Sep 2005
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;uid=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

 
Reply With Quote
 
Jeffrey Tan[MSFT]
Guest
Posts: n/a
 
      23rd Sep 2005
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.

 
Reply With Quote
 
=?Utf-8?B?TWlrZSBM?=
Guest
Posts: n/a
 
      23rd Sep 2005
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]"" 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.
>
>

 
Reply With Quote
 
=?Utf-8?B?TWlrZSBM?=
Guest
Posts: n/a
 
      26th Sep 2005
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

 
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
Record Selectors VS Combo Box Record Selector =?Utf-8?B?RWFybENQaGlsbGlwcw==?= Microsoft Access Form Coding 4 4th Oct 2006 04:58 PM
Populate combo box with record data when record is pulled by another combo box Minikoop Microsoft Access Forms 3 22nd Jul 2006 12:11 AM
Combo 'NotInList' > add record > refresh combo problem Fjordur Microsoft Access Forms 3 27th Jan 2006 09:09 PM
Combo Box, sellecting only the record desired record =?Utf-8?B?SnAzMDA3?= Microsoft Access Forms 1 15th Jul 2005 03:29 PM
Updating a record source of a combo box after creating a new record CQMMAN Microsoft Access 1 16th Dec 2003 07:16 AM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 04:46 PM.