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;
}
}
}