About DataTable or/and Combobox

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • newuserint
    New Member
    • Dec 2009
    • 7

    About DataTable or/and Combobox

    Hi folks, i wanted to know if there's a way i can save into a string array the values contained in a datatable.

    Also, i know i can save into a combobox the values of a DataTable containing a SQL query from a Database, but is there a way i can get this values saved in the combobox, to save them in a string array?

    Class1 Object1=new Class1();
    this.combobox1. DataSource=Obje ct1.MethodDataT able();
  • Frinavale
    Recognized Expert Expert
    • Oct 2006
    • 9749

    #2
    So, a String Array is just an array of a bunch of Strings.
    A DataTable has rows and columns.

    Therefore a DataTable cannot be saved into a 1 dimensional string array.

    It doesn't make any sense to ask what you are asking.
    Maybe you mean to save it into a 2D string array...why you would want to do something like that is beyond me because working with a DataTable has benefits over working with a 2D string array.

    If you really want to do this you need to loop through all of the rows for the table...and for each row that you loop through you need to loop through all of the columns for that row in order to retrieve what is in the specific cell.


    The ComboBox question is even more confusing for me.
    I have no idea what you are trying to do.

    -Frinny

    Comment

    • newuserint
      New Member
      • Dec 2009
      • 7

      #3
      Basically what im trying to do, is to get values from a database to put em as Buttons Texts for example:

      this.button1.te xt=this.cbb.sel ectedvalue.tost ring();

      the number of buttons will depend of the number of data contained in the table(which only contains an atribbute) from the database.
      for example:
      Table Providers:

      ProviderName

      Palmolive
      Hersheys
      Coke

      In the example below i will have only 3 buttons.

      Comment

      • Frinavale
        Recognized Expert Expert
        • Oct 2006
        • 9749

        #4
        I'm not sure why you would want to store this into a String Array at all then.

        You need to loop through each row.
        For each row you need to create a button.
        For each column in the row you need to grab the properties for the button.
        Then you need to add the button to a control....and loop to the next row.

        -Frinny

        Comment

        • newuserint
          New Member
          • Dec 2009
          • 7

          #5
          Thanks for your reply, as you may already noted im newbie at this, so if you could show me an example of what you said in the last post, that would be superb.

          Comment

          • newuserint
            New Member
            • Dec 2009
            • 7

            #6
            I figured it out to save the values from the combobox to an array:
            Code:
            contador = this.cbbAux.Items.Count;
            string[] matrix = new string[contador];
            for (this.cbbAux.SelectedIndex = 0; this.cbbAux.SelectedIndex < contador; this.cbbAux.SelectedIndex++)
            {
              matrix[this.cbbAux.SelectedIndex] = this.cbbAux.SelectedValue.ToString();
            }
            The only problem is that index start in 0 and Count is a real number, so it shows me an "argument out of range exception".
            I dont know how to solve this new exception.
            Last edited by Frinavale; Dec 10 '09, 02:22 PM. Reason: Please post code in [code] ... [/code] tags. Added code tags.

            Comment

            • Frinavale
              Recognized Expert Expert
              • Oct 2006
              • 9749

              #7
              Oh I think I know what your problem is now.
              How are you retrieving your DataTable?

              -Frinny

              Comment

              • newuserint
                New Member
                • Dec 2009
                • 7

                #8
                i solved it:
                Code:
                for (int i = 0; i < contador; i++)
                            {
                                matrix[i] = this.cbbAux.SelectedValue.ToString();
                                if(this.cbbAux.SelectedIndex<contador-1)
                                    this.cbbAux.SelectedIndex++;
                                
                            }
                Code:
                Categorias Cate = new Categorias();
                            this.cbbAux.DataSource = Cate.ListadoGeneral();
                            this.cbbAux.ValueMember = "NombreCategoria";
                            this.cbbAux.DisplayMember = "NombreCategoria";
                Method ListadoGeneral from Categorias Class:
                Code:
                public DataTable ListadoGeneral()
                        {
                            // indicar la instancia SQL 
                            this.cadenasql = string.Format
                                ("Select NombreCategoria From Categorias");
                            // definir un  objeto adapter
                            OleDbDataAdapter Adaptador = new OleDbDataAdapter(this.cadenasql, this.cnpuntoventa);
                            // recuperar la tabla y guardar en el dataset 
                            Adaptador.Fill(this.dspuntoventa, "Categorias");
                            // retornar latabla de categorias
                            return this.dspuntoventa.Tables[0];
                        }

                Comment

                • newuserint
                  New Member
                  • Dec 2009
                  • 7

                  #9
                  Ok now i know all that but i have now another question:
                  If Sql Query doesnt find desired values what is saved into the Dataset?

                  For example:
                  Code:
                  public DataTable ListadoGeneral()
                          {
                              // indicar la instancia SQL 
                              this.cadenasql = string.Format
                                  ("Select ClaveArticulo,NombreArticulo From Mercancia Where NombreProveedor='{0}'",
                                  this.nombreproveedor);
                              // definir un  objeto adapter
                              OleDbDataAdapter Adaptador = new OleDbDataAdapter(this.cadenasql, this.cnpuntoventa);
                              // recuperar la tabla y guardar en el dataset 
                              Adaptador.Fill(this.dspuntoventa, "Mercancia");
                              // retornar latabla de estados
                              return this.dspuntoventa.Tables[0];
                          }
                  if query does not find elements with the specified condition, what is the query returning? and what is the Adapter filling into the dataset?, in case is really filling it.

                  Maybe this is really a SQL question but any help would be really apreciated.

                  Comment

                  Working...