SORTING使用数据库中数据的下拉列表 [英] SORTING Dropdown list that uses data from the database

查看:102
本文介绍了SORTING使用数据库中数据的下拉列表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用此代码从我的数据库中挑选出数据(仅限升序),当我只将其用于我的下拉列表中,其中包含项目但在我的下拉列表中使用时无效需要从数据库中获取其内容。



这里是代码:



//来自PAGE_LOAD OF MY ASPX.CS



I am using this code to sort out data from my database(ascending only), it does work when I am just only using it for my dropdown list with item inside it but doesn't work when using on my dropdown list which needs to get its content from the database.

here's the code:

//FROM THE PAGE_LOAD OF MY ASPX.CS

SortedList slst = new SortedList();
foreach (ListItem li in drpSchoolOff.Items)
{
    slst.Add(li.Text, li.Value);
}
drpSchoolOff.DataSource = slst;
drpSchoolOff.DataValueField = "Value";
drpSchoolOff.DataTextField = "Key";
drpSchoolOff.DataBind();





//我从.ASPX使用的下拉列表





//THE DROPDOWN LIST I'M USING FROM MY .ASPX

<asp:DropDownList ID="drpSchoolOff" runat="server" Width="200px" Enabled="True">







会发生什么事情基本上清楚我尝试删除的下拉菜单drpSch oolOff.DataBind();但排序仍然不起作用,让我再次提醒,下拉列表应该使用数据库中的数据。谢谢高级。




What happens is it basically clear the dropdown I have tried removing drpSchoolOff.DataBind(); but the sorting still doesn't work, Let me just remind again that the dropdown is supposed to be using data from the database. Thanks in advanced.

推荐答案

您好,



您可以在我创建的数据库连接下面的代码中签出我自己的收集数据源。





Hi,

You can checkout below code insted of database connection i created my own collection datasource.


// School entity which will define structure of entity
        public class Schools
        {
            public int OfficeID { get; set; }
            public String OfficeName { get; set; }
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            //Creating collection which will work as datasorce for dropdown
            //Initially it's in unsorted order.
            List<Schools> offices = new List<Schools>{ 
                new Schools{ OfficeID=2, OfficeName="USA"},
                new Schools{ OfficeID=4, OfficeName="IND"},
                new Schools{ OfficeID=1, OfficeName="SGP"},
                new Schools{ OfficeID=3, OfficeName="UK"},
            };

            //Using Lymbda expression we are creating our own asc or dec logic
            // if you want sort by name then use offices.OrderBy(x => x.OfficeName); 

            drpSchoolOff.DataSource = offices.OrderBy(x => x.OfficeID); 
            drpSchoolOff.DataValueField = "OfficeID";
            drpSchoolOff.DataTextField = "OfficeName";
            drpSchoolOff.DataBind();

        }


循环不进行任何实际排序。通过对对象列表进行排序的简单方法是:



the loop doesn't do any actual sorting. a simple way to go by sorting lists of objects would be:

...

...

{sortedlist} = {unsortedlist}.OrderBy(dc => dc.{columnOrattributevalueyouwanttosortby});

//or you could use the same unsortedlist instead of creating a new one.

{unsortedlist} = {unsortedlist}.OrderBy(dc => dc.{columnOrattributevalueyouwanttosortby});

...

//for the binding section, you may try to just set the itemsource of the dropdown list

drpSchoolOff.DataSource = {sortedlist} ;
drpSchoolOff.DataBind();

//or {unsorted} depending on current datasource.

...





然后在.aspx中绑定列值的实际属性





Then bind the actual attributes of column values in .aspx

...

<asp:DropDownList ID="drpSchoolOff" datatextfield="{attribute1}" DataValueField="{attribute2}" runat="server" Width="200px" Enabled="True">

...

</asp:dropdownlist>


在外面声明:

< prev>

静态列表<学校> offices = new List< schools> {};



然后添加:

公共类学校

{

public int OfficeID {get;组; }

public String Description {get;组; }

}

最后:

dtOffice = blSchoolT.loadSchoolAll();



foreach(DataRow r in dtOffice.Rows)

{



offices.Add(新学校{OfficeID = Convert.ToInt32( r [0]),Description = r [2] .ToString()});

}



drpSchoolOff.DataSource = offices。 OrderBy(x => x.Description);

drpSchoolOff.DataValueField =OfficeID;

drpSchoolOff.DataTextField =Description;

drpSchoolOff.DataBind();
Declared outside:
<prev>
static List<schools> offices = new List<schools> { };

Then added this:
public class Schools
{
public int OfficeID { get; set; }
public String Description { get; set; }
}
And finally:
dtOffice = blSchoolT.loadSchoolAll();

foreach (DataRow r in dtOffice.Rows)
{

offices.Add(new Schools { OfficeID = Convert.ToInt32(r[0]), Description = r[2].ToString() });
}

drpSchoolOff.DataSource = offices.OrderBy(x => x.Description);
drpSchoolOff.DataValueField = "OfficeID";
drpSchoolOff.DataTextField = "Description";
drpSchoolOff.DataBind();


这篇关于SORTING使用数据库中数据的下拉列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆