title: Populate check boxes in DataGridView based on database values vb.net
body: <p>PLEASE SEE CLEARER EDITED CODE POINTED OUT BELOW TO BE MORE SPECIFIC ON WHERE HELP IS NEEDED.</p>
  
  <p>What I am trying to accomplish is this: I have a <code>DataGridView</code> that is using a <code>DataAdapter</code> to fill with the <code>DataTable</code>. I have hard-coded a separate <code>DataGridViewCheckBoxColumn</code> to come first. The <code>DataGridView</code> columns are as follows (in order):</p>
  
  <pre><code>[0]"ADD" (hard-coded `DataGridViewCheckBoxColumn`)
  [1]"Job No" (from sql database)
  [2]"Project No" (from sql database)
  [3]"Project Name" (from sql database)
  </code></pre>
  
  <p>I also have a <code>ComboBox</code> populated with usernames from a separate database (but the two have a common key - userId, associating them with which user has admin privileges to edit that particular project in another form). The purpose of this form is to add a project to that user so they can have admin rights to edit additional projects.</p>
  
  <p>I need for the <code>DataGridView</code> to fill with ALL projects, and have the <code>DataGridViewCheckBoxColumn</code> populate '.checked = true' for the projects according to which ones the userId is already associated with having admin privileges (according to existing info in the database). Then I need to have the ability to ADD new projects but checking new checkboxes, then clicking <code>btnUpdate</code>, and updating the database accordingly.</p>
  
  <p>I have been able to populate the <code>DataGridView</code>, create the <code>DataGridViewCheckBoxColumn</code>, make that column NOT readonly, but I can't get it to check the boxes that are associated with the projects, and so on... below is the code... please help?</p>
  
  <pre><code>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 = '" &amp; cmbAddUName.Text &amp; "'", 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
  </code></pre>
  
  <p>Breakdown of SQL tables' layouts: 
  USER_PROJECTS TABLE CONSISTS OF (UserID, UserName, JobNo); PROJECTS TABLES CONSISTS OF (UserID, JobNo, ProjNo, ProjName)</p>