简体   繁体   中英

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. I want to display the names for the modules associated with a project in a 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. 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...

2) let mysql handle that for you:

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

1) Create a class Named MyClass.ie

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM