none
ComboBox Help

    Question

  • Hi,

    I am trying to find the datatable with toolstripcombobox.

                this.tscb.ComboBox.DataSource = <My Table>

                this.tscb.ComboBox.DisplayMember = "PARM_NM";
                this.tscb.ComboBox.ValueMember = "PARM_VAL";

    But all the below values are zero. Also i am not able to set selected index as zero for this combobox.

    Also the below value is 2

    Console.WriteLine(((DataTable)this.tscb.ComboBox.DataSource).Rows.Count);

                Console.WriteLine(this.tscb.Items.Count);
                Console.WriteLine(this.tscb.ComboBox.Items.Count);

    Tuesday, April 13, 2010 3:53 PM

All replies

  • Key Point -->"Count"  ,and the "DisplayMember " or "ValueMember " ?

    The focus of ComboBox is "Item" .

    Using AddRange...

    Tuesday, April 13, 2010 6:17 PM
  • Hi Sonthosh,

    are you sure you got the column name right ?
    If you misspelled it the combobox would display ValueMember instead. Make sure that the column PARM_NM exists in your DataTable in dubugger.

    " Also i am not able to set selected index as zero for this combobox."

    The SelectedIndex is 0 when you bind a combobox to the source.
    What do you mean by can't change it to 0 ... which value does it have ?
    Do you get an exception ? 
    Make sure that the source DataTable has been populated at the time you're trying to set the SelectedIndex ?

    Best regards,
    Vladimir

    • Marked as answer by Liliane Teng Tuesday, April 20, 2010 2:37 AM
    • Unmarked as answer by Sonthosh Wednesday, April 21, 2010 12:37 PM
    Tuesday, April 13, 2010 6:23 PM
  • Key Point -->"Count"  ,and the "DisplayMember " or "ValueMember " ?

    The focus of ComboBox is "Item" .

    Using AddRange...

    data binding, DataSource, DisplayMember and ValueMember is a correct way to populate combobox. Items in this case would be a collection of DataRowView instances.

    Tuesday, April 13, 2010 6:28 PM
  • Sorry ! I don't mean that...

    Is Below:

    Console.WriteLine(((DataTable)this.tscb.ComboBox.DataSource).Rows.Count);

    Console.WriteLine(this.tscb.Items.Count);
    Console.WriteLine(this.tscb.ComboBox.Items.Count);

    ///////////////////////////////////

     

    Tuesday, April 13, 2010 7:56 PM
  • I'm sorry but I don't understand your question. Can you try explaining once more ... what do you expect to get and what do you actually get.

    try writing in your own language and translate using "google translate"

    Tuesday, April 13, 2010 9:07 PM
  • Hello Sonthosh,
    Welcome to MSDN Forum.
    I'm sorry I could not understand what you want to do. If possible, Could you please explain your question clearly?
    (1) As what Sonthosh said,he gives you good answer. The SelectedIndex is default 0 when you bind a combobox to the source.You can use this to change its value.
    this.toolStripComboBox1.SelectedIndex =0;
    (2)In addition,
    toolstripcombobox.items which gets a collection of the items contained in this ToolStripComboBox.
    toolstripcombobox.combobox which Gets a ComboBox in which the user can enter text, along with a list from which the user can select.
    (3)For more information, you can see the following links. I hope they are helpful.
    http://msdn.microsoft.com/en-us/library/system.windows.forms.toolstripcombobox.aspx
    http://msdn.microsoft.com/en-us/library/system.windows.forms.toolstripcombobox.items.aspx
    http://msdn.microsoft.com/en-us/library/system.windows.forms.toolstripcombobox.combobox.aspx

    Hope for your reply.
    Wish you a nice day.
    Best regards,
    Liliane


    Please mark the replies as answers if they help and unmark them if they provide no help. Thanks
    • Marked as answer by Liliane Teng Tuesday, April 20, 2010 2:37 AM
    • Unmarked as answer by Sonthosh Wednesday, April 21, 2010 12:38 PM
    Monday, April 19, 2010 3:49 AM
  • Hello Sonthosh,
    In order to facilate the management of the Forum, I marked the useful replies as answers. If you come back and think they provide no help, please unmark them. Thanks.
    Wish you good luck :)
    Best regards,
    Liliane


    Please mark the replies as answers if they help and unmark them if they provide no help. Thanks
    Tuesday, April 20, 2010 2:36 AM
  • Hi Teng,

    The Segment of the code was writtein in the load. If i set the Selected Index = 1or 0 .

     I get InvalidArgument=Value of '0' is not valid for 'SelectedIndex'

    The Column names are not misspelled.

    The DataTable is populated and i can see the rows

    Console.WriteLine(((DataTable)this.tscb.ComboBox.DataSource).Rows.Count); -- Returns 2

    But only the combo box is not populated, so i am getting zero for the below statements.

    Console.WriteLine(this.tscb.Items.Count);
    Console.WriteLine(this.tscb.ComboBox.Items.Count);

    For your information i am dealing with toolstrip combo box.

     

     

    Wednesday, April 21, 2010 12:37 PM
  • Hi Sonthosh, 

    could you please post the code of the Load event handler (all code) where you set the toolstrip combobox and try to set SelectedIndex, exactly as it is, do not omit anything. 

    I can't reproduce the issue so I guess you must be doing something out of the ordinary to get this behavior.

    Oh, yes, and post the part of InitializeComponent method which refers to toolstrip combobox to see how you have set it up in design time.

     

    Wednesday, April 21, 2010 1:56 PM
  • using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Linq;
    using System.Text;
    using System.Windows.Forms;
    namespace ATEST
    {
        public class ATEST : Form
        {
            private System.ComponentModel.IContainer components = null;

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

     private void InitializeComponent()
     {

         this.ds = new System.Data.DataSet();
         this.sCmd = new System.Data.OleDb.OleDbCommand();
         this.conn = new System.Data.OleDb.OleDbConnection();
         this.iCmd = new System.Data.OleDb.OleDbCommand();
         this.uCmd = new System.Data.OleDb.OleDbCommand();
         this.dCmd = new System.Data.OleDb.OleDbCommand();
         this.oAdap = new System.Data.OleDb.OleDbDataAdapter();
         this.toolStrip1 = new System.Windows.Forms.ToolStrip();
         this.cD = new System.Windows.Forms.ToolStripComboBox();
         this.toolStripLabel1 = new System.Windows.Forms.ToolStripLabel();
         this.toolStripSeparator1 = new System.Windows.Forms.ToolStripSeparator();
         ((System.ComponentModel.ISupportInitialize)(this.ds)).BeginInit();
         this.toolStrip1.SuspendLayout();
         this.SuspendLayout();
         //
         // ds
         //
         this.ds.DataSetName = "NewDataSet";
         //
         // sCmd
         //
         this.sCmd.CommandText = "SELECT RUSER,RENV FROM TREQUEST";
         this.sCmd.Connection = this.conn;
         //
         // conn
         //
         this.conn.ConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=test.mdb;Persist " +
             "Security Info=True";
         //
         // iCmd
         //
         this.iCmd.CommandText = "INSERT INTO `TREQUEST` (`RTYPE`, `RUSER`, `RENV`, `RSTATUS`, `RSRC`, `RPTY`, `RHA" +
             "NDLER`, `RDATE`, `CDATE`, `RSLA`) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?)";
         this.iCmd.Connection = this.conn;
         this.iCmd.Parameters.AddRange(new System.Data.OleDb.OleDbParameter[] {
         new System.Data.OleDb.OleDbParameter("RTYPE", System.Data.OleDb.OleDbType.VarWChar, 0, "RTYPE"),
         new System.Data.OleDb.OleDbParameter("RUSER", System.Data.OleDb.OleDbType.VarWChar, 0, "RUSER"),
         new System.Data.OleDb.OleDbParameter("RENV", System.Data.OleDb.OleDbType.VarWChar, 0, "RENV"),
         new System.Data.OleDb.OleDbParameter("RSTATUS", System.Data.OleDb.OleDbType.VarWChar, 0, "RSTATUS"),
         new System.Data.OleDb.OleDbParameter("RSRC", System.Data.OleDb.OleDbType.VarWChar, 0, "RSRC"),
         new System.Data.OleDb.OleDbParameter("RPTY", System.Data.OleDb.OleDbType.VarWChar, 0, "RPTY"),
         new System.Data.OleDb.OleDbParameter("RHANDLER", System.Data.OleDb.OleDbType.VarWChar, 0, "RHANDLER"),
         new System.Data.OleDb.OleDbParameter("RDATE", System.Data.OleDb.OleDbType.VarWChar, 0, "RDATE"),
         new System.Data.OleDb.OleDbParameter("CDATE", System.Data.OleDb.OleDbType.VarWChar, 0, "CDATE"),
         new System.Data.OleDb.OleDbParameter("RSLA", System.Data.OleDb.OleDbType.VarWChar, 0, "RSLA")});
         //
         // oAdap
         //
         this.oAdap.DeleteCommand = this.dCmd;
         this.oAdap.InsertCommand = this.iCmd;
         this.oAdap.SelectCommand = this.sCmd;
         this.oAdap.TableMappings.AddRange(new System.Data.Common.DataTableMapping[] {
         new System.Data.Common.DataTableMapping("Table", "TREQUEST", new System.Data.Common.DataColumnMapping[] {
         new System.Data.Common.DataColumnMapping("ID", "ID"),
         new System.Data.Common.DataColumnMapping("RTYPE", "RTYPE"),
         new System.Data.Common.DataColumnMapping("RUSER", "RUSER"),
         new System.Data.Common.DataColumnMapping("RENV", "RENV"),
         new System.Data.Common.DataColumnMapping("RSTATUS", "RSTATUS"),
         new System.Data.Common.DataColumnMapping("RSRC", "RSRC"),
         new System.Data.Common.DataColumnMapping("RPTY", "RPTY"),
         new System.Data.Common.DataColumnMapping("RHANDLER", "RHANDLER"),
         new System.Data.Common.DataColumnMapping("RDATE", "RDATE"),
         new System.Data.Common.DataColumnMapping("CDATE", "CDATE"),
         new System.Data.Common.DataColumnMapping("RSLA", "RSLA")})});
         this.oAdap.UpdateCommand = this.uCmd;
         //
         // toolStrip1
         //
         this.toolStrip1.Items.AddRange(new System.Windows.Forms.ToolStripItem[] {
         this.toolStripLabel1,
         this.toolStripSeparator1,
         this.cD});
         this.toolStrip1.Location = new System.Drawing.Point(0, 0);
         this.toolStrip1.Name = "toolStrip1";
         this.toolStrip1.Size = new System.Drawing.Size(753, 25);
         this.toolStrip1.TabIndex = 0;
         this.toolStrip1.Text = "toolStrip1";
         //
         // cD
         //
         this.cD.Name = "cD";
         this.cD.Size = new System.Drawing.Size(121, 25);
         //
         // toolStripLabel1
         //
         this.toolStripLabel1.Name = "toolStripLabel1";
         this.toolStripLabel1.Size = new System.Drawing.Size(86, 22);
         this.toolStripLabel1.Text = "toolStripLabel1";
         //
         // toolStripSeparator1
         //
         this.toolStripSeparator1.Name = "toolStripSeparator1";
         this.toolStripSeparator1.Size = new System.Drawing.Size(6, 25);
         //
         // ATEST
         //
         this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
         this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
         this.ClientSize = new System.Drawing.Size(753, 264);
         this.Controls.Add(this.toolStrip1);
         this.Name = "ATEST";
         this.Text = "Form1";
         this.Load += new System.EventHandler(this.ATEST_Load);
         ((System.ComponentModel.ISupportInitialize)(this.ds)).EndInit();
         this.toolStrip1.ResumeLayout(false);
         this.toolStrip1.PerformLayout();
         this.ResumeLayout(false);
         this.PerformLayout();

     }


     private System.Data.DataSet ds;
     private System.Data.OleDb.OleDbCommand sCmd;
     private System.Data.OleDb.OleDbConnection conn;
     private System.Data.OleDb.OleDbCommand iCmd;
     private System.Data.OleDb.OleDbCommand uCmd;
     private System.Data.OleDb.OleDbCommand dCmd;
     private System.Data.OleDb.OleDbDataAdapter oAdap;
     private System.Windows.Forms.ToolStrip toolStrip1;
     private System.Windows.Forms.ToolStripComboBox cD;
     private System.Windows.Forms.ToolStripLabel toolStripLabel1;
            private System.Windows.Forms.ToolStripSeparator toolStripSeparator1;

            public ATEST()
            {
                InitializeComponent();
            }

            [STAThread]
     static void Main()
     {
         Application.EnableVisualStyles();
         Application.SetCompatibleTextRenderingDefault(false);
         Application.Run(new ATEST());
            }
            private void ATEST_Load(object sender, EventArgs e)
            {
                oAdap.Fill(ds);
                cD.ComboBox.DataSource = ds.Tables[0];
                cD.ComboBox.DisplayMember = "RUSER";
                cD.ComboBox.ValueMember = "RENV";
                cD.SelectedIndex = 1;
                //MessageBox.Show(""+cD.Items.Count);
            }

        }
    }

     

    Friday, April 23, 2010 8:46 AM
  • The Same code runs in my personnal machine and does not run in my office machine.....

    Note: Same Version of .NET Framework is being used to compile it

    Friday, April 23, 2010 8:47 AM
  • hmm, from what you posted here everything seems to be OK ... specially if it runs smoothly on one machine.

    I remember seeing some strange behavior once with a plain ComboBox (not toolstrip), which didn't run on one out of 7 machines we tested. The exception was raised somewhere deep inside the framework ... long time ago, it's just a blur now...

    I think the solution (or more likely a hack, since we didn't find out what was  the cause) was to change the order of statements setting DataSource, DisplayMember and ValueMember.

    So, a shot in the dark, but still, might be worth trying it:

     cD.ComboBox.DisplayMember = "RUSER";
     cD.ComboBox.ValueMember = "RENV";
     cD.ComboBox.DataSource = ds.Tables[0];

    or

     cD.ComboBox.ValueMember = "RENV";
     cD.ComboBox.DisplayMember = "RUSER";
     cD.ComboBox.DataSource = ds.Tables[0];

    EDIT: Is it failing in VS debug mode or are you deploying and testing exe ?
             Could you upload a simple project that demonstrates this bug so we could test it ?

    • Edited by Vladimir.Ilic Friday, April 23, 2010 4:18 PM questions
    Friday, April 23, 2010 4:13 PM