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!