Results 1 to 31 of 31

Thread: [RESOLVED] DGV Column issue

  1. #1

    Thread Starter
    Fanatic Member
    Join Date
    Dec 2011
    Location
    Oregon City, Oregon
    Posts
    703

    Resolved [RESOLVED] DGV Column issue

    I have a DGV that displays a list of approvers using the following code:

    Code:
                Me.TblChangeApproveTableAdapter.FillByChangeID(Me._MasterBase4_0ItemMasterDataSet.tblChangeApprove, glbintCRNum)
                dgvSignList.DataSource = Me._MasterBase4_0ItemMasterDataSet
                dgvSignList.Height = 406
                dgvSignList.Width = 779
                dgvSignList.DataMember = "TblChangeApprove"
                dgvSignList.Columns(0).Visible = False
                dgvSignList.Columns(1).Visible = False
                dgvSignList.Columns(2).HeaderText = "Name"
                dgvSignList.Columns(2).Width = 275
                dgvSignList.Columns(3).HeaderText = "Job Title"
                dgvSignList.Columns(3).Width = 275
                dgvSignList.Columns(4).HeaderText = "Department"
                dgvSignList.Columns(4).Width = 225
                dgvSignList.Columns(5).Visible = False
                dgvSignList.Columns(6).Visible = False
                dgvSignList.Columns(7).Visible = False
                dgvSignList.Columns(8).Visible = False
                dgvSignList.Columns(9).Visible = False
    No problem, the DGV lists exactly what I want from this table.

    So what I do is use a DGV double_Click() event to select one of the persons displayed on the list:

    Code:
                glbstrName = CStr(Me.dgvSignList.Rows(e.RowIndex).Cells(5).Value.ToString)
                RemoveApprover()
    You will note that in the first code block above most of the columns have had .Visible = False.

    So my problem is that the data I am interested in is in a column that .Visible = False and ONLY the fields with .Visible = True will provide a value for the variable in the second set of code. So how do I squeeze a value from a column that .Visible = False?

  2. #2
    Super Moderator Shaggy Hiker's Avatar
    Join Date
    Aug 2002
    Location
    Idaho
    Posts
    38,988

    Re: DGV Column issue

    It shouldn't matter. The DGV is bound to a datatable, which has rows. You know the selected row in the DGV, which is the row of interest in the underlying datatable, so you can get the data from there based off the RowIndex.

    At first, I was surprised to hear that you couldn't get the data from a hidden field in the DGV. I guess I'm still surprised at that, but I realized I don't think I have ever tried, because I always go to the datasource for information.
    My usual boring signature: Nothing

  3. #3

    Thread Starter
    Fanatic Member
    Join Date
    Dec 2011
    Location
    Oregon City, Oregon
    Posts
    703

    Re: DGV Column issue

    I do not believe that I have experienced this problem when I use the Properties window to set datasource and datamember for the DGV. However, this particular DGV, depending on where it is called from, might use different tables so that is why I set the datasource and datamember in the code. Anyway, I have ran the second block set using different column numbers and it only provides a value when the .Visible = True in the first code block.
    It would appear that there must be another setting for that column that is required if .Visible = False.

  4. #4
    Hyperactive Member Mike Storm's Avatar
    Join Date
    Jun 2017
    Location
    Belgium
    Posts
    425

    Re: DGV Column issue

    CStr and then .Value.ToString, no need!!
    I would use just CStr, .ToString provides you with a exception if the value is dbnull.
    Code:
    glbstrName = CStr(Me.dgvSignList.Rows(e.RowIndex).Cells(5).Value)

  5. #5

    Thread Starter
    Fanatic Member
    Join Date
    Dec 2011
    Location
    Oregon City, Oregon
    Posts
    703

    Re: DGV Column issue

    I have been doing some more experimenting and it turns out that I am wrong about .Visible = False being the issue. I guess there has to be a first for everything.

    So here is what I am after. I want to obtain the value of the primary key assign that value to a variable

    Code:
    glbintIDNum = CInt(Me.dgvSignList.Rows(e.RowIndex).Cells(0).Value.ToString)
    However, the variable instead is assigned -1.

  6. #6
    Hyperactive Member Mike Storm's Avatar
    Join Date
    Jun 2017
    Location
    Belgium
    Posts
    425

    Re: DGV Column issue

    Can you post the rest of the code you use to remove the aprovern including the event and the methode?

  7. #7

    Thread Starter
    Fanatic Member
    Join Date
    Dec 2011
    Location
    Oregon City, Oregon
    Posts
    703

    Re: DGV Column issue

    Code:
        Private Sub dgvSignList_CellDoubleClick(sender As Object, e As DataGridViewCellEventArgs) Handles dgvSignList.CellDoubleClick
            If glbintCount = 1 Then
                glbintIDNum = CInt(Me.dgvSignList.Rows(e.RowIndex).Cells(0).Value)
                RemoveApprover()
            ElseIf glbintCount = 2 Then
                RemoveTrainee()
            Else
                MsgBox("Case Failure")
            End If
        End Sub

  8. #8

    Thread Starter
    Fanatic Member
    Join Date
    Dec 2011
    Location
    Oregon City, Oregon
    Posts
    703

    Re: DGV Column issue

    Code:
        Private Sub RemoveApprover()
            Try
                Me.TblChangeApproveTableAdapter.FillByApproveID(Me._MasterBase4_0ItemMasterDataSet.tblChangeApprove, glbintIDNum)
                Me._MasterBase4_0ItemMasterDataSet.tblChangeApprove(0).blnRemoved = False
                Me._MasterBase4_0ItemMasterDataSet.tblChangeApprove(0).strSignature = "Removed by Admin, " + glbstrName + CStr(DateAndTime.Now)
                Me.Validate()
                Me.tblChangeApproveBindingSource.EndEdit()
                Me.TblChangeApproveTableAdapter.Update(Me._MasterBase4_0ItemMasterDataSet)
                Me.TblChangeApproveTableAdapter.FillByActive(Me._MasterBase4_0ItemMasterDataSet.tblChangeApprove, glbintCRNum)
                With dgvSignList
                    .Enabled = False
                End With
            Catch ex As Exception
                'Message for user removal failure
                Dim strMessage As String = Nothing
                'glbstrMessage = CType(HelpMessage.NoRemoveMessage(strMessage), String) + "from Approval table"
                'MsgBox(glbstrMessage)
            End Try
        End Sub
    Forgot this part

  9. #9
    Hyperactive Member Mike Storm's Avatar
    Join Date
    Jun 2017
    Location
    Belgium
    Posts
    425

    Re: DGV Column issue

    First you wanna make sure you clicking a valid row and not the column headers:

    Code:
     Private Sub dgvSignList_CellDoubleClick(sender As Object, e As DataGridViewCellEventArgs) Handles dgvSignList.CellDoubleClick
    
    'I added this
    if e.RowIndex > -1 Then
    
            If glbintCount = 1 Then
                glbintIDNum = CInt(Me.dgvSignList.Rows(e.RowIndex).Cells(0).Value)
                RemoveApprover()
            ElseIf glbintCount = 2 Then
                RemoveTrainee()
            Else
                MsgBox("Case Failure")
            End If
    
    End IF
        End Sub
    Last edited by Mike Storm; Oct 6th, 2017 at 05:24 PM. Reason: Corrected RowIndex miss spealing

  10. #10
    Hyperactive Member Mike Storm's Avatar
    Join Date
    Jun 2017
    Location
    Belgium
    Posts
    425

    Re: DGV Column issue

    What does happen, does it gives you a exception?

  11. #11

    Thread Starter
    Fanatic Member
    Join Date
    Dec 2011
    Location
    Oregon City, Oregon
    Posts
    703

    Re: DGV Column issue

    It does when I take it out of the Try/Catch that I have. I have not run it that far because I can see that I am getting no value assigned to variable when I take that value from the column containing the primary key.

  12. #12
    Hyperactive Member Mike Storm's Avatar
    Join Date
    Jun 2017
    Location
    Belgium
    Posts
    425

    Re: DGV Column issue

    Replace column index by column name

    Code:
    glbintIDNum = CInt(Me.dgvSignList.Rows(e.RowIndex).Cells("ColumnName here").Value)
    also you should check if the value is not dbnull

  13. #13
    Hyperactive Member Mike Storm's Avatar
    Join Date
    Jun 2017
    Location
    Belgium
    Posts
    425

    Re: DGV Column issue

    Inside of the try catch or out of it should trow you the exception anyway

  14. #14

    Thread Starter
    Fanatic Member
    Join Date
    Dec 2011
    Location
    Oregon City, Oregon
    Posts
    703

    Re: DGV Column issue

    Mike,

    I did use the code that you suggested. However, I still get -1 for the value of the variable after that line is executed.

  15. #15
    Hyperactive Member Mike Storm's Avatar
    Join Date
    Jun 2017
    Location
    Belgium
    Posts
    425

    Re: DGV Column issue

    Explain:
    Is the exception trown under the doubleclick event or under RemoveApprover?

  16. #16

    Thread Starter
    Fanatic Member
    Join Date
    Dec 2011
    Location
    Oregon City, Oregon
    Posts
    703

    Re: DGV Column issue

    The exception occurs during the Update(). It was that error that lead me back to what I am looking at currently. So here is what I am trying to do.

    I am wanting to delete a person from the displayed DGV list with a double click. I have been using a methodology to accomplish that but I never really liked it because of the way it works.

    The old method would distribute the values from a record into a set of variables. The row would then be removed, a new row created and then populated from the variables. It worked, but I have always hated it.

    What I am wanting to do now is take the row selected from the double click and just go ahead and replace the values in two of the columns with different values.

  17. #17
    Hyperactive Member Mike Storm's Avatar
    Join Date
    Jun 2017
    Location
    Belgium
    Posts
    425

    Re: DGV Column issue

    I m not understanding, and i dont have VS here now, can you post the exception message?

  18. #18

    Thread Starter
    Fanatic Member
    Join Date
    Dec 2011
    Location
    Oregon City, Oregon
    Posts
    703

    Re: DGV Column issue

    Here you go:

    Name:  error.jpg
Views: 367
Size:  38.5 KB

  19. #19
    Hyperactive Member Mike Storm's Avatar
    Join Date
    Jun 2017
    Location
    Belgium
    Posts
    425

    Re: DGV Column issue

    I dont understand why you in you first post you fill TblChangeApproveTableAdapter, i guess that code will be under some other event like form load, and then you fill TblChangeApproveTableAdapter again under RemoveApprover

  20. #20

    Thread Starter
    Fanatic Member
    Join Date
    Dec 2011
    Location
    Oregon City, Oregon
    Posts
    703

    Re: DGV Column issue

    Here is what it is that I think I should know and what I am seeing as a problem.

    When I execute this code here is what occurs:

    glbintIDNum = CInt(Me.dgvSignList.Rows(e.RowIndex).Cells("intChangeID").Value)
    The value for glbintIDNum = 10000. This is the value that is in intChangeID.

    If I run execute this code instead

    glbintIDNum = CInt(Me.dgvSignList.Rows(e.RowIndex).Cells("intApproveID").Value)
    The value for glbintIDNum = -1. This is the value that is in intApproveID = 6

    intApproveID is a primary key field in this table and is an autoincrement integer field. inChangeID is just an integer field and is not a primary key. That is the only difference I can define between the two.

  21. #21
    Hyperactive Member Mike Storm's Avatar
    Join Date
    Jun 2017
    Location
    Belgium
    Posts
    425

    Re: DGV Column issue

    If the -1 value is the return of the Indexkey/Primary Key then you hgetting the index of a new row that has not been yet saved.
    Means your table has no rows, either couse you not filling it, or the value of the Where "clousele" does not exists on that table.

  22. #22

    Thread Starter
    Fanatic Member
    Join Date
    Dec 2011
    Location
    Oregon City, Oregon
    Posts
    703

    Re: DGV Column issue

    Actually, I get -1 if I selected the first record in the DGV, -2 the second, -3 ...... I guess that is the explanation for the index. OK, I don't understand why, but I see that is apparently what it is doing. So my question is why is it not instead outputting the actual value contained in that field? It does output the actual value for any other field named.

  23. #23
    Hyperactive Member Mike Storm's Avatar
    Join Date
    Jun 2017
    Location
    Belgium
    Posts
    425

    Re: DGV Column issue

    I have no ideia about that one, but if you add a databound datagridview to a form and leave the autoincrement primary key visible, while you dont save the data you will see that the auto increment will be -1, -2, -3 and so on...
    You need to check you code and see whats happening.

  24. #24

    Thread Starter
    Fanatic Member
    Join Date
    Dec 2011
    Location
    Oregon City, Oregon
    Posts
    703

    Re: DGV Column issue

    Interesting. I am not sure even where I would look in the code to find what is occurring. One thing that is clear is that there are only four of the 10 columns/fields, will output a value to the variable when the line of code above is run. So what kind of code should I be looking at that would limit which columns can be read and the value extracted?

  25. #25
    Hyperactive Member Mike Storm's Avatar
    Join Date
    Jun 2017
    Location
    Belgium
    Posts
    425

    Re: DGV Column issue

    I have no idea what may be actualy cousing that, first i would do would be set visible all datagridview columns and see its values.

  26. #26

    Thread Starter
    Fanatic Member
    Join Date
    Dec 2011
    Location
    Oregon City, Oregon
    Posts
    703

    Re: DGV Column issue

    OK, I did that. I did see where the primary key field does show an index instead of the actual field value. However, it is still only the 4 fields that actually output a value to a variable. I don't know what that means, but it is clear that somewhere (upstream in the code) those four columns are declared or defined and the others are not.

  27. #27
    Hyperactive Member Mike Storm's Avatar
    Join Date
    Jun 2017
    Location
    Belgium
    Posts
    425

    Re: DGV Column issue

    I dont understand very well what 4 fields do you refear to?

  28. #28

    Thread Starter
    Fanatic Member
    Join Date
    Dec 2011
    Location
    Oregon City, Oregon
    Posts
    703

    Re: DGV Column issue

    I found it. It was not in the code, it was in my query. The only columns SELECT in the query used were those 4 fields. I normally just put all of the fields into the SELECT command line but apparently did not do this for this query. Damn, that was hard to find.

  29. #29
    Hyperactive Member Mike Storm's Avatar
    Join Date
    Jun 2017
    Location
    Belgium
    Posts
    425

    Re: DGV Column issue

    I was thinking there where a a problem with the fill.

  30. #30

    Thread Starter
    Fanatic Member
    Join Date
    Dec 2011
    Location
    Oregon City, Oregon
    Posts
    703

    Re: DGV Column issue

    Yeah, that was always at the back of my mind too. Anyway, thanks and I like that I actually figured one out myself.

  31. #31
    Hyperactive Member Mike Storm's Avatar
    Join Date
    Jun 2017
    Location
    Belgium
    Posts
    425

    Re: [RESOLVED] DGV Column issue

    Lol...

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width