[2005] Duplicate in Datagridview
Hi
I have a datagridview with ONE column.
I enter my value in each row.
Each time I finish entering the value I check the value to make sure if was not entered. So far I have managed to get this part working this way:
Code:
Private Sub DataGridView1_CellEndEdit(....
If My.Settings.myCollection1.IndexOf(MyDataGridView1.CurrentCell.Value) = -1 Then
Else
MsgBox(MyDataGridView1.CurrentCell.Value & " is duplicate")
end if
myCollection1 is just a dictionay with values I want to compare too.
So far everything works. It does tell me that the value is a duplicate.
What I want to do is to keep the user on the same row, and delete whatever was entered.
I tried MyDataGridView1.CancelEdit
But it does not seem to work,
what am i doing wrong ?
thanks !
Re: [2005] Duplicate in Datagridview
Handle the DataGridView.CellValidating event instead. In this event handler, you read the formattedvalue of the cell then do your validation. If the validation fails, you display a message and set e.Cancel = True. Something like this
Code:
Private Sub DataGridView1_CellValidating(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewCellValidatingEventArgs) Handles DataGridView1.CellValidating
Dim txt As String = e.FormattedValue.ToString
'Do your own validation testing here. For demo purpose, I just text to
'see if the cell is blank.
If txt.Trim = "" Then
MessageBox.Show("Invalid. This cell can not be blank.")
e.Cancel = True
End If
End Sub
Re: [2005] Duplicate in Datagridview
Hi Stanav thanks for this method but there is a bit of a problem.
I did not explain the whole situation.
When I load the datagridview I load in it a System.Collection.Specialized.StringCollection. (a mySettings )
So what I want to prevent is that when a new value is entered in the datagridview it is not a duplicated of the existing loaded data (and hence the mySettings data once the data will be saved back into the StringCollection).
That's why i thoguht i should be using the CellEndEdit method...
but right now I am not too sure
Re: [2005] Duplicate in Datagridview
Quote:
Originally Posted by Zoroxeus
Hi Stanav thanks for this method but there is a bit of a problem.
I did not explain the whole situation.
When I load the datagridview I load in it a System.Collection.Specialized.StringCollection. (a mySettings )
So what I want to prevent is that when a new value is entered in the datagridview it is not a duplicated of the existing loaded data (and hence the mySettings data once the data will be saved back into the StringCollection).
That's why i thoguht i should be using the CellEndEdit method...
but right now I am not too sure
And how do you bind the stringcollection to your datagridview? Or do you just loop thru it and manually add each item in the collection as a new row in your datagridview?
Re: [2005] Duplicate in Datagridview
yes I just loop through it and add it... it's a small list so it's ok
i tried to do the binding but it was too advanced for me as of right now :)
once i get more familiarized
In the cellendedit method I tried:
Code:
If My.Settings.mydic.IndexOf(DataGridView1.CurrentCell.Value) = -1 Then
MsgBox("no dupl")
Else
MsgBox("''" & DataGridView1.CurrentCell.Value & "''" & " is in the dic.")
DataGridView1.Rows(e.RowIndex).ErrorText = String.Empty
End If
I see the message fine, but the string does not clear up...
I want the row to stay at the same and not let any other click in the below row... so that the user has to change