gpt4 book ai didi

c# - 无法使用 DropDownList 按价格对数据进行排序

转载 作者:太空宇宙 更新时间:2023-11-03 11:19:06 24 4
gpt4 key购买 nike

我正在尝试按价格对我的数据(在数据列表中)进行排序。这意味着如果用户想要从最贵到最便宜的价格,则有 DropDownList 供用户选择,反之亦然。存储在我的数据库中的价格是根据我的 catID 随机排列的。我写了如下代码,但它没有按照我写的进行排序。我在这里做错了什么?请给我建议。

protected void Page_Load(object sender, EventArgs e)
{

}

protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
{
bindDropDownList();
}

private void bindDropDownList()
{
DropDownList1.DataTextField = "price";
DataList1.DataSourceID = null;
DataList1.DataSource = getReader();
DropDownList1.DataBind();

}

private SqlDataReader getReader()
{
SqlDataReader reader = null;

if(DropDownList1.Text == "-Select-")
{
string strConnectionString =
ConfigurationManager.ConnectionStrings["ProBizConnection"].ConnectionString;
SqlConnection myConnect = new SqlConnection(strConnectionString);

string strCommandText ="SELECT * FROM [Category ] WHERE catID<= 20";

SqlCommand cmd = new SqlCommand(strCommandText, myConnect);
myConnect.Open();

reader = cmd.ExecuteReader(CommandBehavior.CloseConnection);

}
else if (DropDownList1.SelectedValue == "Price - Highest to Lowest")
{
string strConnectionString =
ConfigurationManager.ConnectionStrings["ProBizConnection"].ConnectionString;
SqlConnection myConnect = new SqlConnection(strConnectionString);

string strCommandText = "SELECT catID, packageName, price, description1, description2, image1, image2 FROM Category WHERE catID <= 20 ORDER BY price desc";

SqlCommand cmd = new SqlCommand(strCommandText, myConnect);
myConnect.Open();

reader = cmd.ExecuteReader(CommandBehavior.CloseConnection);

}

else if (DropDownList1.DataTextField == "Price - Lowest to Highest")
{
/string strConnectionString =
ConfigurationManager.ConnectionStrings["ProBizConnection"].ConnectionString;
SqlConnection myConnect = new SqlConnection(strConnectionString);

string strCommandText = "SELECT catID, packageName, price, description1, description2, image1, image2 FROM Category WHERE catID <= 20 ORDER BY price";

SqlCommand cmd = new SqlCommand(strCommandText, myConnect);
myConnect.Open();

reader = cmd.ExecuteReader(CommandBehavior.CloseConnection);

}
return reader;
}

我的 .aspx 代码:

<asp:DropDownList ID="DropDownList1" runat="server" AutoPostBack="True" AppendDataBoundItems="true" 
onselectedindexchanged="DropDownList1_SelectedIndexChanged" Height="18px"
Width="184px">
<asp:ListItem>-Select-</asp:ListItem>
<asp:ListItem>Price - Highest to Lowest</asp:ListItem>
<asp:ListItem>Price - Lowest to Highest</asp:ListItem>
</asp:DropDownList>

最佳答案

排序在 Price 列上不正确可能是因为 Price 的数据类型不是小数,可能是 Varchar

所以请将 Price 数据类型更改为 Decimal 以正确排序

关于c# - 无法使用 DropDownList 按价格对数据进行排序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11611641/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com