set cell focus in data grid view .NET - vb.net

In my datagridview (Mydgv1),I want to set fourth cell into focus and edit it, after i leave the first cell.
On first cell's leave event , i have written the code to focus the 4th cell, it comes into focus, but there is no cursor in it, and then the focus shifts to second cell and second cell becomes blue(by default highlighted cell in datagridviews). Please post some code for it. So far I have tried this.
Mydgv1.ClearSelection()
Mydgv1.CurrentRow.Cells(3).Selected = True
Mydgv1.BeginEdit(False)

BeginEdit will only have effect on the current cell (marked by the CurrentCell property of the DataGridView object). Selecting it won't help (especially since you can select multiple cells in some DataGridView configurations). Instead try this:
Dim ColumnIndex As Integer = 3
Mydgv1.CurrentCell = Mydgv1.CurrentRow.Cells(ColumnIndex)
Mydgv1.BeginEdit(False)

Related

Excel Form buttons remain in same place, but TopLeftCell is different (all after parent row is unhidden)

Cutting right to the chase, I have a tab with 4 Excel Tables stacked on top of each other. Each row has its own "up" and "down" buttons in the column to the left of the table, like this:
I shrunk the buttons to fit within the same cell (rowheight of 20). This allowed me to use the cell's TopLeftCell.Row property as follows: When clicked, stuff from the button's row is copy-pasted to the row above/below. This is the basic code:
currRow = ActiveSheet.Shapes(Application.Caller).TopLeftCell.Row
If InStr(Application.Caller, "Up") > 0 Then
Move_Project_Up currRow
Else
Move_Project_Down currRow
End If
When the user gets to this tab, any empty table-rows are hidden (with a Rows(rr).EntireRow.Hidden = True). The buttons Move and size with cells so they are automatically hidden). All visible buttons work as expected. However, sometimes a user wishes to move a project from the top of one table "up" to the bottom of the above table (i.e. to a currently hidden row). You can imagine wanting to move data from row 312 "up" to row 244 in the image below:
The code successfully makes the move and unhides the row. HERE'S THE PROBLEM: when the row is unhidden, the buttons are dark gray and their TopLeftCell.Row is wrong:
They are NOT disabled. When clicked, they still trigger the assigned macro. However, I can see from a Debug.Print that their TopLeftCell.Row is not necessarily the row they are actually in. In the image above, BOTH SETS OF BUTTONS have a TopLeftCell.Row of 97. That's correct for the first set, but not the second.
Any thoughts on why this might be happening? I suspect it has to do with the unhiding of the rows, and/or the fact that there's a lot happening on this tab (e.g. over 10,000 array formulas).
Thanks in advance!
As one commenter (jkpieterse) suggested, setting the .Top property of the "broken" buttons to the .Top value of a cell in the same row worked!

Excel VBA: Deselect a cell

Is there a way to deselect a cell with Excel VBA? My situation is I have a worksheet double-click event, so the cell must be selected for the macro to run. But that leaves a flashing cursor in the cell that's selected.
How do I deselect the cell once the macro is done? I've searched Google for a few methods and tried some, but none quite work properly. I've tried moving the selection left one cell and then right, back to the original cell. But for some reason, the cursor is still flashing in the selected cell. Any help?
The Worksheet_BeforeDoubleClick event has an argument called Cancel. Have a line in your double click event code that sets Cancel = True and that will cancel the cell edit mode and just have the cell selected instead of the blinking cursor within the cell

Revert DataGridView cell contents to original value

(I have rephrased this entire question in an attempt to be more clear in what I'm trying to do)
This is a VB.Net WinForms project being written in VS2012.
When the user changes the contents of a DataGridView's cell, I am changing the background color of the cell to visually indicate to the user which cells they have changed. That works fine.
However, if the user changes the cell contents back to what the value was before they initially edited it (essentially what the database table's value is), I need to change the background color to its default row background color.
So, in the CellValidating event I am using the following lines to get the value from the DataGridView's bound dataset and the value that the user entered so I can compare the two:
Dim dgvColDataName As String = dgvEmployees.Columns(e.ColumnIndex).DataPropertyName
Dim dgvRowID As Integer = CInt(dgvEmployees.Rows(e.RowIndex).Cells(1).Value)
Dim index As Integer = EmployeesDataSet.Tables("employees").Rows.IndexOf(EmployeesDataSet.Tables("employees").Rows().Find(dgvRowID))
Dim originalValue As String = EmployeesDataSet.Tables("employees").Rows(index).Item(dgvColDataName).ToString()
Dim enteredValue As String = dgvEmployees.Rows(e.RowIndex).Cells(e.ColumnIndex).EditedFormattedValue.ToString()
These get the correct, corresponding values between the DataGridView's current cell and its corresponding cell in the bound table.
Here is where I am comparing the values to determine if the value entered is the same as the original dataset's value:
If originalValue.ToUpper() <> enteredValue.ToString().ToUpper() Then
' Color the edited cell's background
dgvEmployees.Rows(e.RowIndex).Cells(e.ColumnIndex).Style.BackColor = Color.LightPink
Else
' Set the back color to its default style
dgvEmployees.Rows(e.RowIndex).Cells(e.ColumnIndex).Style.BackColor = dgvEmployees.Rows(e.RowIndex).Cells(e.ColumnIndex).InheritedStyle.BackColor
End If
I thought this was the answer. The cell background is being changed when the value is changed. But I was surprised to discover that if the cell loses focus (leaves Edit Mode) and then the user clicks back in that cell and changes the value to what it originally was before initially editing it, the cell back color doesn't change back to the default row/cell color. I.e. apparently when the cell loses focus, the dataset is changed, so originalValue is now the last entered value.
I need to be able to get the value from when the DataGridView was first loaded or after the last save.
Any ideas?
UPDATE
After some more research I was able to figure out how to access the DataSet's original, pre-editing value of a given cell.
In the variable assignment code above I added this line to get the relevant DataSet row:
Dim row As DataRow = EmployeesDataSet.Tables("employees").Rows(dsRowIndex)
Then I replaced this line:
Dim originalValue As String = EmployeesDataSet.Tables("employees").Rows(index).Item(dgvColDataName).ToString()
with this:
Dim originalValue As String = row(dgvColDataName, DataRowVersion.Original).ToString()
So after thoroughly testing this I am confident that, regardless of the state of the DGV, editing controls, etc. the originalValue will be the value that came from the database table.
I'm marking this as answered...
The below code will accomplish what I was trying to do. This is in the CellValidating event. I am using it to set the value back to the original cell value as it was coming from the database.
Dim dgvColDataName As String = dgvEmployees.Columns(e.ColumnIndex).DataPropertyName
Dim dgvRowID As Integer = CInt(dgvEmployees.Rows(e.RowIndex).Cells(1).Value)
Dim dsRowIndex As Integer = EmployeesDataSet.Tables("employees").Rows.IndexOf(EmployeesDataSet.Tables("employees").Rows().Find(dgvRowID))
Dim row As DataRow = EmployeesDataSet.Tables("employees").Rows(dsRowIndex)
Dim originalValue As String = row(dgvColDataName, DataRowVersion.Original).ToString()

in vb.net, how do i exclude a column from being highlighted with a datagridview?

I have a datagridview set up and I have set one of the properties to
.SelectionMode = DataGridViewSelectionMode.FullRowSelect
which is what I want, but the last col of the row is a button column and the button also gets highlighted with the rest of the row. I do not want the button highlighted. I have tried setting the last col's properties to read only, and others and nothing worked. I also set up a handler to listen for when the row was highlighted and then deselected the last col, but that just deselected the whole row.
Does anyone know how to un-highlight just the last column of buttons?
Thanks.
Try hooking into OnRowSelected event and then making the last column not selected.
In the designer(or through code) set next properties:
ButtonColumn.DefaultCellStyle.BackColor = System.Drawing.Color.White
ButtonColumn.DefaultCellStyle.ForeColor = System.Drawing.Color.Black
ButtonColumn.DefaultCellStyle.SelectionBackColor = System.Drawing.Color.White
ButtonColumn.DefaultCellStyle.SelectionForeColor = System.Drawing.Color.Black
This will set column colors(BackColor and ForeColor) to the same color for the both states(normal and selected).
So when row became selected current column doesn't change color...

DataGridView Cell Value Change Not Happening

I have retrieved data from access database into a DataGridView. Let us suppose I am in Row 0. When I change the contents of Row 0, Cell 1 and press a button, the Update query should modify that row, but I am unable to modify the value of the cell. The cell maintains its previous value and the database is not modified. For example, if I change the contents of a cell from "David" to "Jhon", how can I change the value of the cell from "David" to "Jhon"? I am using VB.NET 2008.
You most likely need to pick up the EditedFormattedValue of the cell, which has the new value. FormattedValue and Value have the original value.
I presume you are checking the formatted value of the cell for getting the value.
Also if you are changing the values, please check if the cell value changed event is triggered or not. Coz the editing control needs to pass the changed value back to the grid cell. If you can post your code then it might be more clear and helpful.