[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1795
  • Last Modified:

Hide a row in DataGridView

I am a new user to vb.net and need to hide a row when a user right clicks on a contextmenu and selects hide. I found a way to hide the entry, there does seem to be a problem If I exit the program and run again the rows are unhidden.

I need to make permanent and have the ability to unhide all clients. I thought that using this code in a unhide menu click and changing visible to true would work but it dosen't.

Any advice. Thanks  As I am new , Any help would be appreciated or if you need anymore code, then please ask.

Code to hide row

Private Sub HideToolStripMenuItem_Click(ByVal sender As Object, ByVal e As EventArgs) Handles pnlContextMenuStrip1.ItemClicked
        'Get the text of the item that was clicked on.
        'Dim text As String = txtCustomerActive.Text
        Try

            If (UserDataGridView.Rows.Count > 0) Then

                For Each row As DataGridViewRow In UserDataGridView.SelectedRows
                    UserDataGridView.CurrentCell = Nothing
                    UserDataGridView.Rows.Item(row.Index).Visible = False
                Next
            End If

            'txtCustomerActive.Visible = False
            pnlContextMenuStrip1.Visible = False
            MessageBox.Show(txtCustomerActive.Text)

        Catch ex As Exception

            MessageBox.Show(ex.Message)

        End Try

    End Sub

Open in new window


Code to unhide row

Private Sub UnhideClientToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles UnhideClientToolStripMenuItem.Click

        Try

            If (UserDataGridView.Rows.Count > 0) Then

                For Each row As DataGridViewRow In UserDataGridView.SelectedRows
                    'UserDataGridView.CurrentCell = Nothing
                    UserDataGridView.Rows.Item(row.Index).Visible = True
                Next
            End If

            MessageBox.Show("Unhidden")

        Catch ex As Exception

            MessageBox.Show(ex.Message)

        End Try
    End Sub

Open in new window

0
peter-cooper
Asked:
peter-cooper
  • 4
  • 3
1 Solution
 
Jesus RodriguezIT ManagerCommented:
My suggestion is that create a public array variable (HidenRows) that keep all the rows hidden and then double check this variable to show or not on your grid
Something like this more or less

On the Main Code of the class
Public HiddenRows As List(Of String)=nothing

Private Sub HideToolStripMenuItem_Click(ByVal sender As Object, ByVal e As EventArgs) Handles pnlContextMenuStrip1.ItemClicked
        'Get the text of the item that was clicked on.
        'Dim text As String = txtCustomerActive.Text
        Try
            If (UserDataGridView.Rows.Count > 0) Then
                For Each row As DataGridViewRow In UserDataGridView.SelectedRows
                    UserDataGridView.CurrentCell = Nothing
                    Dim Item as string=row("ID or Key value that identify the row, not the index).ToString
                    HiddenRows.Add(Item)
                    UserDataGridView.Rows.Item(row.Index).Visible = False
                Next
            End If
            'txtCustomerActive.Visible = False
            pnlContextMenuStrip1.Visible = False
            MessageBox.Show(txtCustomerActive.Text)

        Catch ex As Exception
            MessageBox.Show(ex.Message)
        End Try

    End Sub


And later on the unhide check that the value is on the List of HiddenRows, if is there set as Visible=True the row and then remove this value from the list of HidenRows.

 But also a quick question. How do you will know which row will be hidden with client eyes?? I mean that if you need to unhide you will unhide all the rows because you won't know if there is any row hidden or not. I suggest you that on the unhide show every row and on the hide only hide the row that you want

Your code will be the same for the hide but on the unhide will be like this

Private Sub UnhideClientToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles UnhideClientToolStripMenuItem.Click

        Try
            If (UserDataGridView.Rows.Count > 0) Then
                For Each row As DataGridViewRow In UserDataGridView.Rows
                    'UserDataGridView.CurrentCell = Nothing
                    UserDataGridView.Rows.Item(row.Index).Visible = True
                Next
            End If
            MessageBox.Show("Unhidden")
        Catch ex As Exception
            MessageBox.Show(ex.Message)
        End Try
    End Sub
0
 
peter-cooperAuthor Commented:
@k-designers

Thanks for reply. But I do not understand your explanation of this code:

 
Dim Item as string=row("ID or Key value that identify the row, not the index).ToString

Could you please elaborate. Thanks
0
 
Jesus RodriguezIT ManagerCommented:
If you will go with the list of hiddenrows then

Dim Item as string=row("ID or Key value that identify the row, not the index).ToString

means that if in your Gridview you has the fields of the dataset
(ID , FIRSTNAME , LASTNAME , AGE)
 you identify the row with the ID value. When you have to search for the row then  you search for row("ID") and not for the rowindex in the datagrid and the expression will be like this then

Dim Item as string=row("ID").ToString

Do not use the row index for restore the row because if they sort/page the gridview and hide previous rows then the row index can change and at the time that you have to unhide will be a nightmare. Save on the HiddenRows a value that you can search as unique on your Grid
0
Veeam and MySQL: How to Perform Backup & Recovery

MySQL and the MariaDB variant are among the most used databases in Linux environments, and many critical applications support their data on them. Watch this recorded webinar to find out how Veeam Backup & Replication allows you to get consistent backups of MySQL databases.

 
peter-cooperAuthor Commented:
I am getting error on this row.

Dim Item As String = row("Code").ToString

Open in new window


'Error      1      Class 'System.Windows.Forms.DataGridViewRow' cannot be indexed because it has no default property.'
0
 
peter-cooperAuthor Commented:
Any further help with this please so we can finish it off. Thanks
0
 
SAMIR BHOGAYTAFreelancer and IT ConsultantCommented:
hi.. can you try this example

Private Sub TestGridView_DataBindingComplete(ByVal sender As Object, _
    ByVal e As DataGridViewBindingCompleteEventArgs) _
    Handles DataGridView1.DataBindingComplete
 
        For Each Row As DataGridViewRow In CType(sender, DataGridView).Rows
            Dim Visible As Boolean = True
 
            'Do this to inspect all cells in the row
            For i As Integer = 0 To Row.Cells.Count - 1
                If Row.Cells(i).Value Is Nothing Then
                    Visible = False
                    Exit For
                End If
            Next
 
            'Or you can check specific columns for their values
            If Row.Cells(0).Value Is Nothing OrElse _
            (IsNumeric(Row.Cells(0).Value) AndAlso CInt(Row.Cells(0).Value) < 0) Then
                Visible = False
            End If
 
            Row.Visible = Visible
        Next
    End Sub
0
 
Jesus RodriguezIT ManagerCommented:
Try to Dimension the variable without reference like this and set a break point to see the value that gets and also can you post the query that fill the datagrid or the list of fields that the datagrid has..

Dim Item= row("Code")
0
 
peter-cooperAuthor Commented:
Thank you
0

Featured Post

NEW Veeam Agent for Microsoft Windows

Backup and recover physical and cloud-based servers and workstations, as well as endpoint devices that belong to remote users. Avoid downtime and data loss quickly and easily for Windows-based physical or public cloud-based workloads!

  • 4
  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now