Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chriss Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Changing value during DataGridView Validating

Status
Not open for further replies.

Kliot

Programmer
Jan 10, 2003
622
US
I have a DataGridView and I am Validating a column, in this case it's a zipCode column.

I have a procedure that verifies the zipCode and adds the 4 digit extension, the problem I am having is changing the value of the zip Code column while it's being validated, If I use:
dgv.currentRow.Cells("ZipCode").value = newZipValue
the new value doesn't get accepted. e.formattedValue holds the current value but it's readOnly so I can't change the value there either.

I could easily do this using CellValueChanged but I don't want the user to be able to leave the cell if the zipCode is not valid.

Any Suggestions?

Thanks
Perrin
 
you may have to reference it through the sender obj that is passed. this may be the datagrid itself or the datagridviewrow, you will have to msgbox a sender.GetType to figure out what you are dealing with. Then you can code directly against the sender, and that should work.

-Sometimes the answer to your question is the hack that works
 
Thanks for the suggestion,sender.GetType returns "DataGridView" so I tried:

MsgBox(CType(sender, DataGridView).Rows(e.RowIndex).Cells(e.ColumnIndex).Value.ToString)

and it gives me a value of "" not the e.FormattedValue

Any other suggestions?
 
which event are you handling, so i can get what type of object e is?

You may be able to cancel the event, instead of setting the cell value.

-Sometimes the answer to your question is the hack that works
 
This works, so you may have see what you are doing, differently.

Code:
    Private Sub dgvTest_CellLeave(ByVal sender As System.Object, ByVal e As System.Windows.Forms.DataGridViewCellEventArgs) Handles dgvTest.CellLeave
        Dim dgvSender As DataGridView = sender
        'MsgBox(dgvSender.Rows(e.RowIndex).Cells(e.ColumnIndex).Value)
        TextBox1.Text = dgvTest.Rows(e.RowIndex).Cells(e.ColumnIndex).Value
        TextBox2.Text = dgvSender.Rows(e.RowIndex).Cells(e.ColumnIndex).Value.ToString + "Ha"
        If dgvTest.Columns(e.ColumnIndex).ValueType.ToString <> GetType(Int32).ToString Then
            dgvTest.Rows(e.RowIndex).Cells(e.ColumnIndex).Value = dgvSender.Rows(e.RowIndex).Cells(e.ColumnIndex).Value.ToString + "Ha"
        End If
    End Sub

-Sometimes the answer to your question is the hack that works
 
It's the DataGricView.Validating event, I can e.cancel the event but that doesn't help, I want the event to process and I want to change the value.
 
Your requirements:

I could easily do this using CellValueChanged but I don't want the user to be able to leave the cell if the zipCode is not valid.

why don't you just have a CellLeave and have a flow like:

if column = ZipCode
'validate zip
if valid
'exit sub
else
'set focus to current cell
end if
end_if

then do all you other processing in the Validating

-Sometimes the answer to your question is the hack that works
 
I tried your code and it works fine but it changes the value of the textbox but it doesn't change the value of the cell.

I could use the CellLeave, and I think that's what I have done in the past, I just thought it would be cleaner if I was able to do it from the Validating event.
 
I am not sure if you just dumped my code in and hit play, i was tacking text on to the end and it was blowing up on messing with an INT column, so i added a code around.

-Sometimes the answer to your question is the hack that works
 
I changed the textboxes to variables on your code and ran it fine and it correctly changed dvgTest but that still didn't help me change the cell value. I tried sender = dgvSender and the dataGridView cell still doesn't take the new value.
 
sender needs to be thought of as a local variable. You will probably have to modify the actual datagridview.

-Sometimes the answer to your question is the hack that works
 
True, but if I modify the actual datagridview it accepts the new value but when the validating event finishes it changes it back.

I'm not sure what's actually happening behind the scenes but it seems like the validating event takes the value entered before it gets sent to the datagridview, validates it, then passes it to the datagridveiw. That's why changing the datagridview during the validating event gets overwritten.
 
You may have to move all the logic related to validating that data from the Validating, to the CellLeave, and just assume that the data there is good.

Once you start fighting your own code, it turns into a mess, you have to consolidate the logic and hope you didn't miss a way to bypass your checks.

-Sometimes the answer to your question is the hack that works
 
But I like banging my head agains the wall....

Thanks for all the help
 
No problem.

-Sometimes the answer to your question is the hack that works
 
Ahhhh, a moment of clarity. Canceling the DataGridView edit keeps the cellvalidating event from appending the new value to the datagridview and allows me to change the value.

Code:
 DataGridView1.CancelEdit()
 DataGridView1.Rows(e.RowIndex).Cells(e.ColumnIndex).Value = NewZipValue
 
I found one problem with this solution, if the row is new then it gets deleted and the change is made to the previous row. Instead I used CommitEdit and that posts the new row and solves the problem.

Code:
DataGridView.CommitEdit(DataGridViewDataErrorContexts.Commit)

 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top