0
votes

first at all thanks for reading =)

I have a situation here, i need to create a dynamic datagridview with a sql query.

for example:

I have a table called permissions and another called, user_permissions.

So i need to show a datagridview in this way: the checkbox obviously represnt if has the permission

User Perm1 Perm2 ... Perm n
Gaston (checkbox) (checkbox) (checkbox)
Pepe (checkbox) (checkbox) (checkbox)

So i think i need a dynamic datagrid... what i did till now was:

Make a query to bring all permissions and create a datatable adding those rows as columns, so i could create the datagrid's column propertly with the names and scroll-x (there are so many, a case sentence is not good for this)

but the problem appear when i tried to create a checbox into each column of each row for each user, i dont know how to:

For Each row As DataRow In dt_strSql.Rows
 index = row("sis_codigo").ToString.Trim + row("prf_id").ToString.Trim
 dt_aux.Columns.Add(index)
Next

dt_aux.Rows.Add()
For Each column As DataColumn In dt_aux.Columns
  dt_aux.Rows(0)(column.ColumnName) = True
Next

dt_aux.AcceptChanges()

a1c_permisos_dinamico.DataSource = dt_aux
a1c_permisos_dinamico.DataBind()

For Each row As GridViewRow In Me.a1c_permisos_dinamico.Rows
  For Each column As DataColumn In dt_aux.Columns
    'here i need to transform the value  "true" that i hardcoded above (just a test)      'into a chebox checked in each column of each row for each user
  Next
Next
1
I need something like; Dim check As Checkbox row.type = check or maybe at the begging create each column of each row as checkbox but i dont know how to do that either - GDedieu
are you displaying them online, like on an aspx page? - Cory
Yes i'm exactly in a aspx page. Now i have Perm 1 Perm 2 Perm3 True True True... i need transform that into Perm1 Perm2 Checbox checkbox.. - GDedieu

1 Answers

0
votes

In the html you can set a template checkbox field:

<asp:GridView ID="yourGrid" runat="server">
    <Columns>
        <asp:TemplateField>
            <ItemTemplate >
                <asp:CheckBox runat="server" />
            </ItemTemplate>
        </asp:TemplateField>
    </Columns>
</asp:GridView>

you can add a class or postback commands for when the checkbox is clicked. If you want to populate the permission (is checked or not for each row when you query), you would need to have a permissions field (like y/n or boolean). You could "toggle" the box at binding time when each row is created:

Protected Sub yourGridBind(ByVal sender As Object, ByVal e As GridViewRowEventArgs)


      Dim chk As CheckBox = DirectCast(e.Row.FindControl("chkSelect"), CheckBox)
       If e.Row.Cells(1).Text ="y" Then
        chk.Enabled = True
       Else
      chk.Enabled = False
      End If

End Sub

This assumes that the second field is the y/n field that stores the permission