473,395 Members | 1,530 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

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=Object1.MethodDataTable( );
Dec 9 '09 #1
8 1955
Frinavale
9,735 Expert Mod 8TB
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
Dec 9 '09 #2
Basically what im trying to do, is to get values from a database to put em as Buttons Texts for example:

this.button1.text=this.cbb.selectedvalue.tostring( );

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.
Dec 9 '09 #3
Frinavale
9,735 Expert Mod 8TB
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
Dec 9 '09 #4
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.
Dec 10 '09 #5
I figured it out to save the values from the combobox to an array:
Expand|Select|Wrap|Line Numbers
  1. contador = this.cbbAux.Items.Count;
  2. string[] matrix = new string[contador];
  3. for (this.cbbAux.SelectedIndex = 0; this.cbbAux.SelectedIndex < contador; this.cbbAux.SelectedIndex++)
  4. {
  5.   matrix[this.cbbAux.SelectedIndex] = this.cbbAux.SelectedValue.ToString();
  6. }
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.
Dec 10 '09 #6
Frinavale
9,735 Expert Mod 8TB
Oh I think I know what your problem is now.
How are you retrieving your DataTable?

-Frinny
Dec 10 '09 #7
i solved it:
Expand|Select|Wrap|Line Numbers
  1. for (int i = 0; i < contador; i++)
  2.             {
  3.                 matrix[i] = this.cbbAux.SelectedValue.ToString();
  4.                 if(this.cbbAux.SelectedIndex<contador-1)
  5.                     this.cbbAux.SelectedIndex++;
  6.  
  7.             }
  8.  
Expand|Select|Wrap|Line Numbers
  1. Categorias Cate = new Categorias();
  2.             this.cbbAux.DataSource = Cate.ListadoGeneral();
  3.             this.cbbAux.ValueMember = "NombreCategoria";
  4.             this.cbbAux.DisplayMember = "NombreCategoria";
  5.  
Method ListadoGeneral from Categorias Class:
Expand|Select|Wrap|Line Numbers
  1. public DataTable ListadoGeneral()
  2.         {
  3.             // indicar la instancia SQL 
  4.             this.cadenasql = string.Format
  5.                 ("Select NombreCategoria From Categorias");
  6.             // definir un  objeto adapter
  7.             OleDbDataAdapter Adaptador = new OleDbDataAdapter(this.cadenasql, this.cnpuntoventa);
  8.             // recuperar la tabla y guardar en el dataset 
  9.             Adaptador.Fill(this.dspuntoventa, "Categorias");
  10.             // retornar latabla de categorias
  11.             return this.dspuntoventa.Tables[0];
  12.         }
Dec 10 '09 #8
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:
Expand|Select|Wrap|Line Numbers
  1. public DataTable ListadoGeneral()
  2.         {
  3.             // indicar la instancia SQL 
  4.             this.cadenasql = string.Format
  5.                 ("Select ClaveArticulo,NombreArticulo From Mercancia Where NombreProveedor='{0}'",
  6.                 this.nombreproveedor);
  7.             // definir un  objeto adapter
  8.             OleDbDataAdapter Adaptador = new OleDbDataAdapter(this.cadenasql, this.cnpuntoventa);
  9.             // recuperar la tabla y guardar en el dataset 
  10.             Adaptador.Fill(this.dspuntoventa, "Mercancia");
  11.             // retornar latabla de estados
  12.             return this.dspuntoventa.Tables[0];
  13.         }
  14.  
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.
Dec 11 '09 #9

Sign in to post your reply or Sign up for a free account.

Similar topics

0
by: Mohamed Osman | last post by:
If a bind a data table to a comboxbox as appose to merging the table into a dataset the selectedvalue property does not work consistently On loading of a form: A datatable is created from a...
4
by: Gene Hubert | last post by:
I'm looking at using the result of DataTable.Select as the DataSource for a ComboBox. Is there an easy way to do this with a minimum of code. I tried to bludgeon it into submission by writing:...
2
by: Daylor | last post by:
hi. i have 2 combobox on form. after i load the form. when i select the first combobox , i want to filter the datatable that attached to the second combobox. i wrote :
0
by: reidarT | last post by:
I have found a good combobox with multiple fields The example shows filling the combobox from a listview, but I want to fill it with zip-code and cities from a database. The example is as follows:...
0
by: XenReborn | last post by:
Ok this should be simple. I made a form, added a combobox (for selecting items to edit, not for updating fields), several textboxes, a few checkboxes etc. On formshow it connects to my database,...
1
by: gggram2000 | last post by:
OK here's my code: SqlConnection dbConnection = new SqlConnection(xmldb); SqlCommand cmd = new SqlCommand(" SELECT * FROM dbo.fnInvoiceID() ", dbConnection); cmd.CommandType =...
0
by: Ronald S. Cook | last post by:
Using LINQ, I can bind my IEnumerable to my ComboBox no problem. However, in the past it would be a DataTable that I could insert a new row (e.g. "(Please select...)") and then sort it with a...
1
by: Aleksey Timonin | last post by:
Hi guys, I have a comboBox binded to DataTable: comboBox.DataSource = MyDataTable; comboBox.ValueMember = "id"; comboBox.DisplayMember = "id";
2
by: anatanat | last post by:
hello :) I want to bind a combobox to a specific column in a DataTable. I want the combobox items to display distinct values. I tried: combo.datasource = dataTable; combo.DisplayMember =...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.