Category / Section
How to remove a column from grid while dragging that column outside of column header section in WinForms GridGroupingControl?
1 min read
Remove a header column
In order to remove a column while dragging that column outside of the column header section, the Tablecontrol.MouseUp and TableControl.MouseDown events can be handled. The TableControl.MouseDown is used to get the header column and that retrieved column can be removed by using VisibleColumns property in Tablecontrol.MouseUp.
C#
//Event subscription. this.gridGroupingControl1.TableControl.MouseDown += new MouseEventHandler(TableControl_MouseDown); this.gridGroupingControl1.TableControl.MouseUp += new MouseEventHandler(TableControl_MouseUp); //Handling the TableControl_MouseUp event. void TableControl_MouseUp(object sender, MouseEventArgs e) { GridTableControl tableControl = sender as GridTableControl; int rowIndex, colIndex; tableControl.PointToRowCol(e.Location, out rowIndex, out colIndex); GridTableCellStyleInfo style = tableControl.GetTableViewStyleInfo(rowIndex, colIndex); if (style.TableCellIdentity.DisplayElement.Kind != DisplayElementKind.ColumnHeader && column != null) this.gridGroupingControl1.TableDescriptor.VisibleColumns.Remove(column.ToString()); column = null; } //Handling the TableControl_MouseDown event. void TableControl_MouseDown(object sender, MouseEventArgs e) { GridTableControl tableControl = sender as GridTableControl; GridTableCellStyleInfo style = (GridTableCellStyleInfo)tableControl.PointToTableCellStyle(new Point(e.X, e.Y)); if (style.TableCellIdentity.DisplayElement.Kind == DisplayElementKind.ColumnHeader) column = style.TableCellIdentity.Column; }
VB
'Event subscription. AddHandler gridGroupingControl1.TableControl.MouseDown, AddressOf TableControl_MouseDown AddHandler gridGroupingControl1.TableControl.MouseUp, AddressOf TableControl_MouseUp 'Handling the TableControl_MouseUp event. Private Sub TableControl_MouseUp(ByVal sender As Object, ByVal e As MouseEventArgs) Dim tableControl As GridTableControl = TryCast(sender, GridTableControl) Dim rowIndex, colIndex As Integer tableControl.PointToRowCol(e.Location, rowIndex, colIndex) Dim style As GridTableCellStyleInfo = tableControl.GetTableViewStyleInfo(rowIndex, colIndex) If style.TableCellIdentity.DisplayElement.Kind <> DisplayElementKind.ColumnHeader AndAlso column IsNot Nothing Then Me.gridGroupingControl1.TableDescriptor.VisibleColumns.Remove(column.ToString()) End If column = Nothing End Sub 'Handling the TableControl_MouseDown event. Private Sub TableControl_MouseDown(ByVal sender As Object, ByVal e As MouseEventArgs) Dim tableControl As GridTableControl = TryCast(sender, GridTableControl) Dim style As GridTableCellStyleInfo = CType(tableControl.PointToTableCellStyle(New Point(e.X, e.Y)), GridTableCellStyleInfo) If style.TableCellIdentity.DisplayElement.Kind = DisplayElementKind.ColumnHeader Then column = style.TableCellIdentity.Column End If End Sub
Screenshot
After removing column
Samples:
C#: Removing_Columns
VB: Removing_Columns