Summary of methods for Traversing various data sets in C #

Source: Internet
Author: User

The method for Traversing various data sets in C # is summarized here:
1. Enumeration type Copy codeThe Code is as follows: // traverse the enumerated names of the enumeration type samples.
Foreach (string sp in Enum. GetNames (typeof (Sample )))
{
Ary. Add (sp );
}
// Traverse the enumerated values of the enumeration type Sample
Foreach (string sp in Enum. GetValues (typeof (Sample )))
{
Ary. Add (sp );
}

2. Traverse ArrayList (Queue, Stack)
Here we take string as an example. Of course, the elements in the ArrayList can be of any data type, and the time duration must be confirmed that the elements in the ArrayList are of the same data type.Copy codeThe Code is as follows: // The traversal element is a string-type queue.
Foreach (string text in arraylist)
{
Ary. Add (text );
}

In addition, the way to traverse Queue queues and Stack Stacks is basically the same as that of ArrayList. You can use foreach to traverse the queues cyclically, but one is first-in-first-out and the other is first-out.
3. Controls in Winform Copy codeThe Code is as follows: // search for the control in the main form and remove the qualified control from the form
Foreach (Control ctl in this. Controls)
{
// Obtain and determine the control type or control name
If (ctl. GetType (). Name. Equals ("ListBox") | ctl. Name. Equals ("listBox1 "))
This. Controls. Remove (ctl );
}

4. HashTable hash table
The DictionaryEntry class must reference System. Collections.Copy codeThe Code is as follows: // traverse the keys and values in the complete hash table
Foreach (DictionaryEntry item in hashTable)
{
Ary. Add ("hash Key:" + item. Key + ", hash Value:" + item. Value. ToString ());
}
In addition, you can traverse the keys or values in the hash table separately.
// Only traverse keys in the hash table
Foreach (string key in hashTable. Keys)
{
Ary. Add ("hash key:" + key );
}
// Only traverse values in the hash table
Foreach (string value in hashTable. Values)
{
Ary. Add ("hash value:" + value );
}

5. Traverse rows and columns in DataSet and DataTable Copy codeThe Code is as follows: // traverse the table in DataSet
Foreach (DataTable dt in dataSet. Tables)
{
Ary. Add ("table name:" + dt. TableName. ToString ());
}
// Traverse the rows in the first table by default in DataSet
Foreach (DataRow dr in dataSet. Tables [0]. Rows)
{
// Obtain the data of a field (column) in the row
Ary. Add (dr ["ID"]. ToString ());
}
// Traverse the columns in the first table by default in DataSet
Foreach (DataColumn col in dataSet. Tables [0]. Columns)
{
Ary. Add ("column name:" + col. ColumnName );
}

The method for able to traverse rows and columns is similar to that for DataSet. Just replace dataSet. Tables [0] with a specific table.
In addition, you can perform SQL queries on the able table and then traverse the query results.Copy codeThe Code is as follows: // traverse the results of the SELECT statement in the DataSet table after the query conditions are executed.
Foreach (DataRow dr in dataSet. Tables [0]. Select ("MONTH> 6 and month <12 "))
{
// Obtain the data of a field (column) in the row
Ary. Add (dr ["ID"]. ToString ());
}

6. traverse the rows in the DataGridView. Copy codeThe Code is as follows: // traverse the rows in the DataGridView
Foreach (DataGridViewRow dr in dataGridView1.Rows)
{
// Obtain the data of a field (column) in the row
Ary. Add (dr. Cells ["ID"]. ToString ());
}

7. Traverse items in ListBOX and ComboBox
Generally, foreach traversal can only traverse the names of items in ListBOX and ComboBox. The full traversal requires that the item data added when the item is bound is a binary property custom class object, use the name of one attribute of the object as DisplayMember (item name), and the other as DisplayValue (item value ). In this way, you can obtain the names and values of items in ListBOX and ComboBox during traversal.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.