Winform: Right-click at the beginning of the Datagridview column to bring up the hidden column menu

Source: Internet
Author: User

In Winform, right-click the displayed menu of the Datagridview. Many people have done this. See the following link.

Http://www.codeproject.com/KB/grid/DGVColumnSelector.aspx

Source code link http://www.codeproject.com/KB/grid/DGVColumnSelector/DataGridViewColumnSelector_src.zip

However, when you have many columns or you are not familiar with your column names, it is a headache to check the columns to be displayed.

The following describes how to directly hide the current column.

First, add a dview and ContextMenuStrip to the FORM, and add a ToolStripMenuItem with the display name (hidden) to ContextMenuStrip.

The code is very simple. I added the variable TempColIndex to store the index of the current column, so I will not comment on it.

 

 Public Class Form1
Dim TempColIndex As Int16 =-2
Private Sub maid (ByVal sender As Object, ByVal e As System. Windows. Forms. Maid) Handles maid
If e. Button = Windows. Forms. MouseButtons. Right Then
If e. RowIndex <0 Then
TempColIndex = e. ColumnIndex
ContextMenuStrip1.Show (MousePosition. X, MousePosition. Y)

End If
End If

End Sub

Private Sub hide ToolStripMenuItem_Click (ByVal sender As System. Object, ByVal e As System. EventArgs) Handles hide ToolStripMenuItem. Click
If TempColIndex> = 0 Then
DataGridView1.Columns (TempColIndex). Visible = False
End If
End Sub
End Class

 

 Public class Form1
{
Int16 TempColIndex =-1;

Private void maid (object sender, System. Windows. Forms. Maid e)
{
If (e. Button = Windows. Forms. MouseButtons. Right ){
If (e. RowIndex <0 ){
TempColIndex = e. ColumnIndex;

ContextMenuStrip1.Show (MousePosition. X, MousePosition. Y );
}

}
}

Private void hide ToolStripMenuItem_Click (System. Object sender, System. EventArgs e)
{
If (TempColIndex> = 0 ){
DataGridView1.Columns (TempColIndex). Visible = false;
}
}
}

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.