2 Replies - 26421 Views - Last Post: 16 January 2011 - 07:57 AM Rate Topic: -----

#1 Essel   User is offline

  • D.I.C Head

Reputation: 5
  • View blog
  • Posts: 118
  • Joined: 08-May 09

Prevent duplicates in datagridview

Posted 16 January 2011 - 06:17 AM

Please i have a datagrid which i add values with this code
            Me.dgAppraisal.Rows.Add(Me.cmbAssementArea.Text, Me.NumericUpDownArea.Text, Me.txtremark.Text, Me.txtcoment.Text)


What i want to do is to prevent duplicate entries of values in a column.I am using this code but it is not working.

If Me.dgAppraisal.Rows.Count > 0 AndAlso Me.dgAppraisal.Rows.Contains(Me.cmbAssementArea.Text) Then
            ErrorProvider.SetError(Me.dgAppraisal, "Employee has already been assessed on selected area")
            Me.cmbAssementArea.DroppedDown = True
            Return False

Please help me prevent duplicates in the datagrid rows/columns.

Is This A Good Question/Topic? 0
  • +

Replies To: Prevent duplicates in datagridview

#2 CharlieMay   User is offline

  • This space intentionally left blank
  • member icon

Reputation: 1734
  • View blog
  • Posts: 5,710
  • Joined: 25-September 09

Re: Prevent duplicates in datagridview

Posted 16 January 2011 - 07:30 AM

I think there is a method to search the cells in a datagridview but off the top of my head this should work
It will iterate through the rows and check the column for a matching value and set a boolean to true if found.

 Dim exists As Boolean = False
        For Each itm As DataGridViewRow In dgAppraisal.Rows
            If itm.Cells(0).Value = cmbAssementArea.Text Then
                exists = True
            End If
        Next
        If Me.dgAppraisal.Rows.Count > 0 AndAlso exists = True Then
...
Cells(0) is the first column, you will need to change the number to the column index you want to search.

This post has been edited by CharlieMay: 16 January 2011 - 07:30 AM

Was This Post Helpful? 1
  • +
  • -

#3 Essel   User is offline

  • D.I.C Head

Reputation: 5
  • View blog
  • Posts: 118
  • Joined: 08-May 09

Re: Prevent duplicates in datagridview

Posted 16 January 2011 - 07:57 AM

View PostCharlieMay, on 16 January 2011 - 07:30 AM, said:

I think there is a method to search the cells in a datagridview but off the top of my head this should work
It will iterate through the rows and check the column for a matching value and set a boolean to true if found.

 Dim exists As Boolean = False
        For Each itm As DataGridViewRow In dgAppraisal.Rows
            If itm.Cells(0).Value = cmbAssementArea.Text Then
                exists = True
            End If
        Next
        If Me.dgAppraisal.Rows.Count > 0 AndAlso exists = True Then
...
Cells(0) is the first column, you will need to change the number to the column index you want to search.


It work perfectly for me.Thank you very much i most grateful
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1