Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. how to fill a combobox with a class

how to fill a combobox with a class

Scheduled Pinned Locked Moved C#
questiondatabasetutorial
11 Posts 6 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • A Offline
    A Offline
    adrian salas
    wrote on last edited by
    #1

    Hi, I am filling a combobox with the code below: private void frmCrearCooperativa_Load(object sender, EventArgs e) { string nacionalidad = "select * from cs_nacionalidad"; try { //open connection bdConex.OpenConnection(); //create command and assign the query and connection from the constructor MySqlDataAdapter da = new MySqlDataAdapter(nacionalidad, bdConex.conn); //create dataset DataSet ds = new DataSet(); // filling dataset da.Fill(ds, "cs_nacionalidad"); //dataset values cboNacionalidad.DataSource = ds.Tables["cs_nacionalidad"]; cboNacionalidad.DisplayMember = "nm_nacionalidad"; cboNacionalidad.ValueMember = "cd_nacionalidad"; //close connection bdConex.CloseConnection(); } catch (Exception ex) { MessageBox.Show(ex.Message); } how do I can do a class that fills the combobox with this code? because I have to write it down in all forms. thanks.

    G S W L M 5 Replies Last reply
    0
    • A adrian salas

      Hi, I am filling a combobox with the code below: private void frmCrearCooperativa_Load(object sender, EventArgs e) { string nacionalidad = "select * from cs_nacionalidad"; try { //open connection bdConex.OpenConnection(); //create command and assign the query and connection from the constructor MySqlDataAdapter da = new MySqlDataAdapter(nacionalidad, bdConex.conn); //create dataset DataSet ds = new DataSet(); // filling dataset da.Fill(ds, "cs_nacionalidad"); //dataset values cboNacionalidad.DataSource = ds.Tables["cs_nacionalidad"]; cboNacionalidad.DisplayMember = "nm_nacionalidad"; cboNacionalidad.ValueMember = "cd_nacionalidad"; //close connection bdConex.CloseConnection(); } catch (Exception ex) { MessageBox.Show(ex.Message); } how do I can do a class that fills the combobox with this code? because I have to write it down in all forms. thanks.

      G Offline
      G Offline
      gavindon
      wrote on last edited by
      #2

      I dont have your answer but, If you would like the guys who know more than me to answer you might want to edit your post and put the code in a code block

      code here

      by using pre tags to make it easier to read.

      Programming is a race between programmers trying to build bigger and better idiot proof programs, and the universe trying to build bigger and better idiots, so far... the universe is winning.

      1 Reply Last reply
      0
      • A adrian salas

        Hi, I am filling a combobox with the code below: private void frmCrearCooperativa_Load(object sender, EventArgs e) { string nacionalidad = "select * from cs_nacionalidad"; try { //open connection bdConex.OpenConnection(); //create command and assign the query and connection from the constructor MySqlDataAdapter da = new MySqlDataAdapter(nacionalidad, bdConex.conn); //create dataset DataSet ds = new DataSet(); // filling dataset da.Fill(ds, "cs_nacionalidad"); //dataset values cboNacionalidad.DataSource = ds.Tables["cs_nacionalidad"]; cboNacionalidad.DisplayMember = "nm_nacionalidad"; cboNacionalidad.ValueMember = "cd_nacionalidad"; //close connection bdConex.CloseConnection(); } catch (Exception ex) { MessageBox.Show(ex.Message); } how do I can do a class that fills the combobox with this code? because I have to write it down in all forms. thanks.

        S Offline
        S Offline
        sanforjackass
        wrote on last edited by
        #3

        you only need to pass dataset object

        EASY COME EASY GO

        1 Reply Last reply
        0
        • A adrian salas

          Hi, I am filling a combobox with the code below: private void frmCrearCooperativa_Load(object sender, EventArgs e) { string nacionalidad = "select * from cs_nacionalidad"; try { //open connection bdConex.OpenConnection(); //create command and assign the query and connection from the constructor MySqlDataAdapter da = new MySqlDataAdapter(nacionalidad, bdConex.conn); //create dataset DataSet ds = new DataSet(); // filling dataset da.Fill(ds, "cs_nacionalidad"); //dataset values cboNacionalidad.DataSource = ds.Tables["cs_nacionalidad"]; cboNacionalidad.DisplayMember = "nm_nacionalidad"; cboNacionalidad.ValueMember = "cd_nacionalidad"; //close connection bdConex.CloseConnection(); } catch (Exception ex) { MessageBox.Show(ex.Message); } how do I can do a class that fills the combobox with this code? because I have to write it down in all forms. thanks.

          W Offline
          W Offline
          walterhevedeich
          wrote on last edited by
          #4

          You can just create a method that accepts a combobox as parameter. and then inside your method, just do the usual setting of the datasource to your combobox, just like what you are doing in your code. Im not sure though if your data source for your combo boxes will be the same. If it isn't, you might want to add parameters for your datasource, displaymember, and valuemember to your method.

          Ignorance of the ability brings disability.

          1 Reply Last reply
          0
          • A adrian salas

            Hi, I am filling a combobox with the code below: private void frmCrearCooperativa_Load(object sender, EventArgs e) { string nacionalidad = "select * from cs_nacionalidad"; try { //open connection bdConex.OpenConnection(); //create command and assign the query and connection from the constructor MySqlDataAdapter da = new MySqlDataAdapter(nacionalidad, bdConex.conn); //create dataset DataSet ds = new DataSet(); // filling dataset da.Fill(ds, "cs_nacionalidad"); //dataset values cboNacionalidad.DataSource = ds.Tables["cs_nacionalidad"]; cboNacionalidad.DisplayMember = "nm_nacionalidad"; cboNacionalidad.ValueMember = "cd_nacionalidad"; //close connection bdConex.CloseConnection(); } catch (Exception ex) { MessageBox.Show(ex.Message); } how do I can do a class that fills the combobox with this code? because I have to write it down in all forms. thanks.

            L Offline
            L Offline
            Lukasz Nowakowski
            wrote on last edited by
            #5

            You can create a class, that derives from ComboBox, and there you do

            protected override void OnLoad(EventArgs e)
            {
            string nacionalidad = "select * from cs_nacionalidad";
            try
            {
            //open connection
            bdConex.OpenConnection();
            //create command and assign the query and connection from the constructor
            MySqlDataAdapter da = new MySqlDataAdapter(nacionalidad, bdConex.conn);
            //create dataset
            DataSet ds = new DataSet();
            // filling dataset
            da.Fill(ds, "cs_nacionalidad");
            //dataset values
            cboNacionalidad.DataSource = ds.Tables["cs_nacionalidad"];
            cboNacionalidad.DisplayMember = "nm_nacionalidad";
            cboNacionalidad.ValueMember = "cd_nacionalidad";
            //close connection
            bdConex.CloseConnection();
            }
            catch (Exception ex)
            {
            MessageBox.Show(ex.Message);
            }
            }

            And then use this class, where you want ComboBox to contain this data.

            Don't forget to rate answer, that helped you. It will allow other people find their answers faster.

            A 1 Reply Last reply
            0
            • A adrian salas

              Hi, I am filling a combobox with the code below: private void frmCrearCooperativa_Load(object sender, EventArgs e) { string nacionalidad = "select * from cs_nacionalidad"; try { //open connection bdConex.OpenConnection(); //create command and assign the query and connection from the constructor MySqlDataAdapter da = new MySqlDataAdapter(nacionalidad, bdConex.conn); //create dataset DataSet ds = new DataSet(); // filling dataset da.Fill(ds, "cs_nacionalidad"); //dataset values cboNacionalidad.DataSource = ds.Tables["cs_nacionalidad"]; cboNacionalidad.DisplayMember = "nm_nacionalidad"; cboNacionalidad.ValueMember = "cd_nacionalidad"; //close connection bdConex.CloseConnection(); } catch (Exception ex) { MessageBox.Show(ex.Message); } how do I can do a class that fills the combobox with this code? because I have to write it down in all forms. thanks.

              M Offline
              M Offline
              minnie mouse
              wrote on last edited by
              #6

              I think that the answer that you are looking for is that you must add anther class to your project, create a method within that class using your curent code. You then re use this wherever you like by adding a reference to the new class at the top of each form. have a look at what refactoring does. Instances of the class can then be created and its methods etc can be used. Make sense ?

              1 Reply Last reply
              0
              • L Lukasz Nowakowski

                You can create a class, that derives from ComboBox, and there you do

                protected override void OnLoad(EventArgs e)
                {
                string nacionalidad = "select * from cs_nacionalidad";
                try
                {
                //open connection
                bdConex.OpenConnection();
                //create command and assign the query and connection from the constructor
                MySqlDataAdapter da = new MySqlDataAdapter(nacionalidad, bdConex.conn);
                //create dataset
                DataSet ds = new DataSet();
                // filling dataset
                da.Fill(ds, "cs_nacionalidad");
                //dataset values
                cboNacionalidad.DataSource = ds.Tables["cs_nacionalidad"];
                cboNacionalidad.DisplayMember = "nm_nacionalidad";
                cboNacionalidad.ValueMember = "cd_nacionalidad";
                //close connection
                bdConex.CloseConnection();
                }
                catch (Exception ex)
                {
                MessageBox.Show(ex.Message);
                }
                }

                And then use this class, where you want ComboBox to contain this data.

                Don't forget to rate answer, that helped you. It will allow other people find their answers faster.

                A Offline
                A Offline
                adrian salas
                wrote on last edited by
                #7

                how do I call the class with the combobox as a parameter? Thanks

                L 1 Reply Last reply
                0
                • A adrian salas

                  how do I call the class with the combobox as a parameter? Thanks

                  L Offline
                  L Offline
                  Lukasz Nowakowski
                  wrote on last edited by
                  #8

                  You don't. You create class:

                  public class NationSelector : ComboBox
                  {
                  protected override void OnLoad(EventArgs e)
                  {
                  string nacionalidad = "select * from cs_nacionalidad";
                  try
                  {
                  //open connection
                  bdConex.OpenConnection();
                  //create command and assign the query and connection from the constructor
                  MySqlDataAdapter da = new MySqlDataAdapter(nacionalidad, bdConex.conn);
                  //create dataset
                  DataSet ds = new DataSet();
                  // filling dataset
                  da.Fill(ds, "cs_nacionalidad");
                  //dataset values
                  cboNacionalidad.DataSource = ds.Tables["cs_nacionalidad"];
                  cboNacionalidad.DisplayMember = "nm_nacionalidad";
                  cboNacionalidad.ValueMember = "cd_nacionalidad";
                  //close connection
                  bdConex.CloseConnection();
                  }
                  catch (Exception ex)
                  {
                  MessageBox.Show(ex.Message);
                  }
                  }
                  }

                  And then, when you edit your aspx (or ascx), in the toolbox you should see "NationSelector". And you just drag this control on the designer.

                  Don't forget to rate answer, that helped you. It will allow other people find their answers faster.

                  A 1 Reply Last reply
                  0
                  • L Lukasz Nowakowski

                    You don't. You create class:

                    public class NationSelector : ComboBox
                    {
                    protected override void OnLoad(EventArgs e)
                    {
                    string nacionalidad = "select * from cs_nacionalidad";
                    try
                    {
                    //open connection
                    bdConex.OpenConnection();
                    //create command and assign the query and connection from the constructor
                    MySqlDataAdapter da = new MySqlDataAdapter(nacionalidad, bdConex.conn);
                    //create dataset
                    DataSet ds = new DataSet();
                    // filling dataset
                    da.Fill(ds, "cs_nacionalidad");
                    //dataset values
                    cboNacionalidad.DataSource = ds.Tables["cs_nacionalidad"];
                    cboNacionalidad.DisplayMember = "nm_nacionalidad";
                    cboNacionalidad.ValueMember = "cd_nacionalidad";
                    //close connection
                    bdConex.CloseConnection();
                    }
                    catch (Exception ex)
                    {
                    MessageBox.Show(ex.Message);
                    }
                    }
                    }

                    And then, when you edit your aspx (or ascx), in the toolbox you should see "NationSelector". And you just drag this control on the designer.

                    Don't forget to rate answer, that helped you. It will allow other people find their answers faster.

                    A Offline
                    A Offline
                    adrian salas
                    wrote on last edited by
                    #9

                    Hi, I did it as you told me: protected override void OnLoad(object sender, EventArgs e) { BDconexion bdConex = new BDconexion(); string nacionalidad = "select * from cs_nacionalidad"; try { //open connection bdConex.OpenConnection(); //create command and assign the query and connection from the constructor MySqlDataAdapter da = new MySqlDataAdapter(nacionalidad, bdConex.conn); //create dataset DataSet ds = new DataSet(); // filling dataset da.Fill(ds, "cs_nacionalidad"); //dataset values NationSelector1.DataSource = ds.Tables["cs_nacionalidad"]; NationSelector1.DisplayMember = "nm_nacionalidad"; NationSelector1.ValueMember = "cd_nacionalidad"; //close connection bdConex.CloseConnection(); } catch (Exception ex) { MessageBox.Show(ex.Message); } But I am having problem with these lines: NationSelector1.DataSource = ds.Tables["cs_nacionalidad"]; NationSelector1.DisplayMember = "nm_nacionalidad"; NationSelector1.ValueMember = "cd_nacionalidad"; what is the name that I should write instead of NationSelector1? thanks.

                    L 1 Reply Last reply
                    0
                    • A adrian salas

                      Hi, I did it as you told me: protected override void OnLoad(object sender, EventArgs e) { BDconexion bdConex = new BDconexion(); string nacionalidad = "select * from cs_nacionalidad"; try { //open connection bdConex.OpenConnection(); //create command and assign the query and connection from the constructor MySqlDataAdapter da = new MySqlDataAdapter(nacionalidad, bdConex.conn); //create dataset DataSet ds = new DataSet(); // filling dataset da.Fill(ds, "cs_nacionalidad"); //dataset values NationSelector1.DataSource = ds.Tables["cs_nacionalidad"]; NationSelector1.DisplayMember = "nm_nacionalidad"; NationSelector1.ValueMember = "cd_nacionalidad"; //close connection bdConex.CloseConnection(); } catch (Exception ex) { MessageBox.Show(ex.Message); } But I am having problem with these lines: NationSelector1.DataSource = ds.Tables["cs_nacionalidad"]; NationSelector1.DisplayMember = "nm_nacionalidad"; NationSelector1.ValueMember = "cd_nacionalidad"; what is the name that I should write instead of NationSelector1? thanks.

                      L Offline
                      L Offline
                      Lukasz Nowakowski
                      wrote on last edited by
                      #10

                      You should use this. And additionally you should notice, that OnLoad method should have only one argument: EventArgs e

                      Don't forget to rate answer, that helped you. It will allow other people find their answers faster.

                      A 1 Reply Last reply
                      0
                      • L Lukasz Nowakowski

                        You should use this. And additionally you should notice, that OnLoad method should have only one argument: EventArgs e

                        Don't forget to rate answer, that helped you. It will allow other people find their answers faster.

                        A Offline
                        A Offline
                        adrian salas
                        wrote on last edited by
                        #11

                        I did it and there is not error messages, but what I need to do to see the NationSelector combobox in the toolbox? because I compile the code and It does not appear. Thanks.

                        1 Reply Last reply
                        0
                        Reply
                        • Reply as topic
                        Log in to reply
                        • Oldest to Newest
                        • Newest to Oldest
                        • Most Votes


                        • Login

                        • Don't have an account? Register

                        • Login or register to search.
                        • First post
                          Last post
                        0
                        • Categories
                        • Recent
                        • Tags
                        • Popular
                        • World
                        • Users
                        • Groups