I have been trying to find a good solution googling around and i am having little success on my current project to implement custom paging and Sorting on an ASp GridView Control (.NET 4.0).
This is the most current site i have attempted to use as a base to complete this task:
http://www.codeproject.com/KB/webforms/GridViewCustomPaging.aspx
The biggest problem i have with this solution is that his example FirstID is an integer also his ProductID and I am using GUID’s as my uniqueidentifier. This is obviously causing issues when I try to order by this or anything like that. I tried using another colum (AesExt varchar(50) ) but that is not working either, basically, it keeps saving the top row row for some reason. I also need to implement sorting, so when i get the procedure back in C#, I am stuffing it into a Datatable and using DataView to do the sorting. I was wondering if anyone out there had a better solution for this scenario they could share or show me what I am doing in correctly. I was also trying to find a good way to do the sorting in the procedure, but it won’t work because my boss wanted extension as varchar(50) because there are none entries (personally i think it should be 0). So when I try to sort by AesExt, when i want to convert to int so the sort works right, it will blow up at none and i know looping in a procedure is a no no, so I’m stuck doing that in C# code I think.
Store procedure
USE [Inventory]
GO
/****** Object: StoredProcedure [dbo].[usp_GetExtList] Script Date: 06/26/2011 21:07:06 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
-- =============================================
-- Author: <Author,,Name>
-- Create date: <Create Date,,>
-- Description: <Description,,>
-- =============================================
ALTER PROCEDURE [dbo].[usp_GetExtList]
-- Add the parameters for the stored procedure here
@startRowIndex INT,
@maximumRows INT,
@sortExpression AS VARCHAR(50),
@sortDirection AS VARCHAR(50),
@totalRows INT OUTPUT
AS
DECLARE @first_id INT
DECLARE @startRow INT
SET @startRowIndex = ((@startRowIndex - 1) * @maximumRows) +1
IF @startRowIndex = 1
SET @startRowIndex = 1
SET ROWCOUNT @startRowIndex
SELECT @first_id = AesExt FROM ExtItem
PRINT @first_id
SET ROWCOUNT @maximumRows
SELECT ExtensionGUID, AesExt,Name FROM ExtItem WHERE
AesExt >= @first_id
/*ORDER BY CASE WHEN @sortExpression ='Name' AND @sortDirection = 'Ascending' THEN Name
END ASC,
CASE WHEN @sortExpression = 'Name' AND @sortDirection='Descending' THEN Name
END DESC,
CASE WHEN @sortExpression = 'AesExt' AND @sortDirection = 'Ascending' THEN AesExt
END ASC,
CASE WHEN @sortExpression = 'AesExt' AND @sortDirection='Descending' THEN AesExt
END DESC,
CASE WHEN @sortExpression = 'AgentExt' AND @sortDirection = 'Ascending' THEN AgentExt
END ASC,
CASE WHEN @sortExpression = 'AgentExt' AND @sortDirection='Descending' THEN AgentExt
END DESC
*/
SET ROWCOUNT 0
— Get the total rows
SELECT @totalRows = COUNT(ExtensionGUID) FROM Extitem
C# Code
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
BindData();
}
#region BIND DATA
private void BindData()
{
string connectionString = @"Server=localhost\SQLEXPRESS;" + "Database=Inventory;Trusted_Connection=true";
SqlConnection myConnection = new SqlConnection(connectionString);
SqlCommand myCommand = new SqlCommand("usp_GetExtList", myConnection);
myCommand.CommandType = CommandType.StoredProcedure;
myCommand.Parameters.AddWithValue("@startRowIndex", currentPageNumber);
myCommand.Parameters.AddWithValue("@maximumRows", PAGE_SIZE);
myCommand.Parameters.AddWithValue("@sortExpression", SortExpression);
myCommand.Parameters.AddWithValue("@sortDirection", GridViewSortDirection);
myCommand.Parameters.Add("@totalRows", SqlDbType.Int, 4);
myCommand.Parameters["@totalRows"].Direction =
ParameterDirection.Output;
SqlDataAdapter sqlDataAdapter = new SqlDataAdapter(myCommand);
//DataSet ds = new DataSet();
// ad.Fill(ds);
DataTable dataTable = new DataTable();
sqlDataAdapter.Fill(dataTable);
DataView dataView = new DataView(dataTable);
dataView.Sort = "AesExt ASC";
gvProducts.DataSource = dataView;
gvProducts.DataBind();
// get the total rows
double totalRows = (int)myCommand.Parameters["@totalRows"].Value;
lblTotalPages.Text = CalculateTotalPages(totalRows).ToString();
lblCurrentPage.Text = currentPageNumber.ToString();
if (currentPageNumber == 1)
{
Btn_Previous.Enabled = false;
if (Int32.Parse(lblTotalPages.Text) > 0)
{
Btn_Next.Enabled = true;
}
else
Btn_Next.Enabled = false;
}
else
{
Btn_Previous.Enabled = true;
if (currentPageNumber == Int32.Parse(lblTotalPages.Text))
Btn_Next.Enabled = false;
else Btn_Next.Enabled = true;
}
}
Any help is greatly appreciated!
This has been abandoned and we are now using Select Top as the stored procedure. Word to the wise when using a GridView… it’s all or nothing, so we are just going to use it to make it look nice and handle all the data handling