简体   繁体   English

表,SQL C#通过在datagridview中选择一行将数据添加到“ FK表”

[英]Tables, SQL C# Adding data to “FK table” by selecting a row in datagridview

I have 2 tables that have a Datagridview , I want the second datagridview to fill up with data depending on which row that is selected on the first datagridview . 我有2个具有Datagridview表,我希望第二个datagridview填充数据,具体取决于在第一个datagridview上选择的哪一行。 I also want to filter it after Column , Name , etc. 我也想在ColumnName等之后过滤它。

(Add data to the second table in relation to first ID) So I established a relation between these tables with pk and fk . (将有关第一个ID的数据添加到第二个表中)因此,我使用pkfk在这些表之间建立了关系。 I want to add Data to the second table by selecting value from the first table(by datagridview) 我想通过从第一个表中选择值来将数据添加到第二个表中(通过datagridview)

I always do following 我一直都在关注

  1. I have an ID column (could be invisible) in the main DataGridView 我在主DataGridView中有一个ID列(可能不可见)

  2. I use CellClick event to fire a LoadDetail() sub 我使用CellClick事件来触发LoadDetail()子

  3. In the LoadDetail() sub I use DataGridView1.SelectedRows(0).Cells("ID").value parameter (providing your DGW is in FullRowSelect mode), but you could also pass it as a parameter (Me.DataGridView1.rows(e.RowIndex).Cells("ID").value) in the above CellClick event function. 在LoadDetail()子目录中,我使用DataGridView1.SelectedRows(0).Cells(“ ID”)。value参数(假设您的DGW处于FullRowSelect模式),但是您也可以将其作为参数传递(Me.DataGridView1.rows(上面的CellClick事件函数中的e.RowIndex).Cells(“ ID”)。value)。

  4. LoadDetail() is a standalone sub which loads detailed data for the selected row depending on the selected row or given parameter. LoadDetail()是一个独立的子程序,它根据选定的行或给定的参数为选定的行加载详细数据。 This way, it can also be used for a "Refresh" button or for automatic updates, ie after saving changes. 这样,它也可以用于“刷新”按钮或自动更新,即保存更改后。

If you wanted to update the second DataGridView based on shifting rows by arrows, you have to use KeyUp in a similar fashion. 如果要基于箭头移动行来更新第二个DataGridView,则必须以类似的方式使用KeyUp。

The FK can help in the SQL query, but I think is not important for the way you handle loading of the second DataGridView. FK可以帮助进行SQL查询,但是我认为这对于处理第二个DataGridView的加载方式并不重要。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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