0

PLEASE SEE CLEARER EDITED CODE POINTED OUT BELOW TO BE MORE SPECIFIC ON WHERE HELP IS NEEDED.

What I am trying to accomplish is this: I have a DataGridView that is using a DataAdapter to fill with the DataTable. I have hard-coded a separate DataGridViewCheckBoxColumn to come first. The DataGridView columns are as follows (in order):

[0]"ADD" (hard-coded `DataGridViewCheckBoxColumn`)
[1]"Job No" (from sql database)
[2]"Project No" (from sql database)
[3]"Project Name" (from sql database)

I also have a ComboBox 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.

I need for the DataGridView to fill with ALL projects, and have the DataGridViewCheckBoxColumn 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 btnUpdate, and updating the database accordingly.

I have been able to populate the DataGridView, create the DataGridViewCheckBoxColumn, 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?

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

Breakdown of SQL tables' layouts: USER_PROJECTS TABLE CONSISTS OF (UserID, UserName, JobNo); PROJECTS TABLES CONSISTS OF (UserID, JobNo, ProjNo, ProjName)

leoraelkins
  • 173
  • 1
  • 1
  • 13
  • I would add in your select statement: `'True' As combo... Then in your `DataGridView` under editor add the column and set it's name to `combo` and make it a checkboxcolumn. Then when you set the `DataSource` it will also check the checkboxes... There's no need to create a separate column to do this... This is based off what you said, `I need for the DataGridView to fill with ALL projects, and have the DataGridViewCheckBoxColumn populate '.checked = true'` – Trevor Jun 17 '15 at 14:37
  • Definitely makes sense! I'm not 100% sure how to create a bound `datagridviewcheckboxcolumn` based off my database values though. I was realizing I may have taken a backwards approach on this and have been researching how to manually (for lack of a better word) create my columns rather than just using `fill` to automatically create them... – leoraelkins Jun 17 '15 at 14:44
  • You are already setting the `.DataSource` when you set this the `DataGridViewCheckboxColumns` should be checked... This is because your returning a boolean value: combo. When it's set (true) the column values would be checked. – Trevor Jun 17 '15 at 14:45
  • Also in your `cmbAddUName_SelectedIndexChanged` event you can remove the `Call` word, this is VB6 syntax and you don't need to do this. – Trevor Jun 17 '15 at 14:47
  • Ohhh... i misunderstood your first comment! Thank you for clarifying... I'll give it a shot. – leoraelkins Jun 17 '15 at 14:48
  • Just make sure you add the column in the designer first and make the `DataPropertyName` to `combo` which would be the column name coming from `SQL` – Trevor Jun 17 '15 at 14:50
  • Shoot... Just realized I can't change anything in the SQL databases... these are all dependent on a ton of other uses. Any other solutions with the existing table data? I'm going to post a picture of the tables for better clarity... if that's possible. – leoraelkins Jun 17 '15 at 14:58
  • No you don't change the table, change your command... – Trevor Jun 17 '15 at 14:59
  • For example: `"SELECT UName, 'True' As combo FROM Proj_User WHERE Active = 1 and Admin = 1"` This will return another column that would be for your checkbox's... – Trevor Jun 17 '15 at 15:00
  • If you have enough reputation would you be able to move this to chat? You're not being very clear on which SELECT statement you are referring to, since you are combining aspects of both of them, as well as other things; and this isn't really the place to have this conversation... I'm far from an expert in this, apologies, but clarity is important for everyone's sake regardless :( – leoraelkins Jun 17 '15 at 15:14
  • I can't use chat here where I am at it's blocked. All im saying is your select statement needs to be changed as noted in my last comment. – Trevor Jun 17 '15 at 15:28
  • Change the select statement in your `fillAddGrid` that's where it needs to change. For ex: `"SELECT UserID, PTSID, UName, 'True' As combo ...` – Trevor Jun 17 '15 at 15:46
  • I updated my code to be a little more specific with what i am trying to accomplish... what you had suggested I don't think was helping? I also think my original SELECT statement was incorrect. All I needed to pull was the JobNo (previously referred to as PTSID) so that i could see which rows contained it, and then makes those checkboxes = true. – leoraelkins Jun 17 '15 at 17:37

0 Answers0