如何使用datagridview复选框更新sql表

时间:2015-08-13 11:59:15

标签: vb.net

我正在处理基于用户权限的菜单和表单上的限制。我想知道如何从datagridview复选sql server中的表 我已显示结果,但想要在取消选中复选框时知道如何编辑表格。 这是我的表单enter image description here

的图像

这是我到目前为止所做的事情

Private Sub FillGrid()

    Try

        Dim dt As New DataTable
        Dim strSqll As String
        'strSqll = "Select ugr.programid,ugr.Userid,ugr.menuid,mf.menuname,ugr.ChkAdd, ugr.ChkEdit, ugr.ChkDel, ugr.ChkPrint, ugr.ChkView from UserRights ugr inner join menus mf on(mf.menuid=ugr.menuid and mf.programid=ugr.programid) Where ugr.UserID='admin'"
        strSqll = "Select ugr.programid,ugr.Userid,ugr.menuid,mf.menuname, " _
           & "ugr.ChkAdd,ugr.ChkEdit,ugr.ChkDel,ugr.ChkPrint,ugr.ChkView " _
           & "from UserRights ugr inner join menus mf on(mf.menuid=ugr.menuid and mf.programid=ugr.programid) " _
           & "Where ugr.UserID='" & cmbUserName.SelectedValue.ToString() & "'"
        Dim cmd As New SqlCommand(strSqll, cnMain.dbconnection())
        cmd.CommandType = CommandType.Text
        Dim da As New SqlDataAdapter(cmd)

        da.Fill(dt)
        dgvUserrights.DataSource = dt
        SettingGridColumns()
    Catch ex As Exception
        MsgBox(ex.Message)
    End Try

End Sub

Private Sub SettingGridColumns()
    dgvUserrights.AllowUserToAddRows = False
    dgvUserrights.RowHeadersVisible = False
    dgvUserrights.Columns(0).Visible = False
    dgvUserrights.Columns(1).Visible = False
    dgvUserrights.Columns(2).Visible = False

    dgvUserrights.Columns(3).HeaderText = "Menu "
    dgvUserrights.Columns(4).HeaderText = "Add"
    dgvUserrights.Columns(5).HeaderText = "Edit"
    dgvUserrights.Columns(6).HeaderText = "Del"
    dgvUserrights.Columns(7).HeaderText = "Print"
    dgvUserrights.Columns(8).HeaderText = "View"

    dgvUserrights.Width = Me.Width - dgvUserrights.Left - 20
    dgvUserrights.Columns(3).Width = Convert.ToInt32(dgvUserrights.Width * 0.42)
    dgvUserrights.Columns(4).Width = Convert.ToInt32(dgvUserrights.Width * 0.11)
    dgvUserrights.Columns(5).Width = Convert.ToInt32(dgvUserrights.Width * 0.11)
    dgvUserrights.Columns(6).Width = Convert.ToInt32(dgvUserrights.Width * 0.11)
    dgvUserrights.Columns(7).Width = Convert.ToInt32(dgvUserrights.Width * 0.11)
    dgvUserrights.Columns(8).Width = Convert.ToInt32(dgvUserrights.Width * 0.11)

End Sub

请指导我如何编辑记录并在表格中进行更改。 谢谢..

0 个答案:

没有答案