请在下面找到明确的编辑代码,以便在需要帮助的地方提供更具体的信息。
我想要实现的目标是:我有DataGridView
使用DataAdapter
来填充DataTable
。我已经将单独的DataGridViewCheckBoxColumn
硬编码为第一位。 DataGridView
列如下(按顺序):
[0]"ADD" (hard-coded `DataGridViewCheckBoxColumn`)
[1]"Job No" (from sql database)
[2]"Project No" (from sql database)
[3]"Project Name" (from sql database)
我还有一个ComboBox
填充了来自单独数据库的用户名(但两者有一个共同的密钥 - userId,将它们与哪个用户具有管理员权限以另一种形式编辑该特定项目相关联)。此表单的目的是向该用户添加项目,以便他们可以拥有管理权限来编辑其他项目。
我需要让DataGridView
填写所有项目,并让DataGridViewCheckBoxColumn
填充' .checked = true'对于项目,userId已经与哪些项目具有管理员权限(根据数据库中的现有信息)。然后我需要能够添加新项目但是检查新的复选框,然后单击btnUpdate
,并相应地更新数据库。
我已经能够填充DataGridView
,创建DataGridViewCheckBoxColumn
,使该列不是只读,但我无法检查与项目相关的框,等等......下面是代码......请帮帮忙?
Imports System.Data.SqlClient
Public Class Form1
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
'there is where usernames will be filled into dropdown from Proj_users database
Dim fillUName As New SqlCommand("SELECT UName FROM Proj_User WHERE Active = 1 and Admin = 1", frmConnect.DB)
Dim dr As SqlDataReader = fillUName.ExecuteReader()
While dr.Read()
If dr.HasRows = True Then
cmbAddUName.Items.Add(dr("UName"))
End If
End While
dr.Close()
End Sub
Private Sub cmbAddUName_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmbAddUName.SelectedIndexChanged
Call fillAddGrid(cmbAddUName.Text)
End Sub
Sub fillAddGrid(ByVal PT_User As String)
'column created for checkbox (to be able to check additional projects that will then be added to the user indicated in the combobox's privileges - let's them be able to change that project's details in another form)
Dim chk As New DataGridViewCheckBoxColumn()
grdAddProjectPrivs.Columns.Add(chk)
chk.FalseValue = False
chk.TrueValue = True
chk.HeaderText = "Add"
chk.Name = "Add"
chk.ReadOnly = False
'use stored procedure with parameter to generate recordset
Dim sqlCmd As New SqlCommand
sqlCmd.Connection = frmConnect.DB
sqlCmd.CommandType = CommandType.StoredProcedure
sqlCmd.CommandText = "SP_ManagePrivs"
'IF @SP_Use = 3 -- for ADDING privileges, FILL GRID with ALL projects so can add which ones they need
'BEGIN()
' SELECT
' P.JobNo AS [Job No],
' P.ProjNo AS [Project No],
' P.ProjName AS [Project Name]
' FROM Projects P JOIN User_Projects UP ON P.JobNo = UP.JobNo
' WHERE P.Deleted = 0 and P.Active = 1
' ORDER BY UP.UserID, P.JobNo
'End
'for adding privs, need to show all projects
sqlCmd.Parameters.Add(New SqlParameter("@SP_Use", 3))
sqlCmd.Parameters.Add(New SqlParameter("@UName", DBNull.Value))
sqlCmd.Parameters.Add(New SqlParameter("@Active", DBNull.Value))
sqlCmd.Parameters.Add(New SqlParameter("@Admin", DBNull.Value))
sqlCmd.ExecuteNonQuery()
'use DataAdapter to fill datatable
Dim sqlDA As New SqlDataAdapter()
sqlDA.SelectCommand = sqlCmd
Dim table As New DataTable
sqlDA.Fill(table)
grdAddProjectPrivs.DataSource = table
sqlDA.Dispose()
'reading to get userid to checkboxes accordingly
Dim userID As New SqlCommand("SELECT JobNo FROM User_Projects WHERE PT_User = '" & cmbAddUName.Text & "'", frmConnect.DB)
Dim dr As SqlDataReader = userID.ExecuteReader()
'****THIS IS WHERE I THINK I NEED HELP!!!!
While dr.Read()
If dr.HasRows = True Then
If grdAddProjectPrivs.Columns.Contains(dr("JobNo")) Then
For Each row As DataGridViewRow In grdAddProjectPrivs.Rows
row.Cells("Add").Value = True
Next
End If
End If
End While
dr.Close()
End Sub
Private Sub btnUpdate_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnUpdate.Click
'HERE I AM LOOKING TO UPDATE THE DATABASE BY ADDING THE USER'S UserID number TO THAT SPECIFIC PROJECT THAT THEY CHECK OFF WITH THE CHECKBOX.
'i'm thinking once i get the gist of manipulating the checkboxes down pat i can figure this out but if there is anything additional i need to know please advise?
End Sub
End Class
SQL表的细分'布局: USER_PROJECTS表包含(UserID,UserName,JobNo);项目表包括(UserID,JobNo,ProjNo,ProjName)