简体   繁体   English

C# MySql ASP.NET 从数据库中的行中获取项目并将它们放入 Z4E9F7F44562A881FE61E0ZBFFE0

[英]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.

相关问题 将gridview的所有行保存到数据库asp.net C# - save all rows of gridview to database asp.net c# 如何从 asp.net c# 中的 gridview 中提取行 - How to extract rows from gridview in asp.net c# ASP.NET Datagrid从数据库C#中检索选择的项目,并使它们像表一样排序 - ASP.NET Datagrid retrieve choosed items from database C# and make them sorted like table Gridview 仅显示 MySQL 数据库中的 1 行 - ASP.net ZD7EFA19FBE7D3972FD5ADB6024223D7 - Gridview is showing only 1 row from MySQL database - ASP.net C# ASP.Net如何从gridview C#更新数据库 - ASP.Net How to Update a database from a gridview C# ASP.NET C#将图像从数据库绑定到GridView - ASP.NET C# Binding Images to GridView from Database 过滤(或隐藏)Gridview行时,Asp.net / c#-&gt; Gridview Pager无法更新 - Asp.net/c# -> Gridview Pager doesn't get updated when gridview rows are filtered (or hidden) 选中从gridview1到gridview2的所选行ASP.NET C# - checkboxed selected rows from gridview1 to gridview2 ASP.NET C# 在RadioButtonList中选择一个项目,如何从asp.net中的数据库获取到ListBox的项目列表c# - Selecting an item in the RadioButtonList, how to get a list of items to a ListBox from database in asp.net c# asp.net c#将ArrayList发送到gridview不显示项目 - asp.net c# send ArrayList to gridview not displaying items
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM