【问题标题】:Sorting and paging issues with SQL Stored procedures and GridViewSQL 存储过程和 GridView 的排序和分页问题
【发布时间】:2011-06-27 01:42:23
【问题描述】:

我一直在尝试在谷歌上搜索一个好的解决方案,但我目前的项目在 ASp GridView 控件 (.NET 4.0) 上实现自定义分页和排序方面收效甚微。

这是我尝试用作完成此任务的最新站点: http://www.codeproject.com/KB/webforms/GridViewCustomPaging.aspx

这个解决方案最大的问题是他的示例 FirstID 是一个整数,也是他的 ProductID,我使用 GUID 作为我的唯一标识符。当我尝试按此或类似的方式订购时,这显然会导致问题。我尝试使用另一个列(AesExt varchar(50)),但这也不起作用,基本上,它出于某种原因一直保存第一行。我还需要实现排序,所以当我在 C# 中返回该过程时,我将其填充到 Datatable 中并使用 DataView 进行排序。我想知道是否有人对这种情况有更好的解决方案,他们可以分享或向我展示我在做什么正确。我也试图找到一种在程序中进行排序的好方法,但它不起作用,因为我的老板想要扩展为 varchar(50),因为没有条目(我个人认为它应该是 0)。因此,当我尝试按 AesExt 排序时,当我想转换为 int 以便排序正常工作时,它会崩溃,而且我知道在过程中循环是不行的,所以我坚持在 C# 代码中这样做我想。

存储过程

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

-- 获取总行数

SELECT @totalRows = COUNT(ExtensionGUID) FROM Extitem

C#代码

 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;
        }
    }

非常感谢任何帮助!

【问题讨论】:

  • Ranhiru ...您是想发布一些东西吗?它没有出现在这里,我从来没有收到电子邮件

标签: c# sql-server sorting stored-procedures gridview


【解决方案1】:

这已被放弃,我们现在使用 Select Top 作为存储过程。使用 GridView 时明智的说法......要么全有,要么全无,所以我们只是要使用它来使它看起来不错并处理所有数据处理

【讨论】:

    猜你喜欢
    • 2012-02-06
    • 2012-08-23
    • 2012-03-14
    • 1970-01-01
    • 1970-01-01
    • 2010-11-05
    • 1970-01-01
    • 2016-02-20
    • 1970-01-01
    相关资源
    最近更新 更多