[英]C# MySql ASP.NET Get items from rows in the database and put them in a gridview
I have a table in my database which contains the modules in a project.我的数据库中有一个表,其中包含项目中的模块。 It is a many to many relationship with the module id which is a foreign to the modules table and the project key which is a foreign key to the projects table.
它与模块表的外键模块 id 和项目表的外键项目键是多对多的关系。 I want to display the names for the modules associated with a project in a gridview.
我想在 gridview 中显示与项目关联的模块的名称。 I have the first bit of code done but I don't know how to take in and store all the modules id and then how to get their names in the module table and put them in the gridview.
我已经完成了第一段代码,但我不知道如何获取和存储所有模块 ID,然后如何在模块表中获取它们的名称并将它们放入 gridview。 Any help would be much appreciated.
任何帮助将非常感激。
there are several ways to do this...做这件事有很多种方法...
1) read your module table into a Dictionary, and resolve the needed Names from there when you need them... 1)将您的模块表读入字典,并在需要时从那里解析所需的名称......
2) let mysql handle that for you: 2) 让 mysql 为您处理:
since you provided no table schema you will probably have to adapt the query...由于您没有提供表模式,因此您可能必须调整查询...
SELECT
ProjectsTable.Name AS 'Project',
GROUP_CONCAT(COALESCE(ModulesTable.Name,'None') SEPARATOR ', ') AS 'Modules'
FROM ProjectsTable
LEFT JOIN ProjectsModulesTable ON ProjectsTable.id = ProjectsModulesTable.projectId
LEFT JOIN ModulesTable ON ProjectsModulesTable.moduleId = ModulesTable.id
(query not tested) (查询未测试)
3) use some framework (eg entity framework) and build objects for your database entities and access the names by those objects 3)使用一些框架(例如实体框架)并为您的数据库实体构建对象并通过这些对象访问名称
1) Create a class Named MyClass.ie 1) 创建一个名为 MyClass.ie 的 class
public class MyClass
{
public int MyModules{ get; set; }
}
2) Now in the Main Block
List<MyClass> lstObj = new List<MyClass>();
// add the modules one by one
MyClass obj = new MyClass();
obj.MyModules= // your code to add modules
lstObj.add(obj);
// after your logic and filling the List.
gridView1.datasource = lstObj;
gridView1.databind();
its done now.
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.