DataGridView: programmatically select/highlight entire column on button click - vb.net

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.

Related

FullColumnSelect not working in DataGridView

Actually i'm trying to hightlight whole the column when a user click on a cell of the DataGridView.
The source is load dynamically from the user so initially DataGridView is null.
The issue is that if i change SelectionMode to FullColumnSelect or anyother SelectionMode options nothing change, the DataGridView will still have the SelectionMode as FullRowSelect even if i change it programmatically.
How can i solve that problem and hightlight entire column on user click?
Actually i had to set SortMode to NotSortable for each column of the DataGrid but only after i've called .DataSource
So the code is the following
Grid.DataSource = dt
For Each c In Grid.Columns
c.SortMode = DataGridViewColumnSortMode.NotSortable
Next
Grid.SelectionMode = DataGridViewSelectionMode.FullColumnSelect

access make list field visible when clicking Button

In an access form, I try to make a list field visible when the fokus is on another textfield in which new data should be filled in. The backround is that one should know the last data inputs to create a new one.
As a first step I tried to make the list (liste91) visible when clicking on a button, but I failed using the following code.
Private Sub Befehl97_Click()
Forms!projects!liste91.SetFocus
Me.liste91.visible = True
End Sub
I get error in the line Me.list91
what is wrong?
thank you for your help!
You can't set focus to something not yet visible. Just switch the order:
Private Sub Befehl97_Click()
Me.liste91.visible = True
Me.liste91.SetFocus
End Sub

How to set the CheckBox column in DataGridView value to true

GD All,
I've looking around for a solution to my below challenge.
I have got a form with an unbound datagridview, the dg has one added column that allows user to select a method to be used.
The state of the event is stored in a database and after re-opening the form the code checks if the event is in an 'open' state, if so it compares the previously selected method with the methods in the datagrid and should set the previously activated method to be the 'selected' method.
Yet I can't seem to get this to work unfortunately...
The below code loops through the methods in the dg and compares the values, if it meets the methodID it should set the value to 'True' or to the TrueValue anyway.
This is initialized if the database check returns true and after full initialisation of the form, where session.methodID is a field in the returned LINQ query.
For Each r As DataGridViewRow In dgMethods.Rows
If r.Cells(1).Value = session.methodID Then
Dim c As DataGridViewCheckBoxCell = r.Cells(0)
c.Value = c.TrueValue
End If
Next
Unfortunately, this doesn't set the checkbox to 'Checked'.
The loop runs and evaluates the comparison between r.Cells(1).Value and session.methodID correct and triggers correctly.
The interesting thing is if I do a similar loop after the 'CellContentClick' event it does do exactly what is expected. (the example below sets all checkbox values to checked)
Private Sub dgMethods_CellContentClick(sender As Object, e As DataGridViewCellEventArgs) Handles dgMethods.CellContentClick
'Only single selection allowed, so clear table before submitting new selection
For Each r As DataGridViewRow In dgMethods.Rows
Dim c As DataGridViewCheckBoxCell = r.Cells(0)
c.Value = c.TrueValue
Next
dgMethods.CommitEdit(DataGridViewDataErrorContexts.Commit)
End Sub
So, apparently there is a difference in the state between just calling the loop on the dgMethods and when the dgMethods.CellContentClick event has triggered, yet I do not know which one ?
There are many many post on trying to set the CheckBox column yet I have not been able to get any of them working.
Anybody have any idea ?
I would appreciate your suggestions ?
I was not sure of being undestand your question... but there's s simple way to check and change the state of a chechbox cell in a datagridview:
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
For Each dr As DataGridViewRow In DataGridView1.Rows
If CBool(dr.Cells(0).Value) = True Then dr.Cells(0).Value = False : Continue For
If CBool(dr.Cells(0).Value) = False Then dr.Cells(0).Value = True
Next
End Sub
In this example, when you click this button for each row in the datagridview, checks the checkboxcell and set the value to FALSE or TRUE depending of his value.
Hope this helps you.
And let me one more tip. If you get acces to the cells for his name instead of his index use his name, it should helps you avoiding troubles ;)
GD All,
After searching further I came across the following interesting behaviour.
The method selection process is part of a form called 'frmAddEvent', the frmAddEvent form is called from a main form using below routine.
The new form instance is created and afterwards filled using a public sub in the form class called InitializeForm() which uses a GUID parameter to retrieve corresponding data to set the form fields.
If Not (isOpened(rsTankName.unqID)) Then
Dim newForm As New frmAddEvent() '(rsTankName)
newForm.InitializeForm(rsTankName)
newForm.Show()
Else
End If
The initialization sub queries several datatables and sets the corresponding fields in the new form instance correctly if applicable.
Part of that setting is the method selection in the dgMethods datagridview.
It would appear that the sequence in which you call the form makes all the difference as the below code works perfectly:
If Not (isOpened(rsTankName.unqID)) Then
Dim newForm As New frmAddEvent() '(rsTankName)
newForm.Show()
newForm.InitializeForm(rsTankName)
Else
End If
So calling the newForm.InitializeForm(rsTankName)after the newForm.Show event allows the datagridview to set the CheckBoxColumn correctly.
Likely because the actual CheckBox itself is only actually generated upon the Show command, despite the fact that it is 'available' as a cell with DataGridViewCheckBoxColumn properties in the datagrid, directly after the New frmAddEvent has created the new form instance. The actual CheckBox and its corresponding CheckedState is not created before the newForm.Show event is called. It would appear that the when the CheckBox is created for display (during the newForm.Show event) there is no comparison made to its actual value.
So, in order to set the Checkbox column on initiating a new form you have to call the Show event prior to setting the DataGridViewCheckBoxColumn values otherwise the CheckBox will not show it as 'Checked'.

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)

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.