编辑 GridView 列的代码 asp.net
我一直在寻找一种方法来控制 GridView 特定列中显示的文本。
例如,考虑一个包含两个表 Student 和 Class 的数据库。
我想在GridView中添加一列,打印出数据库中的所有学生,该列将显示学生的班级名称,该怎么做? (我通常可以打印ClassId,因为它是学生表中的FK)
我想在GridView中添加一列来打印所有班级,该列将计算每个班级的学生人数,我该怎么办
I've been looking for a way to control the text that appears in a particular column of a GridView.
For example consider a database with two tables Student and Class.
I want to add a column to the GridView which print out all the students in the Database, the column will show the student's class name, how can do it? (I can normally print the ClassId since its a FK in the student table)
I want to add a column to the GridView which print all the classes, the column will count the number of students in each class, how can I do it?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
1-您可以简单地使用内部联接或存储过程来获取查询中所需的所有数据旁边的类名。
2- 不止一种方法可以做你想做的事:
例如:
您可以向数据表添加一列(空列),并稍后通过在查询中使用
Sum()
聚合函数来填充它。返回自定义数据表(作为数据源)后,您可以将其绑定到网格视图。
另一种解决方案:向网格视图添加一个空列,然后在网格视图的
RowDataBound
事件,您可以通过一些循环填充此列,并且可以使用LINQ
来帮助您获得求和。1- You can do that simply with inner join or a stored procedure to get the class name beside all the data you need in your query.
2- More than one way to do what you want:
For example:
you can add a column to your data table (empty column ), and fill it later through using
Sum()
aggregate function in a query.After you return your customized data table(as a data source), you can bind it to the grid view.
Another solution : add an empty column to the grid view and in the
RowDataBound
event of the grid view you can fill this column through some loop and you can useLINQ
to help you in getting the summation.