如何从 2 table 构建 gridview 并在 asp.net 网络表单中的第 3 table 保存坐标映射?

How to build the gridview from 2 tables and save co-ordinates mapping in 3'rd table in asp.net web form?

我在数据库中有组件 table、角色 table 和 ComponentRoleMapping table。请看下图。我想将组件名称列填充为填充所有行的第一列,角色应该是角色 table 中网格的第一行。所有单元格都必须有复选框。矩阵坐标必须通过单击任何复选框将坐标保存在 ComponentRoleMapping table 中。简而言之,要填充组件和角色之间的多对多关系,我需要这个视图。我正在使用 asp.net 网络表单和 gridview 控件来填充它。如何从 2 tables / datatables 填充单个 gridview?或者我可以在这里使用的任何其他控件。或者我可以出于确切目的关注的任何文章?

您需要一个 RoleComponent table,它是将组件映射到角色的简单 table pk。示例 table:

CREATE TABLE [dbo].[RoleComponent](
    [role_pk] [int] NOT NULL,
    [component_pk] [int] NOT NULL
) ON [PRIMARY]

如果此 table 中的条目针对特定角色和组件存在,则最初会选中一个复选框。

当用户:

  • 选中一个复选框:为给定的 role_pk 和 component_pk
  • 插入一行
  • 取消选中复选框:删除给定 role_pk 和 component_pk
  • 的行

要获得显示中的复选框网格,您将需要 return 位字段的枢轴 table。 Pivot table在SqlServer中并不简单。事实上,它们非常烦人。

Repeater 可能是更好的选择。

我在回答我自己的问题,可能对其他人有帮助。为了使用网格控件,我只使用 Html table。 这里 Dt1 有组件,Dt2 有角色,Dt3 用于检查活动状态。这可能是一些死代码,但是,运行 完美。

Private Sub GenerateTable(dt2 As DataTable, dt1 As DataTable, dt3 As DataTable)

        Dim table As New Table()
        Dim row As TableRow = Nothing    
        'Add the Headers
        row = New TableRow()            
        'Empty 
        Dim headerCellEmpty As New TableHeaderCell()
        headerCellEmpty.Text = ""
        headerCellEmpty.Attributes("style") = "border:1px solid black;text-align:center;"
        row.Cells.Add(headerCellEmpty)    
        For i As Integer = 0 To dt2.Rows.Count - 1
            Dim headerCell As New TableHeaderCell()
            headerCell.Text = dt2.Rows(i)(0).ToString()
            headerCell.ID = dt2.Rows(i)(1).ToString()
            'Check All Components
            Dim chkAllComponent As New CheckBox()
            chkAllComponent.ID = "allcomponents_" + dt2.Rows(i)(1).ToString()
            chkAllComponent.Text = dt2.Rows(i)(0).ToString()
            headerCell.Controls.Add(chkAllComponent)

            headerCell.Attributes("style") = "border:1px solid black;text-align:center;width:15%;"
            row.Cells.Add(headerCell)
        Next
        table.Rows.Add(row)

        'Add the Column values
        For i As Integer = 0 To dt1.Rows.Count - 1
            row = New TableRow()
            For j As Integer = 0 To dt1.Columns.Count - 2
                Dim cell As New TableCell()
                cell.Text = dt1.Rows(i)(j).ToString()
                cell.ID = dt1.Rows(i)(j + 1).ToString()
                'Check All Roles
                Dim chkAllRoles As New CheckBox()
                chkAllRoles.ID = "allroles_" + dt1.Rows(i)(j + 1).ToString()
                chkAllRoles.Text = dt1.Rows(i)(j).ToString()
                cell.Controls.Add(chkAllRoles)

                row.Cells.Add(cell)
            Next

            ' Add the TableRow to the Table
            table.Rows.Add(row)
        Next

        If table.Rows.Count > 0 Then
            If table.Rows(0).Cells.Count > 0 Then


                'Add check boxes
                Dim allColumnsCountinTable As Integer = table.Rows(0).Cells.Count
                For Each tr As TableRow In table.Rows
                    Dim rowIndex As Integer = table.Rows.GetRowIndex(tr)
                    If rowIndex = 0 Then
                    Else
                        For f As Integer = 1 To allColumnsCountinTable - 1
                            Dim cell As New TableCell()
                            Dim roleid = table.Rows(rowIndex).Cells(0).ID
                            Dim roleId1 As Guid = New Guid(roleid)
                            Dim componentid = table.Rows(0).Cells(f).ID
                            Dim Check1 As New CheckBox()
                            Check1.ID = "chk_" + roleid + "_" + componentid


                            Dim configurations = From p In dt3.AsEnumerable() Select p

                            If (configurations.Any(Function(coffee) c.Field(Of Integer)("ComponentId") = componentid And c.Field(Of Guid)("RoleId") = roleId1)) Then

                                Dim isActive = configurations.Where(Function(coffee) c.Field(Of Integer)("ComponentId") = componentid And c.Field(Of Guid)("RoleId") = roleId1).FirstOrDefault()("IsActive")
                                If isActive = True Then
                                    Check1.Checked = True
                                Else
                                    Check1.Checked = False
                                End If
                            Else
                                Check1.Checked = False
                            End If
                            Check1.Attributes("Name") = "checkbox"
                            cell.Attributes("style") = "border:1px solid black;text-align:center;"
                            cell.Controls.Add(Check1)
                            tr.Cells.AddAt(f, cell)
                        Next
                    End If
                    tr.Attributes("style") = "color:red; border:1px solid black;"
                Next
            End If
        End If

        'Styling


            Panel1.Controls.Add(table)

    End Sub