将DataGridView绑定到多个表
我的数据库中有以下三个表,我想在单个 DataGridView 中向用户显示这些表:
RecipeNames RecipeValue Ingredient
----------- ------------- ----------
PK Name FK RecipeName
FK IngredientName PK Name
Amount
用户应该看到每个 RecipeName 一行,配方名称是第一列,成分名称是后面的列。每个单元格将包含配方值的数量,每个配方都有每种成分。此外,用户必须能够编辑任何单元格,通过添加新行来添加新配方。应该看起来像这样:
Name | Ingredient 1 | ... | Ingredient n |
-----------------------------------------------
Recipe1 | 100 | ... | 1000 |
Recipe2 | 200 | ... | 2000 |
目前我正在考虑手动生成和填充网格,如下所示:
- 为成分表中的每一行添加列
- 为 RecipeNames 中的每个名称添加行
- 根据 RecipeValue 表填充单元格
然后用户可以添加 /修改表中的内容。当他点击“保存”按钮时,我再次迭代数据网格并为每一行执行适当的更新/插入操作。
这可以自动完成吗?如何将表绑定到数据网格? (我知道数据绑定如何适用于单个表/视图,但在这种情况下我不知道如何做到这一点)
此外,欢迎对设计提出任何评论。
编辑澄清:表格被简化,有更多字段,其中一个ID号作为主键,查询将受到其他参数的限制,因此只会列出少数成分。我主要想知道如何在数据网格或类似的 UI 中呈现这样的表格设计,从而最大限度地减少开发工作。
I've got the following three tables in my database which I want to display to the user in a single DataGridView:
RecipeNames RecipeValue Ingredient
----------- ------------- ----------
PK Name FK RecipeName
FK IngredientName PK Name
Amount
The user should see one row per RecipeName, the recipe name being the first column and the ingredient names the following columns. Each cell will contain the amount of a recipeValue and every Recipe has every Ingredient. Also the user must be able to edit any cell add a new recipe by adding a new row. Should look like this:
Name | Ingredient 1 | ... | Ingredient n |
-----------------------------------------------
Recipe1 | 100 | ... | 1000 |
Recipe2 | 200 | ... | 2000 |
At the moment I'm thinking about generating and filling the grid manually like this:
- add columns for each row in the ingredient table
- add rows for each Name in RecipeNames
- fill Cells according to RecipeValue table
Then the user can add / modify stuff in the table. When he hits a "Save" button I iterate over the datagrid again and do the appropriate update/insert operations for each row.
Can this be done automatically? How would you bind the tables to the Datagrid? (I know how databinding works for a single table/view, but am at a loss how to do it in this case)
Also any comment on the design is welcome.
edit to clarify: the tables are simplified, there are more fields to them, among them an ID number as primary key and the queries will be limited by other parameters so that only a few ingredients will be listed. I primarily want to know ways to present such a table design in a datagrid or similiar UI which keeps the development effort minimal.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您能否不要使用字符串作为主键或外键?这确实是很糟糕的做法,如果您使用 MSSQL(我猜),您就会降低服务器的速度。
我重命名了您的 RecipeValue,因为该表是一个“关联表”,因此这就是“As”的来源。我还向每个表添加了 ID,因为这是您应该使用的。
表AmountTypes是因为类型不同:
等等
之后,现在回到您的问题,首先为您的 getter 和 setter 生成存储过程。您希望将 SQL Server 分离为从中获取信息的“硬盘驱动器”,因为它是处理数据的最快方式。
例如:
这是第一步。如果您在那里,则可以将 SqlDatasource 添加到 GridView 中,并在站点中将存储过程添加到控件中,如下所示:
sp* 代表您自己的存储过程。
Could you please not work with strings as primary or foreign keys? That is really bad practice and if you're working with MSSQL (which I guess) you're turning down the server's speed.
I renamed your RecipeValue because this table is an "association table" so that's where the "As" comes from. I also added IDs to every table because that's what you should be working with.
The new table AmountTypes is because of the different types:
and so on.
After that and now coming back to your question, first of all you generate stored procedures for your getters and setters. You want to seperate your SQL-Server as the "Hard-Drive" you're getting information from since it's the fastest way to handle data.
For Example:
That's the first step. If you're there you can add a SqlDatasource to your GridView and in the site you add your stored procedures to the control like this:
sp* stands for your own stored procedure.