4

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 have googled this but have yet to find a way to do it.

At the moment, when a user clicks on an entry in the grid, the value is entered into a text box which is fine. What I need to do is hide the entry the user right clicked on and hide the selection. As I am new I am finding it hard going to code this as I have just finished my first course which entailed the basics. Any help would be appreciated or if you need anymore code, then please ask.

Dim value As Object = UserDataGridView.Rows(e.RowIndex).Cells(0).Value

txtCustomerActive.Text = CType(value, String)

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

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

        Catch ex As Exception

            MessageBox.Show(ex.Message)

        End Try

    End Sub
user1532468
  • 1,723
  • 8
  • 41
  • 80
  • It does not seem possible to affect the visibility of a single cell: http://stackoverflow.com/questions/6359952/make-cells-visible-datagridview-winforms-c-sharp – varocarbas Nov 08 '13 at 09:11
  • @varocarbas I need to hide the whole row. Thanks – user1532468 Nov 08 '13 at 09:17
  • You should be more clear on your answer, there is no reference to row; but various to cells. (updated version to avoid misunderstandings :)) – varocarbas Nov 08 '13 at 09:26

2 Answers2

9

You could use Rows.Item() to hide specific DataGridViewRow, like:

 If (UserDataGridView.Rows.Count > 0) Then
     For Each row As DataGridViewRow In UserDataGridView.SelectedRows
         UserDataGridView.Rows.Item(row.Index).Visible = False
     Next
 End If

I am assuming you are using FullRowSelect here.

If you are not using FullRowSelect you could have this alternative code which could catch both Cell being Selected or Row being Selected:

  If (UserDataGridView.SelectedRows.Count > 0) Then
     For Each row As DataGridViewRow In UserDataGridView.SelectedRows
         UserDataGridView.Rows.Item(row.Index).Visible = False
     Next
  ElseIf (UserDataGridView.SelectedCells.Count > 0) Then
     For Each cell As DataGridViewTextBoxCell In UserDataGridView.SelectedCells
         UserDataGridView.Rows.Item(cell.RowIndex).Visible = False
     Next
  End If

To Unhide everything let's say from a Button Click you could have this:

 Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click
    For Each row As DataGridViewRow In UserDataGridView.Rows
        If (row.Visible = False) Then
            UserDataGridView.Rows.Item(row.Index).Visible = True
        End If
    Next
 End Sub
Edper
  • 9,144
  • 1
  • 27
  • 46
  • The OP does not want to remove the row, just to hide it. And thus he should rely on the Visible property of the row. – varocarbas Nov 08 '13 at 09:24
  • 2
    I have tried both codes Edper and both error with: Row associated with the currency manager's position cannot be made invisible. – user1532468 Nov 08 '13 at 12:46
  • Think I found an answer. I used UserDataGridView.CurrentCell = Nothing in the for each loop and that seemed to work Is that ok to use there? – user1532468 Nov 08 '13 at 12:55
  • 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 your code in a unhide menu click and changing visible to true would work but it dosen't. Any advice. Thanks – user1532468 Nov 08 '13 at 13:01
  • Any further help with this or shall I mark it as answered which in all fairness you have. Just thought i'd ask for extra help.Cheers – user1532468 Nov 08 '13 at 18:30
  • @user1532468 Check my update to `Unhide` ALL rows that are `Hidden`. – Edper Nov 08 '13 at 22:16
  • Is there a way to make the hidden items stay hidden even when the program restarts so that they can only be unhidden when I click button. Many thanks for updated code. – user1532468 Nov 09 '13 at 10:17
  • When the program restarts all things in the memory is lost including the datagridview and of course the hidden rows. You might as well store in the Database the last state of this hidden rows so that when your program restarts you could load in the `DataGridView` and hide those rows that are `Hidden=True` for example. – Edper Nov 09 '13 at 10:36
  • So does that mean when a user click the hide button, I make a call to db after I have created a hidden field, and update. Thanks – user1532468 Nov 09 '13 at 10:45
  • Yes, or you could do the update when the user clicks a button to close the Form, so that you don't have to update every time. – Edper Nov 09 '13 at 10:54
  • Thanks very much. If I encounter any problems with this, I shall make new post. Thanks very much for all your help. – user1532468 Nov 09 '13 at 11:26
  • @user1532468: Glad to be of help. Happy coding and GOD bless. – Edper Nov 09 '13 at 11:29
0

As far as I know, you can not make a server-side handler for right mouse click (as you did for HideToolStripMenuItem_Click, which works as part of .NET postback mechanism).

However, I believe that such feature could be done with some client-side javascript progamming.

Hope this helps!

Cheerkin
  • 43
  • 5
  • Wow, actualy, you can =) http://stackoverflow.com/questions/1718389/right-click-context-menu-for-datagrid – Cheerkin Nov 08 '13 at 09:20
  • 2
    If you realise about something, just update your answer; don't post comments. Also, it would be a good idea to have a more or less good knowledge about the specific issue before answering. At least, knowing the exact framework: you are assuming that the OP refers to ASP and everything on his question seems to indicate otherwise (you don't even have a control called DataGridView in ASP). – varocarbas Nov 08 '13 at 09:22