943,703 Members | Top Members by Rank

Ad:
  • C# Discussion Thread
  • Marked Solved
  • Views: 31376
  • C# RSS
Apr 16th, 2009
0

Datatable select

Expand Post »
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.

C# Syntax (Toggle Plain Text)
  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. }
Similar Threads
Reputation Points: 10
Solved Threads: 0
Junior Poster in Training
JackDurden is offline Offline
92 posts
since Jun 2008
Apr 16th, 2009
0

Re: Datatable select

C# Syntax (Toggle Plain Text)
  1. foreach(DataRow row in table.Rows)
  2. {
  3. if(row["columnName"] == value)
  4. //display the row, otherwise escape.
  5. }
Featured Poster
Reputation Points: 480
Solved Threads: 276
Postaholic
Ramy Mahrous is offline Offline
2,189 posts
since Aug 2006
Apr 16th, 2009
2

Re: Datatable select

To use the select, try this:
C# Syntax (Toggle Plain Text)
  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.
Reputation Points: 69
Solved Threads: 75
Posting Pro in Training
JerryShaw is offline Offline
465 posts
since Nov 2006
Apr 16th, 2009
0

Re: Datatable select

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?
Reputation Points: 10
Solved Threads: 0
Junior Poster in Training
JackDurden is offline Offline
92 posts
since Jun 2008
Apr 17th, 2009
0

Re: Datatable select

The rows array contains all rows where column [First] = 'Lance' so all you need to do is iterate through the array of rows.
C# Syntax (Toggle Plain Text)
  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
Reputation Points: 69
Solved Threads: 75
Posting Pro in Training
JerryShaw is offline Offline
465 posts
since Nov 2006
Apr 17th, 2009
0

Re: Datatable select

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!

C# Syntax (Toggle Plain Text)
  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. }
Reputation Points: 10
Solved Threads: 0
Junior Poster in Training
JackDurden is offline Offline
92 posts
since Jun 2008
Apr 17th, 2009
0

Re: Datatable select


You need to use the row not rows[0]
C# Syntax (Toggle Plain Text)
  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. }
Reputation Points: 69
Solved Threads: 75
Posting Pro in Training
JerryShaw is offline Offline
465 posts
since Nov 2006
Jun 25th, 2009
0

Re: Datatable select

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.
Reputation Points: 10
Solved Threads: 0
Newbie Poster
flavioweb1 is offline Offline
2 posts
since Jun 2009

This thread is solved

Either the thread starter or a moderator has marked this thread as solved. You can most likely trust the responses and answers given. There is most likely no reason for any further responses to be posted here. If you have a related question, please start a new thread in this forum instead.

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C# Forum Timeline: Delegate functions
Next Thread in C# Forum Timeline: how to save string from textbox





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC