Hey, I didn't know you, but I couldn't think you were so easy to use. NET.

Source: Internet
Author: User

Some methods in. NET have never been used before, but after they have been used, they make people shine. Wow, you are so easy to use.

The following describes the methods I have ignored. Of course, everyone's programming experience, experience, and awareness of. NET are different. Therefore, this is just a family statement, and there must be many shortcomings. You are welcome to criticize and correct them.

1. ADO. NET class

DataTable. Merge ()

How to merge two tables? Methods previously written by myself:

Private void UniteTable (sourable sourceTable, targetable targetTable) {foreach (DataRow row in sourceTable. rows) {DataRow newRow = targetTable. newRow (); // assign the value of row in sourceTable to row newRow ["column1"] = row ["column1"] in the corresponding targetTable; //... targetTable. rows. add (newRow );}}

Ah, this method is silly and naive. In fact, the DataTable has provided a method to Merge two tables, that is, DataTable. Merge (). Merge two tables with only one statement:

targetTable.Merge(sourceTable);

DataRow. ItemArray

If the structure is the same, how do I assign the values of one row to another? I used to write it like this:

            DataRow row1 = table1.Rows[0];            DataRow row2 = table2.Rows[0];            row1["column1"] = row2["column1"];            row1["column2"] = row2["column2"];            //...

God, what should I do if there are more than 30 columns. In fact, DataRow has an ItemArray attribute, which can be completed in one sentence:

row1.ItemArray = row2.ItemArray;

SqlCommand. Parameters. AddWithValue ()

How to add parameters when SqlCommand executes the stored procedure, just like this:

// Set the parameter name and type cmd. parameters. add ("@ Target", SqlDbType. NChar); cmd. parameters. add ("@ Description", SqlDbType. NChar); cmd. parameters. add ("@ Actor", SqlDbType. NChar); // assign cmd to the parameter. parameters [0]. value = "ATarget"; cmd. parameters [1]. value = "Description"; cmd. parameters [2]. value = "Actor ";

The preceding steps are divided into two steps: Add a parameter and assign a value to the parameter. In fact, we can do this in one step:

                cmd.Parameters.AddWithValue("@Actor", "Actor");                cmd.Parameters.AddWithValue("@Target", "Target");                cmd.Parameters.AddWithValue("@Description", "Description");

2. Collection class

List. AddRange ()

How to add multiple values to the List? I used to do this:

List<int> list = new List<int>();            list.Add(1);            list.Add(2);            list.Add(3);            list.Add(4);

In fact, you can use a method to write data. It is AddRange (). (AddRange () exists in many classes. Here I just use LIST as an example)

list.AddRange(new int[] { 1,2,3,4});

List. Find () and List. FindAll ()

Search for specific values in the List? I used to do this in the past:

          foreach (int i in list)            {                if (i == 3)                {                    Console.Write(i);                }            }

In fact, there is no need for a foreach loop. Using Find () can be a good solution: (similar to the use of FindAll () and Find)

int result = list.Find(delegate(int i) { return i == 3; });

3. Controls

DataGridView. HitTest ()

In the DataGridView datagri, how does one obtain the number of rows and columns at the current mouse position? I think this method can be used for the DataGridView, but it has not been found for a long time. At last, I worked hard and found it at the tip of VS intelligence. What's the name of HitTest? Starting with Mao, not Get, Find, or Index. The returned value is still an internal class: DataGridView. HitTestInfo

// Capture the information of the mouse clicking area. hitTestInfo hitTestInfo = this. sourceGrid. hitTest (e. x, e. y); // get the row number int rowIndex = hitTestInfo. rowIndex; // get the number of columns int columnIndex = hitTestInfo. columnIndex;

ListBox. IndexFromPoint ()

Similarly, ListBox also has a method to obtain the number of rows based on the Point, that is, IndexFromPoint (). You see, this name is much better:

 

           // Get the index of the item the mouse is below.           indexOfItemUnderMouseToDrag = ListDragSource.IndexFromPoint(e.X, e.Y);

4. Others

Hexadecimal conversion.

In the past, hexadecimal conversion was performed, for example, hexadecimal conversion to octal conversion, and an independent method was also written. In fact, Convert. ToInt32 () and string. Format () both provide overload methods to achieve this:

// Convert the hexadecimal value "10" to the decimal I int I = Convert. toInt32 ("10", 16); // converts decimal I to hexadecimal s string s = string. format ("{0: X}", I );

PS. I will only provide examples here. You can use Baidu for details about the methods mentioned above.

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.