Why DataGridViewTextBoxCell showing output of previous input in DataGridView - vb.net

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.

Related

Get the actual value of a BoundField element in a gridview VB.NET

The following image depicts the full gridview I have:
I'm able to get the values of the second column (project_ID) in the back end as follows:
Dim val As String = grdProjects.SelectedRow.Cells(1).Text.ToString
For instance if I press the Select of the third row I'll get back the value 3.
My problem appears when the gridview is updated and has less records as the one in the following image.
If I press the second record I get the value 2 instead of the value 4 that I'd like to get.
Any suggestions please on how to get the actual value of the cell instead of the number of the selected row?
I might be wrong but in VB.net datagrids SelectedRow does not exist, SelectedRows do exist.
Anyway, try this way and let me know if it work:
On the RowEnter or CellClick event use the E.RowIndex to get what you want. Do as follow:
Private Sub DataGrid1_RowEnter(sender As Object, e As DataGridViewCellEventArgs) Handles DataGrid1.RowEnter
Dim CellValue As String = DataGrid1.Rows(e.RowIndex).Cells("Project_ID").Value.ToString
End Sub
Notice that for the .cells() I use the name of the column, this is better because if in the future you add columns this code will not need to be changed. Easier code maintenance always pay! Also. Cell().Value instead Cell().Text
Nandostyle

VB Datagridview cellvalidating event to check if active cell is empty berore leaving problems

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

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

Is there a way to allow only unique values in a column of an Infragistics UltraWinGrid?

I'm using an Infragistics UltraWinGrid with a column of drop-down boxes. I don't want the user to be able to select the same value for multiple rows in that column. Is there a simple (or heck, I'd settle for advanced) way to do this?
I'm using VB.NET
-EDIT-
I tried setting a filter on the data source for the drop-down box. But when I did that, the values in the other boxes in that column started disappearing (not the values themselves, but the descriptions that they were supposed to represent, so instead of reading "Information", it just said "1"). All of the cells in a column refer to the same combo box, so if you filter out a value from the data source for one of them, you filter it out for all of them.
I'm currently trying to catch a CellChange event and check against all other currently-used values. If it was already used, I would put up a message saying as much and revert back to the old value. However, the value comes back as the previously-saved one, so... not helpful. About to see if I can use the "text" property.
Since you're using Infragistics, you could use an UltraDropDown which is bound to a DataTable (or something similiar) which you can add a "Selected" column in addition to a column holding the values you want to show.
As each value is selected (via AfterCellUpdate or AfterCellListCloseUp for instance), you could update the "Selected" column in that data source and use a column filter to only show items which haven't been marked as selected. That way as items are selected or removed, the contents of the drop-down would be automatically updated.
To clear the selected flag from the old value, you can use the BeforeCellUpdate event to access the cell's current value then perform a lookup on the data source bound to the UltraDropDown using that value to clear the flag.
Solved it:
The trick was to use BeforeCellUpdate whose BeforeCellUpdateEventArgs has a "NewValue" and a "Cancel" member. I just look through all of the items in the column to see if any of them match the new value. If one does, I notify the user and cancel the operation.
And that's it. Here's the code:
Private Sub myUltraWinGrid_BeforeCellUpdate(ByVal sender As Object, ByVal e As Infragistics.Win.UltraWinGrid.BeforeCellUpdateEventArgs) Handles myUltraWinGrid.BeforeCellUpdate
If e.Cell.Column.Key = "myColumn" Then
Dim newValue As Integer = CInt(e.NewValue)
For Each row As Infragistics.Win.UltraWinGrid.UltraGridRow In myUltraWinGrid.Rows
If row.Cells("myColumn") IsNot e.Cell _ 'So I'm not checking against the current cell
AndAlso CInt(row.Cells("myColumn").Value) = newValue Then
MsgBox("That value has already been used in this column")
e.Cancel = True
End If
Next
End If
End Sub