Obtain the value of a column in the first row of the able in Asp.net, asp. netdatatable

Source: Internet
Author: User

Obtain the value of a column in the first row of the able in Asp.net, asp. netdatatable

The data source is a DataTable. Now we need to obtain the value of the first column of the first row of the DataTable.

Prepare a dataset, create a able, and fill in the data:


Source code:

Using System; using System. collections. generic; using System. data; using System. linq; using System. text; using System. threading. tasks; using Insus. NET. models; namespace Insus. NET. entities {public class CustomerEntity {public DataTable MERs () {DataTable dt = new DataTable (); dt. columns. add (new DataColumn ("CustomerID", typeof (int); dt. columns. add (new DataColumn ("CustomerName", typeof (string); dt. columns. add (new DataColumn ("PID", typeof (string); DataRow dr = dt. newRow (); dr ["CustomerID"] = 9; dr ["CustomerName"] = "Zhang San"; dr ["PID"] = "123456789012"; dt. rows. add (dr); dr = dt. newRow (); dr ["CustomerID"] = 10; dr ["CustomerName"] = ""; dr ["PID"] = "321245677812"; dt. rows. add (dr); dr = dt. newRow (); dr ["CustomerID"] = 30; dr ["CustomerName"] = "Wu Guang"; dr ["PID"] = "213445678912"; dt. rows. add (dr); dr = dt. newRow (); dr ["CustomerID"] = 35; dr ["CustomerName"] = "Wang Wei"; dr ["PID"] = "334456789012"; dt. rows. add (dr); dr = dt. newRow (); dr ["CustomerID"] = 36; dr ["CustomerName"] = "Zhao Yong"; dr ["PID"] = "213445678912"; dt. rows. add (dr); return dt ;}}}

Method 1: Use LINQ and Lambda:

In the preceding example, the first row is obtained first, and then a value is obtained from the obtained row.

Method 2:

Method 3:

Or:

Method 4:

Create an object:

Convert a able to this object set:

......

There are many methods, you can choose your preferred one.

The above section describes how to obtain a DataTable and select a column value for the first row. I hope it will be helpful to you. If you have any questions, please leave a message for me, the editor will reply to you in a timely manner. Thank you very much for your support for the help House website!

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.