如何基于DataTable填充下拉列表我有下面的代码,用从SP和CachedTable获取的值填充一个ASP.net标签:publicvoidUpdateDropDownList(){stringstrQuery="";使用(SqlConnectionscCon=newSqlConnection(connString)){使用(SqlCommandscCmd=newSqlCommand("cation",scCon)){SqlDataAdaptersda=newSqlDataAdapter();DataTabledt=newDataTable();scCmd.CommandType=CommandType.StoredProcedure;scCmd.Parameters.Add("@ation",SqlDbType.VarChar).Value=cation.SelectedItem.Value;sda.SelectCommand=scCmd;sda.填充(dt);vardistinctValues=dt.AsEnumerable()。Select(row=>row.Field("Specialty")).Distinct();Label1.Text="所有专业";Label1.Text+=string.Join("",distinctValues);//显示与位置相关的所有特性vark=distinctValues.ToArray();strQuery="(";for(inty=0;yrow.Field("Name")).Distinct();Label1.Text+="AllProviders";Label1.Text+=string.Join("",distinctValues2);//显示与位置相关的所有提供者strQuery="([Specialty]='所有专业'";for(inty=0;yrow.Field("Topic")).Distinct();Label1.Text+="所有主题";Label1.Text+=string.Join("",distinctValues3);//显示与该位置的专业相关的所有主题Session.Add("DTTableLocation",dt);我有四个下拉列表:上面的C#代码是针对Location_SelectedIndexChanged执行的如何替换以下内容:Label1.Text="AllSpecialties";Label1.Text+=string.Join("",distinctValues);//显示与位置相关的所有专业填充专业下拉列表(DataField和DataValue是值)。Label1.Text+="所有供应商";Label1.Text+=string.Join("",distinctValues2);//显示与位置相关的所有提供者填充名称下拉列表(DataField和DataValue是值)。Label1.Text+="所有主题";Label1.Text+=string.Join("",distinctValues3);//显示与该位置的专业相关的所有主题填充主题下拉列表(DataField和DataValue是值)。Specialty.DataSource=distinctValuesSpecialty.DataBind()您可能需要调整DataTextField和DataValueField属性,但我不知道它们应该是什么。对所有三个下拉列表执行相同的操作。我还将这段代码分成了单独的函数。也许GetSpecialties从数据库中检索所有专业。GetProviders构建查询并从缓存表中返回提供程序。GetTopics构建该查询并返回主题。这将使主要功能更短,更容易理解。因为现在读起来很困难。以上就是C#学习教程:如何基于DataTable填充下拉列表。如果对大家有用,需要进一步了解C#学习教程,希望大家多多关注——vardistinctSpecialties=GetSpecialties();Specialty.DataSource=distinctSpecialties;Specialty.DataBind();vardistinctProviders=GetProviders(distinctSpecialties);Name.DataSource=distinctProviders;名称.DataBind();vardistinctTopics=GetTopics(distinctSpecialties);Topic.DataSource=distinctTopics;主题.DataBind();收藏不代表立场,如涉及侵权,请点击右侧联系管理员删除。如需转载请注明出处: