Click here to Skip to main content
15,913,722 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
How to get a datarow from a datable in C#?

I have a datatable which is having n no. of rows and each row has a unique field say ID.
Now I want to get a particular row for a specified ID.
eg. there are 5 rows in a datatable and the id's are
1,2,3,4 and 5 and if I want to select a row having ID = 4
What will be the code snippet for that?
Does anybody have any idea?
Thanks in advance,help me as soon as possible..Please
Posted
Comments
[no name] 3-May-12 7:58am    
What have you tried?

This is the first in a series:
A Practical Guide to .NET DataTables, DataSets and DataGrids - Part 1[^]

Best regards
Espen Harlinn
 
Share this answer
 
Comments
Member 8405530 3-May-12 8:34am    
thank u
Member 8405530 3-May-12 8:36am    
thanks
VJ Reddy 3-May-12 8:38am    
Good reference. 5!
Espen Harlinn 3-May-12 8:38am    
Thanks VJ :-D
Prasad_Kulkarni 3-May-12 8:43am    
+5!
Since ID is a unique field then if it is set as the primary key of the DataTable then the DataRowCollection.Find method can be used as explained here
DataRowCollection.Find Method (Object)[^]
The Rows property of DataTable returns DataRowCollection, which can be used for Find method
 
Share this answer
 
Comments
Member 8405530 3-May-12 8:34am    
thank u
VJ Reddy 3-May-12 8:37am    
You're welcome and thank you for the response.
Espen Harlinn 3-May-12 8:36am    
5'ed!
VJ Reddy 3-May-12 8:37am    
Thank you, Espen.
Prasad_Kulkarni 3-May-12 8:43am    
+5!
 
Share this answer
 
Comments
Member 8405530 3-May-12 8:34am    
thank u
Prasad_Kulkarni 3-May-12 8:41am    
Glad it helps!
Thank you for accepting solution
Espen Harlinn 3-May-12 8:35am    
5'ed!
Prasad_Kulkarni 3-May-12 8:41am    
Thank you Espen :)
VJ Reddy 3-May-12 8:38am    
Good references. 5!
use the following code

C#
DataTable dt; //intialize the table with data
           int [] sidx={4}; //put index you wish to find in array
           DataRow dr = dt.Rows.Find(sidx);


hope this helps

Best of luck.........
 
Share this answer
 
Comments
Member 8405530 3-May-12 8:34am    
thank u
Mantu Singh 3-May-12 8:49am    
U r welcome...........!

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900