Click here to Skip to main content
15,901,426 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello there

I have created a C# application.
The application runs fine but got stuck at one point.
I am using DataTable "Items" to fetch data from the database.
Currently my DataTable having two columns Item_Code,Item_Desc and it fetches around 500 rows from the database. Now I need to select only row corresponding to Item_Code ='BO97' and if this row is found, then it will be automatically store in the DataTable . Overall I want only one single row to be display in the DataTable "Items",if found.


Thanks in Advance
Posted
Updated 30-Apr-12 0:00am
v3

The Select method of DataTable explained here
http://msdn.microsoft.com/en-us/library/b5c0xc84.aspx[^]
can be used to filter the Rows of the DataTable as follows:
C#
DataRow[] rows = items.Select("Item_Code ='BO97'",string.Empty, DataViewRowState.CurrentRows);

The second parameter can be used to Sort the rows returned by the method.
The Select method returns an Array of DataRows. Hence all Columns of DataTable are automatically present in the returned rows.

It is preferable to use DataViewRowState.CurrentRows as the third argument to the method, so that it will not return the rows which have been deleted by Row.Delete() method. Otherwise, it returns these rows also and while performing an operation on these rows an error will be thrown.
If the single row is required if found then
C#
if (rows.Length > 0) 
    //use rows[0]

Alternatively, the DataView can be used
C#
DataView itemsView = items.DefaultView();
itemsView.RowFilter= "Item_Code='BO97'";
if itemsView.Count > 0 then
    //itemsView[0].Row; can be used
 
Share this answer
 
Comments
Mayank Topiwala 30-Apr-12 8:48am    
EXCELLENT !!
VJ Reddy 30-Apr-12 9:49am    
Thank you for the response.
But excellent = vote 4, then 5 = ? :-)
If the solution is helpful you may accept the solution using the Accept Solution button in the title of the Solution.
You have to modify your select query.
Use something like this.
select * from Items where Item_Code='B097'
 
Share this answer
 
Comments
Mayank Topiwala 30-Apr-12 5:57am    
I can do this but the above question is just an example. I have different different datatable which select multiple data.
nagendrathecoder 30-Apr-12 5:59am    
So what exactly do you need?
You have to where clause in your queries to fetch single record.
Prasad_Kulkarni 30-Apr-12 6:02am    
See Mayank, this answer is relevant according to your question. If you are not getting expected result then use 'Improve question' widget add more details so CP experts can do needful for you. Some code snippets always works.
Mayank Topiwala 30-Apr-12 6:04am    
ok here's the Sample :
(The Data is already get collected in Items Table, Now I want to filter for the one row)
foreach(DataRow row in Items.Select("Item_Code='BO97'")
{
//Here What should I do so the relevant data has been fetched in Items table.
}
nagendrathecoder 30-Apr-12 6:19am    
pass a variable in place of actual value.
Hi,

if u want to filter datatable then
try following Code


SQL
DataRow[] drarray= objDataTable.Select("Item_Code='BO97'");


Hope this help u
Best Luck
Happy Coding:)
 
Share this answer
 

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