Skipping columns when tabbing on a DataGrid

Skipping columns when tabbing on a DataGrid

It happens quite often that you have a DataGrid with hidden columns (with width = 0), because you need their values but don’t want to show them to the user. However, when you tab through the DataGrid’s columns, these “hidden” columns are still taken into account in the tab order. This means that when the user presses the Tab key the focus might seem to disappear from the DataGrid and the form, when in reality it is on the hidden cell. This behavior is not very user friendly, and may leave the user wondering what happened, and if he has to click somewhere or press Tab again. It would be much better if the Tab could skip some columns, so that everything work as if those hidden columns were not there at all. This skipping mechanism could also be useful in other situations, when you have visible columns but don’t want the user to be able to give them the focus, either by pressing Tab or by clicking directly on them. You can solve this problem by handling the DataGrid’s CurrentCellChanged event, and simulate a Tab keypress when the user selects an “inaccessible” column. Here’s the code to enable/disable the column-skipping mechanism:

' Enable the column-skipping mechanism for the input grid,'  and for the columns with the specified index' Example: skip the 2nd (index 1) and 4th (index 3) columns'   EnableDataGridColumnSkip(DataGrid1, 1, 3)Sub EnableDataGridColumnSkip(ByVal grid As DataGrid, _    ByVal ParamArray columnsToSkip() As Integer)    ' save the array of column indexes in the grid's Tag property    grid.Tag = columnsToSkip    ' attach the grid's CurrentCellChanged event to the     ' GenDataGrid_CurrentCellChanged event handler    AddHandler grid.CurrentCellChanged, AddressOf GenDataGrid_CurrentCellChangedEnd Sub' Disable the column-skipping mechanism for the input grid' Example: DisableDataGridColumnSkip(DataGrid1)Sub DisableDataGridColumnSkip(ByVal grid As DataGrid)    ' detach the grid's CurrentCellChanged event from the     ' GenDataGrid_CurrentCellChanged event handler    RemoveHandler grid.CurrentCellChanged, _        AddressOf GenDataGrid_CurrentCellChangedEnd Sub' Handle the grid's CurrentCellChanged, to skip the columns whose index is ' found in the array stored in grid's Tag propertySub GenDataGrid_CurrentCellChanged(ByVal sender As Object, _    ByVal e As System.EventArgs)    ' cast the generic sender Object to a DataGrid    Dim grid As DataGrid = DirectCast(sender, DataGrid)    ' cast the grid's Tag to an array of Integers, that contains the indexes of     ' the columns to skip    Dim columnsToSkip() As Integer = DirectCast(grid.Tag, Integer())    ' get the index of the current column    Dim currColIndex As Integer = grid.CurrentCell.ColumnNumber    ' if the current column's index is found in the columnsToSkip array,    '  simulate a TAB key press,    ' so that the focus is moved to the next column in the same row,    '  or to the next row if this is the last column    If Array.IndexOf(columnsToSkip, currColIndex) > -1 Then        SendKeys.Send("{TAB}")    End IfEnd Sub

Share the Post:
XDR solutions

The Benefits of Using XDR Solutions

Cybercriminals constantly adapt their strategies, developing newer, more powerful, and intelligent ways to attack your network. Since security professionals must innovate as well, more conventional endpoint detection solutions have evolved

AI is revolutionizing fraud detection

How AI is Revolutionizing Fraud Detection

Artificial intelligence – commonly known as AI – means a form of technology with multiple uses. As a result, it has become extremely valuable to a number of businesses across

AI innovation

Companies Leading AI Innovation in 2023

Artificial intelligence (AI) has been transforming industries and revolutionizing business operations. AI’s potential to enhance efficiency and productivity has become crucial to many businesses. As we move into 2023, several

data fivetran pricing

Fivetran Pricing Explained

One of the biggest trends of the 21st century is the massive surge in analytics. Analytics is the process of utilizing data to drive future decision-making. With so much of

kubernetes logging

Kubernetes Logging: What You Need to Know

Kubernetes from Google is one of the most popular open-source and free container management solutions made to make managing and deploying applications easier. It has a solid architecture that makes

ransomware cyber attack

Why Is Ransomware Such a Major Threat?

One of the most significant cyber threats faced by modern organizations is a ransomware attack. Ransomware attacks have grown in both sophistication and frequency over the past few years, forcing

data dictionary

Tools You Need to Make a Data Dictionary

Data dictionaries are crucial for organizations of all sizes that deal with large amounts of data. they are centralized repositories of all the data in organizations, including metadata such as