Answered by:
Get Specific Cloumn value of the Datatable

Question
-
Hi,
I've Datatable that contain value from the database. Now in Program I don't want to print all column values. I just want to print specific column value. Suppose I've datatable which populate from table having a,b , c and d column and with 15 rows. I want to print only c column values will all 15 values. How can I achieve this. I know a loop will be required to iterate the datatable but I'm not sure how to set column and increment rows to that I can Achieve my target.
Thank for the anticipation.
Regards,
KamAbA
Monday, January 27, 2014 7:13 PM
Answers
-
The Rows property of a DataTable contains all of the rows in the table. Use it to iterate over each row in the table:
foreach(DataRow row in myTable.Rows) { var cValue = row["NameOfColumnC"]; // do something here with cValue }
- Edited by JeffreyFerguson Monday, January 27, 2014 7:33 PM spelling
- Proposed as answer by Loganathan.v Friday, January 31, 2014 6:22 AM
- Marked as answer by Herro wongMicrosoft contingent staff Friday, February 7, 2014 1:54 AM
Monday, January 27, 2014 7:33 PM -
Try code below
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Data; namespace ConsoleApplication1 { class Program { static List<List<object>> inputs = new List<List<object>>{ new List<object> {"a","b","c",0}, new List<object> {"d","e","f",1}, new List<object> {"a","b","c",2}, new List<object> {"a","b","c",3}, new List<object> {"a","b","c",4}, new List<object> {"a","b","c",5}, new List<object> {"a","b","c",6}, new List<object> {"a","b","c",7}, new List<object> {"a","b","c",8}, new List<object> {"a","b","c",9}, new List<object> {"a","b","c",10} }; static void Main(string[] args) { DataTable table = new DataTable(); table.Columns.Add("a", typeof(string)); table.Columns.Add("b", typeof(string)); table.Columns.Add("c", typeof(string)); table.Columns.Add("i", typeof(int)); foreach (List<object> input in inputs) { DataRow newRow = table.Rows.Add(); newRow.ItemArray = input.ToArray(); } var selectedColumns = table.AsEnumerable() .Select(x => new { a = x.Field<string>("a"), i = x.Field<int>("i") }) .ToList(); } } }
jdweng
- Marked as answer by Herro wongMicrosoft contingent staff Friday, February 7, 2014 1:54 AM
Monday, January 27, 2014 7:40 PM
All replies
-
The Rows property of a DataTable contains all of the rows in the table. Use it to iterate over each row in the table:
foreach(DataRow row in myTable.Rows) { var cValue = row["NameOfColumnC"]; // do something here with cValue }
- Edited by JeffreyFerguson Monday, January 27, 2014 7:33 PM spelling
- Proposed as answer by Loganathan.v Friday, January 31, 2014 6:22 AM
- Marked as answer by Herro wongMicrosoft contingent staff Friday, February 7, 2014 1:54 AM
Monday, January 27, 2014 7:33 PM -
Try code below
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Data; namespace ConsoleApplication1 { class Program { static List<List<object>> inputs = new List<List<object>>{ new List<object> {"a","b","c",0}, new List<object> {"d","e","f",1}, new List<object> {"a","b","c",2}, new List<object> {"a","b","c",3}, new List<object> {"a","b","c",4}, new List<object> {"a","b","c",5}, new List<object> {"a","b","c",6}, new List<object> {"a","b","c",7}, new List<object> {"a","b","c",8}, new List<object> {"a","b","c",9}, new List<object> {"a","b","c",10} }; static void Main(string[] args) { DataTable table = new DataTable(); table.Columns.Add("a", typeof(string)); table.Columns.Add("b", typeof(string)); table.Columns.Add("c", typeof(string)); table.Columns.Add("i", typeof(int)); foreach (List<object> input in inputs) { DataRow newRow = table.Rows.Add(); newRow.ItemArray = input.ToArray(); } var selectedColumns = table.AsEnumerable() .Select(x => new { a = x.Field<string>("a"), i = x.Field<int>("i") }) .ToList(); } } }
jdweng
- Marked as answer by Herro wongMicrosoft contingent staff Friday, February 7, 2014 1:54 AM
Monday, January 27, 2014 7:40 PM -
You can make use of foreach loop
referce
http://www.dotnetperls.com/foreach
foreach(datarow dr in dt.rows)
{
var xResult = dr("c")// Where c is columnname whose values needs to displayed
}
Shridhar J Joshi
Mark as Ans if you find it useful
Tuesday, January 28, 2014 9:37 AM