vb.net 在 DataGridView 中隐藏一行
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/19855155/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Hide a row in DataGridView
提问by user1532468
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.
我是 vb.net 的新用户,当用户右键单击上下文菜单并选择隐藏时,需要隐藏一行。我已经用谷歌搜索了这个,但还没有找到一种方法来做到这一点。
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
回答by Edper
You could use Rows.Item()
to hide specific DataGridViewRow
, like:
您可以使用Rows.Item()
隐藏特定的DataGridViewRow
,例如:
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.
我假设你在FullRowSelect
这里使用。
If you are not using FullRowSelect
you could have this alternative code which could catch both Cell
being Selected or Row
being Selected:
如果您不使用,FullRowSelect
您可以使用此替代代码,它可以同时捕获Cell
被选中或被Row
选中:
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:
对于Unhide
一切,让我们说Button Click
你可以拥有这个:
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
回答by Cheerkin
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).
据我所知,您不能为鼠标右键单击创建服务器端处理程序(就像您为 HideToolStripMenuItem_Click 所做的那样,它是 .NET 回发机制的一部分)。
However, I believe that such feature could be done with some client-side javascript progamming.
但是,我相信可以通过一些客户端 javascript 编程来完成这样的功能。
Hope this helps!
希望这可以帮助!