Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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 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
like image 718
user1532468 Avatar asked Feb 15 '23 17:02

user1532468


1 Answers

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
like image 110
Edper Avatar answered Feb 23 '23 23:02

Edper