Select first visible cell of new row in DataGridView - vb.net

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.

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.

change color of DataGrid row

I want to color entire rows of a System.Windows.Forms.DataGrid based on the content of one cell of the Row.
I couldn't find any way to do that, since there is no .Rows-Property of the DataGrid or a .Row-Property of the DataGridTextBoxColumn (or any similar).
Searching for solutions in the internet also didn't help me so far.
Switching to DataGridView is unfortunately not an option.
So the question remains: How can I change the color of a DataGrid row?
Thanks to this link (provided by #Monty) I was able to solve the problem.
You have to format every Cell on its own, so you have to hook up on the SetCellFormat-Event of every Column:
AddHandler column.SetCellFormat, AddressOf FormatGridRow
In the EventHandler you can check for example the cell value of another column of the same row and change the color of the current cell accordingly. When every cell of the row gets formatted this way, it seems like the row as a whole is formatted.
Private Sub FormatGridRow(ByVal sender As Object, ByVal e As DataGridFormatCellEventArgs)
Dim Cell As DataGridColumnStyle = sender
Dim ColumnStyles As GridColumnStylesCollection = Cell.DataGridTableStyle.GridColumnStyles
Dim columnIndex As Integer = ColumnStyles.IndexOf(ColumnStyles.Item("ColumnName"))
If Cell.DataGridTableTableStyle.DataGrid(e.Row, columnIndex).ToString() = "SomeValue" Then
e.BackBrush = New SolidBrush(Color.Red) 'this does only change the Cell`s background
End If
End Sub

VB.NET - Focus first row and first cell of DataGridView

Good morning,
I have a form with two textbox and a DataGridView.
I'm used to fill my controls then i press Tab to switch to the next one.
However, when i switch from my textbox to my DataGridView, the selected cell is the second one of the first row => DTG.Rows(0).Cells(1).
I need to enter on the DTG.Rows(0).Cells(0) to fill my DTG without using my mouse.
I tried the code bellow :
Public Sub txtBoxTest_Leave() Handles txtBoxTest.Leave
DTG.Focus()
DTG.CurrentCell = DTG(0, 0)
DTG.BeginEdit(True)
End Sub
The cell seems to be selected, but it is not in editmode.
Modify the edit mode to EditOnEnter does not resolve my problem :
DTG.EditMode = DataGridViewEditMode.EditOnEnter
Can someone help me please ?
EDIT :
My datagridview cells are in ReadOnly = False.
Try this:
Dim cell as Windows.Forms.DataGridViewCell= dataGridView1.Rows(0).Cells(0)
DataGridView1.CurrentCell = cell
DataGridView1.BeginEdit(True)

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, ...