VB Datagridview cellvalidating event to check if active cell is empty berore leaving problems - vb.net

In Visual basic i'm trying to make a field required by preventing the user to leave the active cell if it's empty.
The grid consists out of two columns. The first one is the ID which is automaticly filled in on adding a new row, the second one is the name.
On form load there are 4 items loaded into the grid. When the form is loaded the first cell that is selected passes the check with no problem. It is the same for the three cells that are filled on form load.
Unforunately it does not seem to work when i try to add a new row to the grid, enter a value and try to leave the cell. I've set a break point and checked the values. The value is equal to nothing... This is not the case with any of the already loaded values.
How does it not get the value i enter?
Thanks in advance.
Ivan.f
Private Sub ValidateCellValue(sender As Object, e As
DataGridViewCellValidatingEventArgs)
Handles grdResult.CellValidating
Dim catNaam = grdResult.Rows(e.RowIndex).Cells(1).Value
If IsNothing(catNaam) Then
MessageBox.Show("Veld mag niet leeg zijn")
e.Cancel = True
Exit Sub
End If
End Sub

Related

Why DataGridViewTextBoxCell showing output of previous input in DataGridView

I am working with- window application, VB, VS 2012, .net F/W- 4.5
I have a DGV (datagridview) in form.
There are different types of column (dgv-combobox, dgv-textbox) in DGV which are created runtime.
Code: textchanged event (runtime created using addhandler) of DataGridViewTextBoxCell
Private Sub txt_box_textchanged(sender As Object, e As EventArgs)
Dim a, b, total_price As Int32
a = dgv.Rows(0).Cells(3).Value
b = dgv.Rows(0).Cells(4).Value
total_price = a * b
dgv.Rows(0).Cells(5).Value = total_price
End Sub
If I change input value (any of textbox- a or b), it does not affect on output.
In other words, output is not as it should be, like 5*3=15, 4*9=36
But if I change cell and change value of that cell, then output will be of previous input (when last time cell got changed).
Otherwise output remains same until input is coming from same cell.
what I mean by "change cell " is click on any other cell of DGV except current cell.
Please note that all row in image are individual input (only one row- not multiple row of DGV).
I also tried "key up" event of textbox using same code, but same issue.
Please guys help me to overcome this issue.

DataGridView: EditOnEnter selection mode allowing row deletion

By default, a DataGridView is set to EditOnKeystrokeOrF2 edit mode. This means two or three clicks (spaced further apart than the user's double click interval) are required to change the value of a combobox in this view. As this is rather strange for a UI object, you would tend to think the control doesn't work.
Fortunately, you can change the selection mode to EditOnEnter. This will immediately select a cell when clicking on it, not first select the row, reducing the amount of clicks by 1. However, DataGridViews are implemented somewhat strangely. There is a '-1th' cell that is not manually selectable in each row.
When this '-1th' cell is selected, in the normal selection mode the row is selected, but in the "EditOnEnter" mode the 1st cell in the row is selected instead. If the DataGridView is set to enable row deletion using the "Del" key, then using EditOnEnter makes it impossible to use this functionality.
How do I get both to work? I.e.: I don't have a view where the user can have click up to 6 times (users tend to click more rapidly when they have to do so a lot of times) to open a box, while at the same time allowing row selection using the special -1th column?
One would need to programmatically toggle between both edit modes when any cell in the row is clicked. However, the CellClick event fires too late: after the row is already selected. Naively just toggling the EditMode would mean that the first click on the row selection box doesn't work, while the second would, which would appear as buggy behaviour.
The trick is to do much more manually. The following event handler, when attached to the CellClick event, will resolve almost all issues.
Private Sub CellSelect(ByVal sender As Object, ByVal e As DataGridViewCellEventArgs) Handles myDataGridView.CellClick
Dim dgv As DataGridView = CType(sender, DataGridView)
If dgv.Rows.Count = 0 Then
Return
End If
Dim rowToSelect As Integer = e.RowIndex
Dim columnToSelect As Integer = e.ColumnIndex
If e.RowIndex = -1 Then
rowToSelect = 0
End If
If rowToSelect >= dgv.Rows.Count Then
rowToSelect = 0
End If
If columnToSelect = -1 Then
dgv.EditMode = DataGridViewEditMode.EditOnKeystrokeOrF2
dgv.SelectionMode = DataGridViewSelectionMode.FullRowSelect
dgv.CurrentCell = Nothing
dgv.Rows(rowToSelect).Selected = True
Else
If columnToSelect >= dgv.Rows(rowToSelect).Cells.Count Then
columnToSelect = 0
End If
dgv.EditMode = DataGridViewEditMode.EditOnEnter
dgv.SelectionMode = DataGridViewSelectionMode.CellSelect
dgv.Rows(rowToSelect).Cells(columnToSelect).Selected = True
End If
End Sub
It works by unsetting the selected cell, then setting the selected row programmatically. As the EditMode was changed beforehand, it will select the entire row, not just the first cell, even the first time that the row-selection box is clicked.
There are also a whole bunch of edge cases where a user that clicks quickly enough can create click events on cells that do not exist. So we assume those clicks are on the cell [0,0] so at least our application won't blow up.
This isn't a perfect solution (yet). With this solution: A small graphical glitch remains; for about one frame the DataGridView will flicker between edit modes, very briefly appearing as though the row is selected.

DataGridView check all Rows vb.net

Private Sub comboBoxStudentID_SelectedIndexChanged(sender As Object, e As EventArgs) Handles comboBoxStudentID.SelectedIndexChanged
For Each dr As DataGridViewRow In Me.dataGridViewStudent.Rows
If dr.Cells(0).Value.ToString.Contains(comboBoxStudentID.Text) Then dr.Visible = True Else dr.Visible = False
Next
End Sub
I've created this method to check and display a row that contains the same Student ID as selected from the comboBoxStudentID the problem is it only checks the first row of the DataGridView. How can I make it check all of the rows if there is match of Student ID?
Without knowing how your data is organized, you could do this by binding the DataGridView and the ComboBox to the same data source. Since this is not specified and looking at the snippet of code (which appears to work) it appears to loop through the rows and if the currently selected combo box text is contained in the first column cell, then make that row visible. Otherwise make the row invisible.
The posted code appears to work however there is one scenario you may want to consider. If the combo box contains the values that are in column one of the DataGridView, THEN, when the user selects a value in the combo box, the DataGridView will display ONLY those values that match the current combo box selection. This appears to be what you are trying to do.
The problem is how do you re-display all the rows?
Once the combo box selection has changed to some value, the user can delete the selection but all the rows will not display unless you handle another event. Also this strategy is not very user intuitive. One possible solution (below) simply adds a top item of “Reset” to the top of the combo boxes items. Then when the user selects “Reset” all row will become visible. Hope this helps.
To reiterate: you should consider setting this up with a DataSource and possibly a BindingSource where this is done for you automatically. Just a thought.
Private Sub ComboBox1_SelectedIndexChanged(sender As Object, e As EventArgs) Handles ComboBox1.SelectedIndexChanged
If ComboBox1.SelectedIndex > 0 Then
For Each dr As DataGridViewRow In DataGridView1.Rows
If (Not dr.IsNewRow) Then
If dr.Cells(0).Value.ToString.Contains(ComboBox1.Text) Then
dr.Visible = True
Else
dr.Visible = False
End If
End If
Next
Else
For Each row As DataGridViewRow In DataGridView1.Rows
row.Visible = True
Next
End If
End Sub

Infragistics grid combo box value change should disable cell of that specific row

I am using infragistics wingrid, I have column (EmploymentType) which is combobox has two values FullTime and Contract, If i have selected contract value for any specific row in grid, the Fringes column of that specific cell should be disabled. But i tried something here with below code, it works well but it's disabling whole Fringes column, instead it should disable specific cell of that fringes column. How can i do that.
Please find code below
Private Sub ugMain_AfterCellListCloseUp(sender As Object, e As CellEventArgs) Handles ugMain.AfterCellListCloseUp
If e.Cell.Column.Key = "EmploymentType" Then
If e.Cell.Text = "Contract" Then
e.Cell.Band.Columns("Fringes").CellActivation = Activation.Disabled
Else
e.Cell.Band.Columns("Fringes").CellActivation = Activation.AllowEdit
End If
End If
End Sub
Please find picture below
Try accessing the cells via the cell->row and settings its activation.
e.Cell.Row.Cells("Fringes").Activation = Activation.Disabled

Losing cell data when datagridview gets focus

I know I need to provide some code, but I'm not sure what I should show, so please suggest if you can.
I have a bound datagridview on a Windows form. After the form loads and the datagridview gains focus (on mouse click), the first row (and a specific column) loses it's data, changing the cell's state to dirty. It doesn't matter where I click to bring the dgv into focus, that row/column always goes blank. What event is firing that may trigger that loss of data?
Again, any suggestions as to what code to post would be great. I know that will help answer this question.
Edit #1
This code is an infinite loop, but I'm adding it in response to a comment:
Private Sub dgQCOrders_CellPainting(sender As Object, e As DataGridViewCellPaintingEventArgs) Handles dgQCOrders.CellPainting
If e.RowIndex = 0 And e.ColumnIndex = 9 Then
If e.FormattedValue <> e.Value Then
MsgBox("Changed")
Else
MsgBox("Unchanged")
End If
End If
End Sub
Edit #2:
Private Sub dgQCOrders_CellPainting(sender As Object, e As DataGridViewCellPaintingEventArgs) Handles dgQCOrders.CellPainting
If e.RowIndex = 0 And e.ColumnIndex = 9 Then
If e.FormattedValue <> e.Value Then
Me.txtTest.Text = "Changed"
Else
Me.txtTest.Text = "Unchanged"
End If
End If
End Sub
This test tells me that the new value is null, it is deleting that first record (which I already knew)--still don't know how to fix it!
Edit #3
More explanation:
Currently, the only event I'm handling is form_Load, which fills the dgv using the tableadapter for my (bound) dataset. I then bind the dgv to the binding source.
I know that this error only occurs when the dgv gains focus (I tested this by setting the focus to the dgv when the form loads). I have a series of checkboxes/listboxes/textboxes on this form as well that allows the user to filter the dgv dynamically (back-end, I filter the binding source). If I filter the dgv first, the same row and the same column (their indexes do not change) maintains it's value when I move the focus to the dgv. When I clear the filter, the same row and the same column, loses it's data again.
I did have the _CellStateChanged event firing after a user makes an edit. Currently, it is commented out so the data loss isn't reflected in my dataset.
Additionally, I have another dgv on a different form, bound the exact same way, with the _CellStateChanged event and everything fires and saves correctly. I have gone through the designer coding for both forms, I can't find any setting difference between the two.
I'm losing my mind over here! Any help is GREATLY appreciated!
I decided to recreate the form from scratch and this error is no longer occuring. I have compared the two sets of code and can't find one discrepancy between them. If anyone has this problem in the future, save time and recreate all of the objects related to your dgv.
Maybe I understand that statement within the focus cell is not saved in the database table if U save
this problem I solved
add blank textbox control to your form which contain dgv and named txtFocus
placed it behand dgv and use its properties send to back Or
Evoked by the bottom of the screen so do not show it
then
before save
white then :
txtFocus.Focus()
sendKeys.send("{F2}")
only in this case U can save the data inside last cell changed in dgv
best Reg
Ashraf