Click here to Skip to main content
15,867,488 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
Am using one procedure to display search result. But the search result returning same values but i want to show it only once in my grid.
Consider, Procedure returns
Column A
A
A
A
B
C
C
and i want to show it in grid like,
Column
A
B
C

Procedure is correct but i need to filter values in C# code how can i do this?
Posted
Updated 4-Mar-13 1:02am
v2
Comments
I guess you have to select only distinct values from the procedure itself.
That would be good.

SELECT DISTINCT(Column_Name)
FROM
MyTable
Am Gayathri 4-Mar-13 5:55am    
In procedure i have more than one columns so i cant use this distinct over there. I need to do it from front end code.
Nandakishore G N 4-Mar-13 7:30am    
you can use groupby clause

Try this query in your datatable:
C#
var uniqueColors =
               (from dbo in MainTable.AsEnumerable()
                 select dbo.Field<string>("Column").Distinct();



--Amit
 
Share this answer
 
v2
Comments
Am Gayathri 4-Mar-13 6:20am    
Is this Linque??
_Amy 4-Mar-13 6:29am    
Yes. :)
Am Gayathri 4-Mar-13 7:00am    
Here where i can give table name,field name etc?? please help
Am Gayathri 4-Mar-13 7:18am    
How can i call/use my procedure here?
_Amy 4-Mar-13 7:31am    
MainTable is your datatable. You can use the same connection string which you was using to fetch the data from database.
If you can alter / create a procedure to use SELECT DISTINCT then do that.

If you cannot, and you are using a datasource that queries directly then you will have to add a pre-processing step to return a distinct table. Take a look at this:
C#... Selecting Distinct rows from DataTable[^]
 
Share this answer
 
v2
Comments
Am Gayathri 4-Mar-13 9:42am    
Tried this, not working
May be this will Help you..!!
Declare string array which coumn you want to distinct
C#
string[] TobeDistinct = { "Cloumn1",  "Cloumn2", "Cloumn3" };

call GetDistinctRecords fuction which will return you distinct record.

C#
DataTable dtDistinct = GetDistinctRecords(dt, TobeDistinct);// dt which is to be distinct


Fuction is here:

C#
private DataTable GetDistinctRecords(DataTable dt, string[] Columns)
    {
        DataTable dtUniqRecords = new DataTable();
        dtUniqRecords = dt.DefaultView.ToTable(true, Columns);
        return dtUniqRecords;
    }
 
Share this answer
 
Comments
Am Gayathri 4-Mar-13 8:54am    
Here you have created two tables
dtDistinct
dtUniqRecords
is it correct?
OPees 4-Mar-13 23:12pm    
Yes, Try this.
dtUniqRecords(DataTable) is returned from the fuction GetDistinctRecords(Which return type is DataTable)
which is assign to dtDistinct(DataTable)



hi professional........


you should try DISTINCT query for this..........like

your Procedure returns
Column A
A
A
A
B
C
C

and for it,
Column
A
B
C
Try this--
SQL
SELECT DISTINCT column_name(s)
FROM table_name


it mean
SELECT DISTINCT Column_A from YourTableName.

for more go throw this link.....

SQL SELECT DISTINCT Statement[^]

Happy to help
 
Share this answer
 
v2

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