在asp中对GridView中的数据进行排序

时间:2022-11-26 01:35:43

I have a GridView that is filled with data from a database. I get My data using a mssql function. This is how i am getting my data:

我有一个GridView,它充满了数据库中的数据。我使用mssql函数获取我的数据。这就是我获取数据的方式:

private void GetNoLocationScanAddress(GridView grid, int week)
        {
            try
            {
                FunctionDataContext function = new FunctionDataContext();
                List<Fn_GetNoLocationAddressGroupedResult> address = function.Fn_GetNoLocationAddressGrouped("0X", week).ToList();
                grid.DataSource = address;
                grid.DataBind();
            }
            catch (Exception exception)
            {
                Console.WriteLine("ERROR in GetNoLocationScanAddress() method. Error Message : " + exception.Message);
            }
        }

The Problem is i am already sorting the data in my function. I am also using a Switch case to determine what data is selected for viewing.

问题是我已经在我的函数中对数据进行排序。我还使用Switch案例来确定选择哪些数据进行查看。

 protected void WeekSelector_OnIndexChanhed(object sender, EventArgs e)
        {
            switch (RadioButtonList.SelectedIndex)
            {
                case 0:
                    GetScanErrorsAddress(Grid, Convert.ToInt32(WeekSelector.SelectedValue));
                    Grid.DataBind();
                    break;

                case 1:
                    GetScanErros(Grid, Convert.ToInt32(WeekSelector.SelectedValue));
                    Grid.DataBind();
                    break;

                case 2:
                    GetNoLocationScanAddress(Grid, Convert.ToInt32(WeekSelector.SelectedValue));
                    Grid.DataBind();
                    break;

                case 3:
                    GetNoLocationScan(Grid, Convert.ToInt32(WeekSelector.SelectedValue));
                    Grid.DataBind();
                    break;
            }
        }

Function sql:

函数sql:

SELECT     TOP (100) PERCENT Request1.RequestID, TrackIT.dbo.Sending.Barcode, TrackIT.dbo.Address_View.AdrID, TrackIT.dbo.Address_View.Name, 
                      TrackIT.dbo.Address_View.Street, TrackIT.dbo.Address_View.HouseNo, TrackIT.dbo.Address_View.Postal, TrackIT.dbo.Address_View.City, 
                      TrackIT.dbo.Address_View.Country, Request1.Latitude +','+  Request1.Longitude AS Location,  Case when(Request1.Latitude<>'')then cast(1 as bit) else cast(0 as bit) end AS showLocation, Request1.ReceivedDate
FROM         (SELECT DISTINCT RequestID, LTRIM([Content]) AS Barcode, Latitude, Longitude, ReceivedDate
                       FROM          dbo.RequestWithLocation
                       WHERE      (Site LIKE @Site) AND ([Content] <> '') AND (AddressID = '0') AND (ReceivedDate > DATEADD(day, -@ReceivedDate, GETDATE()))) AS Request1 INNER JOIN
                      TrackIT.dbo.Sending ON Request1.Barcode = TrackIT.dbo.Sending.Barcode INNER JOIN
                      TrackIT.dbo.Address_View ON TrackIT.dbo.Sending.DeliveryAdrID = TrackIT.dbo.Address_View.AdrID

ORDER BY TrackIT.dbo.Address_View.AdrID

Can anyone help me with this ?

谁能帮我这个 ?

2 个解决方案

#1


1  

You can use - Linq (OrderBy or OrderByDescending), to sort your List<Fn_GetNoLocationAddressGroupedResult>.

您可以使用--Linq(OrderBy或OrderByDescending)对List 进行排序。

Eg.

例如。

address.OrderBy(x => x.Country);            // ascending
address.OrderByDescending(x => x.Country);  // descending

#2


1  

From your comments, if the user has to be able to sort the gridview by pressing the column header and if you are using a standard ASP.NET System.Web.UI.WebControls.GridView, you can simply "Enable Sorting", in the properties pane by setting "AllowSorting" to true or via the "pop-out" on the top right of the control by checking "Enable Sorting".

根据您的注释,如果用户必须能够通过按列标题对gridview进行排序,并且如果您使用的是标准的ASP.NET System.Web.UI.WebControls.GridView,则只需“启用排序”即可。通过选中“启用排序”,将“AllowSorting”设置为true或通过控件右上角的“弹出”设置属性窗格。

#1


1  

You can use - Linq (OrderBy or OrderByDescending), to sort your List<Fn_GetNoLocationAddressGroupedResult>.

您可以使用--Linq(OrderBy或OrderByDescending)对List 进行排序。

Eg.

例如。

address.OrderBy(x => x.Country);            // ascending
address.OrderByDescending(x => x.Country);  // descending

#2


1  

From your comments, if the user has to be able to sort the gridview by pressing the column header and if you are using a standard ASP.NET System.Web.UI.WebControls.GridView, you can simply "Enable Sorting", in the properties pane by setting "AllowSorting" to true or via the "pop-out" on the top right of the control by checking "Enable Sorting".

根据您的注释,如果用户必须能够通过按列标题对gridview进行排序,并且如果您使用的是标准的ASP.NET System.Web.UI.WebControls.GridView,则只需“启用排序”即可。通过选中“启用排序”,将“AllowSorting”设置为true或通过控件右上角的“弹出”设置属性窗格。