How to display group data separately using DataList in ASP.NET?

落爺英雄遲暮 提交于 2019-12-10 23:36:37

问题


I have a sql table which have the following data,

Id   City      Country 
---  ------    ------------
1    Delhi     India
2    New York  United States
3    Karachi   Pakistan
4    Mumbai    India
5    Lahore    Pakistan
6    Kanpur    India
7    Delhi     India
8    Mumbai    India

Now, I want to display the above data in my web app as displayed below;

India
Delhi (2)    Mumbai (2)    Kanpur (1)    

United States
New York (1)

Pakistan
Karachi (1)    Lahore (1)

Please tell me:

  • The SQL query which will fetch the data as I want. I want City, Country and Count (grouping of all cities)
  • And how to display the fetched data in the format I given above in ASP.NET C#. Is there any control which we can use to display the data as I want. Or we have to write any customized code, if customized code then please tell me the code for this.

回答1:


Your SQL should be


select country,city,count(city)
from dbo.location 
group by country,city order by country

Then use datarepeter to display your data. Follow this link




回答2:


You can use a DataSet, normalize your DB, read two tables into it and then display it with two nested DataRepeater, just like two for-loops would do.



来源:https://stackoverflow.com/questions/523345/how-to-display-group-data-separately-using-datalist-in-asp-net

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!