Reputation: 129
I have a DataTable like this
Name Age
Raj 20
Biny 19
Raj 17
Jose 27
Jose 15
When I click Name
Column, then I should get the datatable sorted as
Name Age
Biny 19
Jose 15
Jose 27
Raj 17
Raj 20
The above table is sorted Based on Name column. How can I do it?
Upvotes: 0
Views: 77
Reputation: 1594
One solution for this problem is by making a stored procedure
SET ANSI_NULLS ON
GO SET QUOTED_IDENTIFIER ON
GO
Create PROCEDURE [dbo].[proc_<procedure_name>]
AS
BEGIN
SET NOCOUNT ON
DECLARE @Err int
SELECT [Name], [Age]
FROM [Table_Name]
ORDER BY Name ASC
SET @Err = @@Error
RETURN @Err
END
call this stored procedure
using (var conn = new SqlConnection(connectionString))
using (var command = new SqlCommand("ProcedureName", conn) {
CommandType = CommandType.StoredProcedure }) {
conn.Open();
command.ExecuteNonQuery();
conn.Close();
}
Upvotes: 0
Reputation: 11727
Something of this kind should work for you.
dataTable.DefaultView.Sort = "Name asc";
Also you can create a method to change the ascending/descending behavior of dofferent columns by taking them as arguments. Only challenge you will have to recognize is which column header is clicked. That should not be that difficult.
Hope it helps.
Upvotes: 1
Reputation: 1858
I think that select will do the trick. It has a sorting option
http://msdn.microsoft.com/en-us/library/way3dy9w.aspx
Upvotes: 0