C# 根据RowDataBound上列中标签的整数值,按降序显示Gridview行

C# 根据RowDataBound上列中标签的整数值,按降序显示Gridview行,c#,asp.net,gridview,C#,Asp.net,Gridview,根据RowDataBound上列中标签的整数值,按降序显示Gridview行。我将Gridview中的一个数据绑定到一个标签“lblTitle”中,该标签工作正常,我还有另一个标签“lblMatchcount”,现在我不是从数据库中检索“lblMatchcount”的值,而是从RowDataBound上的另一个事件中获取该值。在这里,我隐藏了那些“lblMatchcount”值为“0”的行,并且我希望根据RowDataBound中“lblMatchcount”的值将这些行相应地从高到低排序。任何

根据RowDataBound上列中标签的整数值,按降序显示Gridview行。我将Gridview中的一个数据绑定到一个标签“lblTitle”中,该标签工作正常,我还有另一个标签“lblMatchcount”,现在我不是从数据库中检索“lblMatchcount”的值,而是从RowDataBound上的另一个事件中获取该值。在这里,我隐藏了那些“lblMatchcount”值为“0”的行,并且我希望根据RowDataBound中“lblMatchcount”的值将这些行相应地从高到低排序。任何经验都将不胜感激。 以下是我的设计观点:

<asp:GridView ID="GridView1" runat="server" class="UCGridView" GridLines="None" DataKeyNames="FId" CellPadding="4"
                ForeColor="#333333" OnRowDataBound="GridView1_RowDataBound">
                <AlternatingRowStyle BackColor="White" ForeColor="#284775" />
                <Columns>
                    <asp:TemplateField HeaderText="">
                        <ItemTemplate>
                            <table style="width: 90%; text-align: center; border: 0px solid red;">

                                <tr>
                                    <td>
                                        <asp:Label ID="Label2" Visible="false" runat="server" Text='<%#(Eval("Id")) %>'> </asp:Label>

                                        Title :
                                        <asp:Label ID="lblTitle" Style="font-weight: bold;" runat="server" Text='<%#(Eval("DTitle")) %>'> </asp:Label>
                                        <br />
                                        Match count :<asp:Label ID="lblMatchcount" runat="server" Text="" Visible="true"></asp:Label>

                                        <hr />
                                    </td>
                                </tr>

                            </table>

                        </ItemTemplate>
                    </asp:TemplateField>
                </Columns>
                <EditRowStyle BackColor="#999999" />
                <FooterStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" />
                <HeaderStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" />
                <PagerStyle BackColor="#284775" ForeColor="White" HorizontalAlign="Center" />
                <RowStyle BackColor="#F7F6F3" ForeColor="#333333" />
                <SelectedRowStyle BackColor="#E2DED6" Font-Bold="True" ForeColor="#333333" />

            </asp:GridView>

您可以按照我的建议使用SQL查询按降序对数据进行排序。您可以根据设置的主键对它们进行排序。在这种情况下,应将其Id设置为
主键
。因此,订购将仅基于
Id
字段

假设您的查询如下所示:

select Id, Title from your TableName
public void PopulateData(){
     string connectionName = "YourConnectionString Name here";
     string query = "select Id, Title from YourTableName Order By Id desc";

     using(SqlConnection con = new SqlConnection(connectionName)){
     using(SqlCommand cmd = new SqlCommand(query, connectionName)){

           SqlDataAdapter sda = new SqlDataAdapater(cmd);
           DataTable dt = new DataTable();
           sda.Fill(dt); 
           GridView1.DataSource = dt; // this will bind your GridView 
           GridView1.DataBind();

             }

          }

      }

您只需要添加
orderby
子句来对数据进行排序,然后是ASC | DESC,其中ASC是升序,DESC是降序

因此,您的完整查询将成为

select Id, Title 
from your Table 
ORDER BY Id DESC
DESC将根据Id对数据进行降序

如何使用它

在后端代码中,创建一个方法并调用sql查询。然后在页面加载事件上调用此方法(其中包含SQL查询)。所以应该是这样的:

select Id, Title from your TableName
public void PopulateData(){
     string connectionName = "YourConnectionString Name here";
     string query = "select Id, Title from YourTableName Order By Id desc";

     using(SqlConnection con = new SqlConnection(connectionName)){
     using(SqlCommand cmd = new SqlCommand(query, connectionName)){

           SqlDataAdapter sda = new SqlDataAdapater(cmd);
           DataTable dt = new DataTable();
           sda.Fill(dt); 
           GridView1.DataSource = dt; // this will bind your GridView 
           GridView1.DataBind();

             }

          }

      }

现在在PageLoad()事件上调用此方法
PopulateData


您可以在SQL查询本身中执行此操作。避免为它编写额外的代码。正如@noobprogrammer所说的,在将数据绑定到GridView之前对数据进行排序。好的,谢谢您的回复。实际上,数据库中没有lblMatchcount字段(列)。lblMatchcount的值在TextBox中的TextChanged事件之后填充,然后在RowDataBound中处理。我假设,如果数据库中没有条件(列或值),那么我将没有任何东西可以写入或传递查询和绑定,我希望我是有意义的,如果没有,你能告诉我你打算怎么做吗?非常感谢…谢谢,我明白你的意思了。但是我的桌子结构并不是你想要展示的样子。我在gridview中有一个标签是“lblMatchcount”,现在我不是从数据库中检索“lblMatchcount”的值,也不是从数据库中检索。lblMatchcount的值来自TextChanged事件。我有一个搜索文本框,当用户在搜索框中键入文本时,它会计算匹配的字数,并将其传递给“lblMatchcount”。然后在这里,我希望根据“lblMatchcount”中的值按降序或升序排列记录。似乎我现在必须更改表的结构,因为我看不到任何解决方案。我能做的是在我的表中添加一个名为lblMatchcount的列,当lblMatchcount获得它的值时,我会在Gridview中用页面中生成的lblMatchcount值更新表中的lblMatchcount字段,然后再次绑定Gridview。这是我现在能看到的唯一可能的解决办法。