Datatable select

Please support our C# advertiser: Intel Parallel Studio Home
Thread Solved

Join Date: Jun 2008
Posts: 92
Reputation: JackDurden is an unknown quantity at this point 
Solved Threads: 0
JackDurden JackDurden is offline Offline
Junior Poster in Training

Datatable select

 
0
  #1
Apr 16th, 2009
How would I find a row with a column value? Say Im looking for people with the name "Lance" then once ive found Lance display the row of info for lance. Ive tried using select but cant seem to get it to work. Here is my code.

  1. using System;
  2. using System.Collections;
  3. using System.Data;
  4.  
  5. namespace Table
  6. {
  7. class Program
  8. {
  9. static void Main(string[] args)
  10. {
  11. DataTable table = new DataTable("records");
  12. DataColumn Col;
  13. DataRow Row;
  14.  
  15. Col = new DataColumn();
  16. Col.DataType = System.Type.GetType("System.Int32");
  17. Col.ColumnName = "ID";
  18. table.Columns.Add(Col);
  19.  
  20.  
  21. Col = new DataColumn();
  22. Col.DataType = System.Type.GetType("System.String");
  23. Col.ColumnName = "FIRST";
  24. table.Columns.Add(Col);
  25.  
  26.  
  27. Col = new DataColumn();
  28. Col.DataType = System.Type.GetType("System.Int32");
  29. Col.ColumnName = "AGE";
  30. table.Columns.Add(Col);
  31.  
  32. DataColumn[] PrimKey = new DataColumn[1];
  33. PrimKey[0] = table.Columns["ID"];
  34. table.PrimaryKey = PrimKey;
  35.  
  36. Row = table.NewRow();
  37. Row["ID"] = 1;
  38. Row["FIRST"] = "Steve";
  39. Row["AGE"] = 34;
  40. table.Rows.Add(Row);
  41.  
  42. Row = table.NewRow();
  43. Row["ID"] = 2;
  44. Row["FIRST"] = "Peter";
  45. Row["AGE"] = 54;
  46. table.Rows.Add(Row);
  47.  
  48. Row = table.NewRow();
  49. Row["ID"] = 3;
  50. Row["FIRST"] = "Lance";
  51. Row["AGE"] = 94;
  52. table.Rows.Add(Row);
  53.  
  54. // Display the data
  55. foreach (DataRow m in table.Rows)
  56. {
  57. Console.Write("{0} ", m["ID"]);
  58. Console.Write("{0} ", m["FIRST"]);
  59. Console.Write("{0} ", m["AGE"]);
  60. Console.WriteLine();
  61. }
  62. }
  63. }
  64. }
Reply With Quote Quick reply to this message  
Join Date: Aug 2006
Posts: 2,065
Reputation: Ramy Mahrous is just really nice Ramy Mahrous is just really nice Ramy Mahrous is just really nice Ramy Mahrous is just really nice 
Solved Threads: 256
Featured Poster
Ramy Mahrous's Avatar
Ramy Mahrous Ramy Mahrous is offline Offline
Postaholic

Re: Datatable select

 
0
  #2
Apr 16th, 2009
  1. foreach(DataRow row in table.Rows)
  2. {
  3. if(row["columnName"] == value)
  4. //display the row, otherwise escape.
  5. }
BI Developer | LINKdotNET
B.Sc Computer Science, Helwan University
Technical blog | http://ramymahrous.wordpress.com
LinkedIn | http://www.linkedin.com/in/ramymahrous
Reply With Quote Quick reply to this message  
Join Date: Nov 2006
Posts: 436
Reputation: JerryShaw is on a distinguished road 
Solved Threads: 72
JerryShaw JerryShaw is offline Offline
Posting Pro in Training

Re: Datatable select

 
1
  #3
Apr 16th, 2009
To use the select, try this:
  1. DataRow[] rows = table.Select("[First]='Lance'");
  2. if(rows.Length > 0)
  3. {
  4. Console.Write("{0} ", rows[0]["ID"]);
  5. Console.Write("{0} ", rows[0]["FIRST"]);
  6. Console.Write("{0} ", row[0]["AGE"]);
  7. Console.WriteLine();
  8. }
// Jerry
Last edited by JerryShaw; Apr 16th, 2009 at 10:24 am.
Reply With Quote Quick reply to this message  
Join Date: Jun 2008
Posts: 92
Reputation: JackDurden is an unknown quantity at this point 
Solved Threads: 0
JackDurden JackDurden is offline Offline
Junior Poster in Training

Re: Datatable select

 
0
  #4
Apr 16th, 2009
Yes JerryShaw thats exactly what i was looking for, thanks! Just curious what if I wanted to find more then one row that had the name "Lance" in it?
Reply With Quote Quick reply to this message  
Join Date: Nov 2006
Posts: 436
Reputation: JerryShaw is on a distinguished road 
Solved Threads: 72
JerryShaw JerryShaw is offline Offline
Posting Pro in Training

Re: Datatable select

 
0
  #5
Apr 17th, 2009
The rows array contains all rows where column [First] = 'Lance' so all you need to do is iterate through the array of rows.
  1. DataRow[] rows = table.Select("[First]='Lance'");
  2. foreach(DataRow row in rows)
  3. {
  4. Console.Write("{0} ", row["ID"]);
  5. Console.Write("{0} ", row["FIRST"]);
  6. Console.Write("{0} ", row["AGE"]);
  7. Console.WriteLine();
  8. }

BTW, there is a second parameter for the select method that allows you to set the order the rows are returned in.

// Jerry
Reply With Quote Quick reply to this message  
Join Date: Jun 2008
Posts: 92
Reputation: JackDurden is an unknown quantity at this point 
Solved Threads: 0
JackDurden JackDurden is offline Offline
Junior Poster in Training

Re: Datatable select

 
0
  #6
Apr 17th, 2009
I tried that new bit of code and it just prints out the same line twice, which is row 3 when it should print out rows 3 and 4. Here is the code im working with..I added another row, so there are two rows with the name Lance.

Can this line hold more then one row at a time?
DataRow[] rows = table.Select("[First]='Lance'");

Thanks for the help!

  1. using System;
  2. using System.Collections;
  3. using System.Data;
  4.  
  5. namespace Table
  6. {
  7. class Program
  8. {
  9. static void Main(string[] args)
  10. {
  11. DataTable table = new DataTable("records");
  12. DataColumn Col;
  13. DataRow Row;
  14.  
  15. Col = new DataColumn();
  16. Col.DataType = System.Type.GetType("System.Int32");
  17. Col.ColumnName = "ID";
  18. table.Columns.Add(Col);
  19.  
  20.  
  21. Col = new DataColumn();
  22. Col.DataType = System.Type.GetType("System.String");
  23. Col.ColumnName = "FIRST";
  24. table.Columns.Add(Col);
  25.  
  26.  
  27. Col = new DataColumn();
  28. Col.DataType = System.Type.GetType("System.Int32");
  29. Col.ColumnName = "AGE";
  30. table.Columns.Add(Col);
  31.  
  32. DataColumn[] PrimKey = new DataColumn[1];
  33. PrimKey[0] = table.Columns["ID"];
  34. table.PrimaryKey = PrimKey;
  35.  
  36. Row = table.NewRow();
  37. Row["ID"] = 1;
  38. Row["FIRST"] = "Steve";
  39. Row["AGE"] = 34;
  40. table.Rows.Add(Row);
  41.  
  42. Row = table.NewRow();
  43. Row["ID"] = 2;
  44. Row["FIRST"] = "Peter";
  45. Row["AGE"] = 54;
  46. table.Rows.Add(Row);
  47.  
  48. Row = table.NewRow();
  49. Row["ID"] = 3;
  50. Row["FIRST"] = "Lance";
  51. Row["AGE"] = 94;
  52. table.Rows.Add(Row);
  53.  
  54. Row = table.NewRow();
  55. Row["ID"] = 4;
  56. Row["FIRST"] = "Lance";
  57. Row["AGE"] = 61;
  58. table.Rows.Add(Row);
  59.  
  60. // Display the data
  61. foreach (DataRow m in table.Rows)
  62. {
  63. Console.Write("{0} ", m["ID"]);
  64. Console.Write("{0} ", m["FIRST"]);
  65. Console.Write("{0} ", m["AGE"]);
  66. Console.WriteLine();
  67. }
  68.  
  69. Console.WriteLine("\n");
  70. DataRow[] rows = table.Select("[First]='Lance'");
  71. foreach (DataRow row in rows)
  72. {
  73. Console.Write("{0} ", rows[0]["ID"]);
  74. Console.Write("{0} ", rows[0]["FIRST"]);
  75. Console.Write("{0} ", rows[0]["AGE"]);
  76. Console.WriteLine();
  77. }
  78.  
  79. }
  80. }
  81. }
Reply With Quote Quick reply to this message  
Join Date: Nov 2006
Posts: 436
Reputation: JerryShaw is on a distinguished road 
Solved Threads: 72
JerryShaw JerryShaw is offline Offline
Posting Pro in Training

Re: Datatable select

 
0
  #7
Apr 17th, 2009

You need to use the row not rows[0]
  1. foreach (DataRow row in rows)
  2. {
  3. Console.Write("{0} ", row["ID"]; // rows[0]["ID"]);
  4. Console.Write("{0} ", row["FIRST"]; //rows[0]["FIRST"]);
  5. Console.Write("{0} ", row["AGE"]; //rows[0]["AGE"]);
  6. Console.WriteLine();
  7. }
Reply With Quote Quick reply to this message  
Join Date: Jun 2009
Posts: 2
Reputation: flavioweb1 is an unknown quantity at this point 
Solved Threads: 0
flavioweb1's Avatar
flavioweb1 flavioweb1 is offline Offline
Newbie Poster

Re: Datatable select

 
0
  #8
Jun 25th, 2009
Staff, Why don't you do create DataTable to store the data separately, then to create the DataTable is just using a "for"

Then you have something like this:
''-----------
dim dataArray as ArrayList = GetData();
for integer i = 0 to dataArray.count - 1
{
Row = table.NewRow();
Row["ID"] = dataArray(0)(i);
Row["FIRST"] = dataArray(1)(i);
Row["AGE"] = dataArray(2)(i);
table.Rows.Add(Row);
}
next
''-----------
With this you create evething rows your datatable, sure you need store data in ArrayList, and you can do for columns too.
Sorry my mistake english, I'm Brazilian, my english is basic.

:-)
Last edited by flavioweb1; Jun 25th, 2009 at 12:10 pm.
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:



Similar Threads
Other Threads in the C# Forum
Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC