在 DataGridView 中隐藏一行 [英] Hide a row in DataGridView

查看:40
本文介绍了在 DataGridView 中隐藏一行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是 vb.net 的新用户,需要在用户右键单击上下文菜单并选择隐藏时隐藏一行.我已经用谷歌搜索了这个,但还没有找到一种方法来做到这一点.

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

推荐答案

您可以使用 Rows.Item() 来隐藏特定的 DataGridViewRow,例如:

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

我假设您在此处使用 FullRowSelect.

I am assuming you are using FullRowSelect here.

如果您没有使用 FullRowSelect,您可以使用这个替代代码,它可以同时捕获 Cell 被选中或 Row 被选中:

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

取消隐藏所有内容,让我们从Button Click说起,您可以这样做:

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

这篇关于在 DataGridView 中隐藏一行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆