Solved

Hide a row in DataGridView

Posted on 2013-11-08
8
1,331 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
  • 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
 

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
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 

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 500 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

Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

Join & Write a Comment

Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
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…
Access reports are powerful and flexible. Learn how to create a query and then a grouped report using the wizard. Modify the report design after the wizard is done to make it look better. There will be another video to explain how to put the final p…

708 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

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now