开发者

Rows showing up multiple times in GridView

开发者 https://www.devze.com 2023-04-08 21:45 出处:网络
I have a GridView control on my web page which makes use of paging. I am seeing apparently duplicate rows appearing, but I know they are not in the data, and they seem to disappear whenever I sort on

I have a GridView control on my web page which makes use of paging. I am seeing apparently duplicate rows appearing, but I know they are not in the data, and they seem to disappear whenever I sort on a different column from the default one I'm using. However, they reappear when sorting on this original column again.

Here's a snippet of the ascx;

<asp:GridView
    ID="gvResults"
    AllowPaging="True"
    CssClass="DataTable"
    runat="server"
    AutoGenerateColumns="False"
    OnRowDataBound="gvResults_RowDataBound"
    AllowSorting="True"
    Width="750px"
    OnSorting="gvResults_Sorting"
    PagerSettings-Mode="NumericFirstLast"
    PagerSettings-FirstPageText="<<"
    PagerSettings-LastPageText=">>"
    PagerSettings-PageButtonCount="5"
    PagerSettings-Position="Bottom"
    PagerStyle-CssClass="paginationContainer"
    PagerStyle-Ho开发者_JAVA技巧rizontalAlign="Left"
    OnPageIndexChanging="gvResults_PageIndexChanging">

<PagerSettings 
    FirstPageText="&lt;&lt;" 
    LastPageText="&gt;&gt;" 
    Mode="NumericFirstLast"
    PageButtonCount="5" />

<PagerStyle CssClass="paginationContainer" HorizontalAlign="Left" />

That might be irrelevant to my issue, but I'm including it just in case.

Now, I believe that the issue is possibly being caused because there is a particular scenario in which the column on which the grid is sorted by default, will be populated with an identical value for many rows. That might sound strange, but the column is a proximity, and the grid is displaying proximity search results, so it is possible that some searches are going to return many results with the same proximity.

The issue of the duplicate results only happens once the number of rows with an identical proximity exceeds the number of results displayed per page (in my case, 10).

Whenever this happens, I see a row appear on, e.g. the 2nd page, and also on the 3rd page (never on the same page however). Sorting on a different column and paging through the results seems to eradicate the issue.

Now, after much head / wall interfacing, I have arrived at a speculative explanation;

That this is totally expected behaviour because there is no specified way to sort the rows when the fields are identical. Some other mechanism must be deciding how to sort the rows, and whatever this mechanism is causes the duplicate issue whenever paging through results.

Am I on the right track here? And if so, how can I address this issue? Is there some way to sort on a secondary column in addition to the user specified one?


I don't know whether you are on the right track or not because w/o looking at the data it's difficult to tell, but to your question regarding sorting by a secondary column, you definitely can do this easily:

  1. If you are using a DataTable to bind your data, you can order by two columns with something like this on your gvResults_Sorting event:

    Expression<Func<DataRow,object>> expCol1 = row => row["SortExpressionFirstColumn"];
    Expression<Func<DataRow,object>> expCol2 = row => row["SortExpressionSecondColumn"];
    
    OrderedEnumerableRowCollection<DataRow> result = yourTabale.AsEnumerable().OrderBy(expCol1.Compile()).ThenBy(expCol2.Compile());
    
    DataTable sortedTable = new DataTable();
    foreach (var item in result)
    {
            sortedTable.ImportRow(item);
    }
    gvResults.DataSource=sortedTable;
    gvResults.DataBind();
    
  2. Or you don't get so fancy and use a DataView to sort the datatable:

    DataTable t ....
    t.DefaultView.Sort="SortExpressionColumn1 ASC/DESC , SortExpressionColumn2 ASC/DESC";
    gvResults.DataSource=t.DefaultView.ToTable();
    gvResults.DataSource=t.DataBind();
    
  3. If you are using custom business objects, much easier:

    List<CutomObject> co = ....
    
    co=co.OrderBy(x => x.FirstProperty).ThenBy(x => x.SecondProperty);
    gvResults.DataSource=co;
    gvResults.DataBind();
    
0

精彩评论

暂无评论...
验证码 换一张
取 消