Move to row that starts with specific text in DataGridView - vb.net

I have a text box on my form that when you enter a customer code into it, my DataGridView will highlight the row in the grid if an exact match is found (see code below). However, I would like it if the DataGridView would display the first record that is the closest match. For example if a user enters just ā€˜Cā€™ the grid would go to the first customer code that starts with a C. Please note I do not want to filter the DataGridView to achieve this. All records must remain in it.
Dim targetString As String = txtAccountCode.Text
For Each row As DataGridViewRow In CustomerDataGridView.Rows
If row.Cells(0).Value = targetString Then
CustomerDataGridView.ClearSelection()
CustomerDataGridView.Rows(row.Index).Selected = True
CustomerDataGridView.FirstDisplayedScrollingRowIndex =
CustomerDataGridView.SelectedRows(0).Index
Dim selectedIndex = CustomerDataGridView.SelectedRows(0).Index
CustomerDataGridView.Rows(selectedIndex).Selected = True
CustomerDataGridView.Rows(selectedIndex).Cells(0).Selected = True
End If
Next

Related

Vb.net TableLayoutPanel Rows overlapping while every second alternate row control visible true/false

Small overview:-
(Assuming the row index starts with 1 for better clarity)
I have every odd row (1,3,5...) Drug information.
Every alternate row (2,4,6...) has a special description field which is optional
which i have kept by default visible = false.
So I had kept a button in every first row (1,3,5...) when clicked the appropriate control TextBox (in next row) visible property is assigned true/false (toggle button).
The second row has only one TextBox control.
Hence when TextBox control is false it is expected that the second row height is automatically zero and when its true the appropriate second row should be visible.
I have set all the rows height autosize
Issue:-
When the button is clicked the TextBox comes overlapped on third row (3,5,7....) instead of displaying on its appropriate second row(2,4,6...)
When i set the TextBox default visible = true then it takes every second row correctly. But its unnecessary taking a lot of space. As the TextBox entry is optional as said and not required everytime.
Following are some sreenshots for better more clarity for my issue:-
Fig.1: TableLayout Design
Fig.2: Overlapped on third row instead of taking visible second row
Fig.3: When second row TextBox visible is set true by default
My Code behind the button click:-
Private Sub Button_Drug_Dosage_General_Instructions_Click(
sender As Object, e As EventArgs
) Handles Button_Drug_Dosage_General_Instructions_20.Click,
Button_Drug_Dosage_General_Instructions_19.Click,
Button_Drug_Dosage_General_Instructions_18.Click,
Button_Drug_Dosage_General_Instructions_17.Click,
Button_Drug_Dosage_General_Instructions_16.Click,
Button_Drug_Dosage_General_Instructions_15.Click,
Button_Drug_Dosage_General_Instructions_14.Click,
Button_Drug_Dosage_General_Instructions_13.Click,
Button_Drug_Dosage_General_Instructions_12.Click,
Button_Drug_Dosage_General_Instructions_11.Click,
Button_Drug_Dosage_General_Instructions_10.Click,
Button_Drug_Dosage_General_Instructions_09.Click,
Button_Drug_Dosage_General_Instructions_08.Click,
Button_Drug_Dosage_General_Instructions_07.Click,
Button_Drug_Dosage_General_Instructions_06.Click,
Button_Drug_Dosage_General_Instructions_05.Click,
Button_Drug_Dosage_General_Instructions_04.Click,
Button_Drug_Dosage_General_Instructions_03.Click,
Button_Drug_Dosage_General_Instructions_02.Click,
Button_Drug_Dosage_General_Instructions_01.Click
If Initialization_In_Progress = True Then Exit Sub
'------------------------------------------------------------------------------------------------
'Button_Drug_Dosage_General_Instructions_obj
'------------------------------------------------------------------------------------------------
Dim btn_Button_Drug_Dosage_General_Instructions_obj As Button = CType(sender, Button)
'------------------------------------------------------------------------------------------------
'------------------------------------------------------------------------------------------------
Dim object_name As String = btn_Button_Drug_Dosage_General_Instructions_obj.Name
Dim xCustomer_Selected_srno As Double = object_name.Substring(object_name.Length - 2)
'------------------------------------------------------------------------------------------------
'------------------------------------------------------------------------------------------------
Dim txtbox_TextBox_General_Instructions_obj As TextBox = CType(Me.Controls.Find("TextBox_Drug_Dosage_General_Instructions_" + (xCustomer_Selected_srno).ToString("00"), True)(0), TextBox)
'------------------------------------------------------------------------------------------------
If txtbox_TextBox_General_Instructions_obj.Visible = False Then
txtbox_TextBox_General_Instructions_obj.Visible = True
txtbox_TextBox_General_Instructions_obj.SelectionLength = 0
Else
txtbox_TextBox_General_Instructions_obj.Visible = False
End If
End Sub
Hope I have made my best to put the exact issue. If still any further more information is needed I would definately post.
My other workouts:-
Tried TextBox Dock Fill, None, Anchor Top,Left, also all, RowHeight Absolute, but nothing worked.

DataGridView: programmatically select/highlight entire column on button click

I want to programmatically select/highlight entire column on button click to let users know what they searched.
This is what currently happens on button click ("GO" button)
but this is what I need to happen.
I have tried these so far to no avail:
DataGridView2.SelectionMode = DataGridViewSelectionMode.FullColumnSelect
DataGridView2.Columns(2).Selected = True
Doing so gave me this error: DataGridView control's SelectionMode cannot be set to FullColumnSelect while it has a column with SortMode set to DataGridViewColumnSortMode.Automatic.
I also tried to simply select the whole column. No error, but it didn't work.
DataGridView2.Columns(2).Selected = True
Just set the Selected property of each cell in the column.
Found this thread. I modified the code like so:
Dim row As DataGridViewRow
For Each row In DataGridView2.Rows
row.Cells(1).Selected = True
Next
and placed it inside Private Sub Button1_Click
I noticed that the first cell is automatically selected. This solved that problem.

Monitoring Datagridview for rows that are being unhidden

I've got a a bit of code that is allowing a Results box to pop up, populating a DataGridView with information. The DataGridView uses the same BindingSource that another DataGridView uses, so when one is clicked, the other automatically moves to the same selected cell.
The issue I am having is the Result_DataGridView doesn't always contain all of the same rows as the master DGV, as it hides rows that don't match a criteria. If the user clicks on a cell on the Master DGV that isn't present in the Result DGV, the Result DGV un-hides that row (as you can't hide a selected row).
I'm currently trying to use this method to hide the row that appears again, but because of how VB treats "Entering a cell" it fires before the cell actually appears (as it fires on input focus, but before the cell actually appears in the DGV, so that row never gets checked)
Private Sub Result_Datagridview_CellEnter(sender As Object, e As DataGridViewCellEventArgs) Handles Result_Datagridview.CellEnter
Dim Result As Integer
Dim i
For row As Integer = 0 To Result_Datagridview.RowCount - 1
i = row
Result = Array.Find(ResultArray, Function(x) x = Result_Datagridview.Rows(i).Cells(0).Value)
If Result = 0 Then
Result_Datagridview.Rows(i).Visible = False
End If
Next
End Sub
If there was a ".RowsUnhidden" event, this would work fine.
Note - the ResultArray contains all of the index numbers that need to remain visible, the check is performed to see if any rows exist with an index number that does not appear in the array, if so, hide it again.
Does anyone have a work around or better approach to this?
Handling the RowEnter event, you can suspend the binding and reset row.Visible to False.
So, if ResultArray is an Integer array of visible row indices, you simply do the following:
Private Sub Result_Datagridview_RowEnter(sender As Object, e As DataGridViewCellEventArgs)
If Not ResultArray.Contains(e.RowIndex) Then
Dim currencyManager1 As CurrencyManager = DirectCast(BindingContext(Result_Datagridview.DataSource), CurrencyManager)
currencyManager1.SuspendBinding()
Result_Datagridview.Rows(e.RowIndex).Visible = False
currencyManager1.ResumeBinding()
End If
End Sub
You can look at the DataGridView.CellStateChanged event. It occurs when a cell get the focus, lose it, is selected, ...

Combobox Make First Row As Display

So my combobox is used to populate a datagridview when user clicks the customersID in the combobox it will popupate the grid. I have the code for populating but the problem is that they combobox does no reset itself. It still shows the current value that was Selected. I wanted to know it is posible to reset the combobox after selected index event. The code below is how i got my combobobox.
' after a command and connection was made
customerAdapt = New SqlDataAdapter(customerQuery,con)
customerSet = New DataSet
customerAdapt.Fill(customerSet)
customerCb.DataSource = adoAddtRs.tables(0)
customerCb.DisplayMember = "custID"
customerCb.ValueMember = "custID"
customerCb.Visible = False
customerCb.DropDownStyle = ComboBoxStyle.DropDownList
Try this piece of coding
customerCb.selectedindex = -1

Select first visible cell of new row in DataGridView

I'm trying to focus input and fire the editing event on each new row that I add to a DataGridView in my form.
This is the code I am trying to user to achieve this.
Private Sub grd_GoldAdders_RowsAdded(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewRowsAddedEventArgs) Handles grd_GoldAdders.RowsAdded
Dim grid As DataGridView = DirectCast(sender, DataGridView)
grid.ClearSelection()
If grid.Rows(e.RowIndex).Cells("grid_flag").FormattedValue = Constants.[New] Then
For Each cell As DataGridViewCell In grid.Rows(e.RowIndex).Cells
If Not cell.Visible Then Continue For
grid.CurrentCell = cell
grid.BeginEdit(False)
Exit For
Next
End If
End Sub
The "grid_flag" is a hidden cell which is used to store custom states for a row.
Prior to adding a row, this is what we see on the form:
This is what we see when we actually try and add a new row:
Notice that both the column 0,0 and the first visible column of the new row are selected, but the column 0,0 has the focus. I do not wish for 0,0 to either get selected or have the focus. I also see here that the row indicator is pointing at row 0 too...
This is how I would like to see things after clicking my Add button:
Does anyone know where I am going wrong with the code? I've searched SO for the most part of the day trying to solve this one.
Instead of using your DataGridView's RowAdded event to set the CurrentCell, add the following code wherever you're adding a new record to your DGV (in your Add button's Click event I assume):
''# Add the new record to your Data source/DGV.
For Each row As DataGridViewRow In grd_GoldAdders.Rows
If row.Cells("grid_flag").FormattedValue = Constants.[New] Then
grd_GoldAdders.CurrentCell = row.Cells("AssySiteColumn") ''# I'm calling the first column in your DGV 'AssySiteColumn'.
grd_GoldAdders.BeginEdit(False)
Exit For
End If
Next
This code simply loops through all the rows in your DGV and specifies as the CurrentCell the first cell in the first row with your Constants.[New] flag value.