[2 days left] What’s wrong with your cloud strategy? Learn why multicloud solutions matter with Nimble Storage.Register Now

x
?
Solved

Hide a row in DataGridView

Posted on 2013-11-08
8
Medium Priority
?
1,728 Views
Last Modified: 2013-11-11
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
Comment
Question by:peter-cooper
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 3
8 Comments
 
LVL 13

Expert Comment

by:Jesus Rodriguez
ID: 39633924
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
 

Author Comment

by:peter-cooper
ID: 39634263
@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
 
LVL 13

Expert Comment

by:Jesus Rodriguez
ID: 39634821
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
NFR key for Veeam Agent for Linux

Veeam is happy to provide a free NFR license for one year.  It allows for the non‑production use and valid for five workstations and two servers. Veeam Agent for Linux is a simple backup tool for your Linux installations, both on‑premises and in the public cloud.

 

Author Comment

by:peter-cooper
ID: 39635412
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
 

Author Comment

by:peter-cooper
ID: 39636881
Any further help with this please so we can finish it off. Thanks
0
 
LVL 11

Accepted Solution

by:
SAMIR BHOGAYTA earned 2000 total points
ID: 39638005
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
 
LVL 13

Expert Comment

by:Jesus Rodriguez
ID: 39639066
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
 

Author Closing Comment

by:peter-cooper
ID: 39639298
Thank you
0

Featured Post

Visualize your virtual and backup environments

Create well-organized and polished visualizations of your virtual and backup environments when planning VMware vSphere, Microsoft Hyper-V or Veeam deployments. It helps you to gain better visibility and valuable business insights.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Today I had a very interesting conundrum that had to get solved quickly. Needless to say, it wasn't resolved quickly because when we needed it we were very rushed, but as soon as the conference call was over and I took a step back I saw the correct …
A long time ago (May 2011), I have written an article showing you how to create a DLL using Visual Studio 2005 to be hosted in SQL Server 2005. That was valid at that time and it is still valid if you are still using these versions. You can still re…
This is Part 3 in a 3-part series on Experts Exchange to discuss error handling in VBA code written for Excel. Part 1 of this series discussed basic error handling code using VBA. http://www.experts-exchange.com/videos/1478/Excel-Error-Handlin…
Video by: ITPro.TV
In this episode Don builds upon the troubleshooting techniques by demonstrating how to properly monitor a vSphere deployment to detect problems before they occur. He begins the show using tools found within the vSphere suite as ends the show demonst…

656 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question