【问题标题】:Best way to sort a gridview by age groups按年龄组对网格视图进行排序的最佳方法
【发布时间】:2016-12-12 12:46:53
【问题描述】:

我正在使用 Visual Studio 在 C# 中创建一个 asp.net Web 应用程序。我有一个注册页面,孩子们可以在其中注册,然后他们的详细信息被发送到我数据库中的“孩子”表中,点击“查看已注册的孩子”按钮后,会打开一个新页面,其中显示名字、出生日期和用户名( pk) 的每个注册儿童。按照目前的情况,DOB 被存储并读取为“04/02/2006”。我现在需要找到一种从 DOB 获取年龄并能够按年龄组(6-10、11-13、14-16)查看孩子的方法。

我想知道实现这一目标的最简单方法是什么?每个年龄组的单选按钮,即在选择时,只显示GridView中该年龄组中的孩子。或者当页面被加载并且所有的孩子都显示在网格视图上时,他们已经被分类到年龄组中?

请记住,我是 C# 和 asp.net 的新手,谁能告诉我实现这一目标的最简单方法!我附上了我的 gridview 当前外观的屏幕截图。提前致谢。

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Configuration;
using System.Data.SqlClient;

namespace Coursework
{
public partial class Testy1 : System.Web.UI.Page
{
    //create a datasource
    SqlDataSource source = new SqlDataSource();

    protected void Page_Load(object sender, EventArgs e)
    {
        //always set some defaults for the sqldatasource
        source.ID = "source1";
        SqlConnection connectionString = new SqlConnection(ConfigurationManager.ConnectionStrings["newregDBConnectionString"].ConnectionString);
        source.SelectCommand = "SELECT firstname, dob, DATEDIFF(hour, dob, GETDATE()) / 8766 AS age FROM table ORDER BY age";

        if (!IsPostBack)
        {
            //bind the grid
            GridView1.DataSource = source;
            GridView1.DataBind();
        }
    }

    protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
    {
        //the new database query, now with where clause
        source.SelectCommand = "SELECT firstname, dob, DATEDIFF(hour, dob, GETDATE()) / 8766 AS age FROM table WHERE (DATEDIFF(hour, dob, GETDATE()) / 8766 BETWEEN @start AND @end) ORDER BY age";

        //get the end age from the dropdown and cast as int
        int end = Convert.ToInt32(DropDownList1.SelectedValue);

        //get the start int for the filter
        int start = end - 5;

        //if the filter is resetted, make sure the query returns all ages
        if (end == 0)
        {
            start = 0;
            end = 99;
        }

        //replace the parameters in the query
        source.SelectParameters.Add("start", start.ToString());
        source.SelectParameters.Add("end", end.ToString());

        //rebind the grid
        GridView1.DataSource = source;
        GridView1.DataBind();
    }
}

}

【问题讨论】:

  • 对网格的数据源进行排序。
  • 您能详细说明一下吗?我以前从未使用过gridviews,对.net 和c# 完全陌生!
  • 如何将数据放入 GridView ?你bind it to the DataSource?还是你手动做的
  • 所以你应该让你的 dob 列可点击,点击时你点击(去)到你的控制器,具体属性 sortByDate ="ASC" 或 sortByDate="Desc" 取决于你的情况。当您在您的索引控制器中,并且您收到此参数 sortByDate 时,您应该使用 .OrderByDescending 或 OrderBy 这个日期查询数据。如果您使用的是 mvc。如果您使用的是 asp.net 表单,方法是相同的,但您需要在单击按钮时使用正确排序的数据重新绑定 DataGrid。
  • 我已将其绑定到数据源。我会尝试这种方法,不过我使用的是 asp.net 表单,那么如何在单击按钮时使用正确排序的数据重新绑定数据网格?

标签: c# sql asp.net gridview


【解决方案1】:

你可以从这个 sn-p 开始。您需要做的第一件事是编辑从数据库中获取数据的查询。添加以下内容:DATEDIFF(hour, dob, GETDATE()) / 8766 as AGE,这将为您提供数据库中人员的年龄。然后,您可以使用它按年龄进行过滤。

然后将一个 DropDownList 添加到页面,其中包含 OnSelectedIndexChanged 事件并将 AutoPostback 设置为 true

<asp:DropDownList ID="DropDownList1" runat="server" AutoPostBack="true" OnSelectedIndexChanged="DropDownList1_SelectedIndexChanged">
    <asp:ListItem Text="Filter age" Value="0"></asp:ListItem>
    <asp:ListItem Text="0 - 5" Value="5"></asp:ListItem>
    <asp:ListItem Text="6 - 10" Value="10"></asp:ListItem>
    <asp:ListItem Text="10 - 15" Value="15"></asp:ListItem>
</asp:DropDownList>


<asp:GridView ID="GridView1" runat="server">
    <Columns>
        <asp:TemplateField>
            <ItemTemplate>
                <%# Eval("firstname") %>
            </ItemTemplate>
        </asp:TemplateField>
        <asp:TemplateField>
            <ItemTemplate>
                <%# Convert.ToDateTime(Eval("dob")).ToString("d MMMM yyyy") %>
            </ItemTemplate>
        </asp:TemplateField>
        <asp:TemplateField>
            <ItemTemplate>
                <%# Eval("age") %>
            </ItemTemplate>
        </asp:TemplateField>
    </Columns>
</asp:GridView>

然后在后面的代码中

using System;
......

namespace Project1
{
    public partial class WebForm1: System.Web.UI.Page
    {
        //create a datasource
        SqlDataSource source = new SqlDataSource();

        protected void Page_Load(object sender, EventArgs e)
        {
            //always set some defaults for the sqldatasource
            source.ID = "source1";
            source.ConnectionString = "connectionString";
            source.SelectCommand = "SELECT firstname, dob, DATEDIFF(hour, dob, GETDATE()) / 8766 AS age FROM table ORDER BY age";

            if (!IsPostBack)
            {
                //bind the grid
                GridView1.DataSource = source;
                GridView1.DataBind();
            }
        }

        protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
        {
            //the new database query, now with where clause
            source.SelectCommand = "SELECT firstname, dob, DATEDIFF(hour, dob, GETDATE()) / 8766 AS age FROM table WHERE (DATEDIFF(hour, dob, GETDATE()) / 8766 BETWEEN @start AND @end) ORDER BY age";

            //get the end age from the dropdown and cast as int
            int end = Convert.ToInt32(DropDownList1.SelectedValue);

            //get the start int for the filter
            int start = end - 5;

            //if the filter is resetted, make sure the query returns all ages
            if (end == 0)
            {
                start = 0;
                end = 99;
            }

            //replace the parameters in the query
            source.SelectParameters.Add("start", start.ToString());
            source.SelectParameters.Add("end", end.ToString());

            //rebind the grid
            GridView1.DataSource = source;
            GridView1.DataBind();
        }
    }
}

【讨论】:

  • 谢谢你,我正在下班回家的路上,我一回家就试试这个。我会在这里发表评论,告诉你我是怎么过的。再次感谢!
  • 嗨@VDWWD,我终于开始实施你的代码了,但我遇到了你给我的第一行代码的问题。目前,该页面只有一个gridview,我已经在其中配置了数据源,因此目前还没有代码。那么我将把这段代码放在哪里来编辑我的查询呢?谢谢!
  • 另外,您是说我需要禁用我的 gridview 的当前数据源并使用您提供的代码手动添加它吗?如果是这样,那不会禁用我的删除链接按钮吗?
  • 我在我的示例中使用了SqlDataSource,因为它似乎来自您正在使用的一个 cmets。但是是的,最好为网格使用单个数据源。当然还有其他的可能性来绑定一个排序的 GridView 数据
  • 好的,我的 .cs 文件中没有代码,因为我只是使用简单的 DataSource 来填充 gridview 和删除函数。至于第一行代码,我该如何去实现它以允许我尝试其余的代码?
猜你喜欢
  • 2019-02-23
  • 1970-01-01
  • 2017-12-31
  • 1970-01-01
  • 2021-03-19
  • 2010-09-07
  • 2021-01-04
  • 2011-11-21
  • 2017-05-23
相关资源
最近更新 更多