Prikazi cijelu temu 15.06.2018 11:29
Gjoreski Van mreze
Administrator
Registrovan od:02.02.2009
Lokacija:Ohrid Makedonija


Predmet:Re: Kako sprečiti dupli unos u DataGrid (dupli proizvod)
Nemam ja Visual Basic 2017.
Da bi resio tvoj problem treba da uradis probera dali vec si uneo artikal prije unosa novog artikla.
To bi uradio na dogadzaj na data gridu koi se zove Befor Update.

Eve vidi go ovaj kod bi trebalo da zavrsi rabota.
PreuzmiIzvorni kôd (vbnet):
  1.  Dim blnErrorsExist As Boolean = False
  2.     Dim strMsg As String = ""
  3.     Dim strValue As String = ""
  4.     Dim strSQL As System.Text.StringBuilder
  5.     Dim cm As CurrencyManager = _
  6.         CType(DataGrid.BindingContext(DataGrid.DataSource), CurrencyManager)
  7.     Dim intRowCount As Integer = cm.Count - 1
  8.     Dim intRowIndex As Integer = 0
  9.     Dim intRows As Integer = 0
  10.     Dim intColIndex As Integer
  11.     Dim intColCount As Integer = 1
  12.     '
  13.     ' Loop through the datagrid rows.
  14.     '
  15.     For intRowIndex = 0 To intRowCount
  16.         '
  17.         ' Initialize the filter string. Trim values so "JOE" matches "  JOE  ".
  18.         '
  19.         strSQL = "Trim(Cust_Name)='?0' AND (Trim(Cust_Office)='?1' ?2)"
  20.         '
  21.         ' Loop through the row's columns. The key to the Customer table is
  22.         ' Cust_Name. If the key consisted of more than one column you could
  23.         ' easily expand this technique.
  24.         '
  25.         For intColIndex = 0 To intColCount
  26.             '
  27.             ' Get the datagrid cell's value and trim it.
  28.             '
  29.             strValue = DataGrid(intRowIndex, intColIndex).ToString().Trim()
  30.  
  31.             Select Case intColIndex
  32.                 Case 0
  33.                     '
  34.                     ' Customer name is required.
  35.                     '
  36.                     If strValue = "" Then
  37.                         blnErrorsExist = True
  38.                         strMsg = "Missing Customer Name."
  39.                         Exit For
  40.                     End If
  41.                     '
  42.                     ' Build the filter SQL.
  43.                     '
  44.                     strSQL = strSQL.Replace("?0", strValue)
  45.  
  46.                 Case 1
  47.                     '
  48.                     ' The Customer Office can be blank or Null. The filter string
  49.                     ' must catch this and treat the following 2 rows as duplicates:
  50.                     '   Cust Name   Cust Office
  51.                     '   JOE           blank
  52.                     '   JOE           null
  53.                     '
  54.                     strSQL = strSQL.Replace("?1", strValue)
  55.                     If aValue = "" Then
  56.                         strSQL = strSQL.Replace("?2", " OR Cust_Office IS Null")
  57.                     Else
  58.                         strSQL = strSQL.Replace("?2", "")
  59.                     End If
  60.             End Select
  61.         Next
  62.  
  63.         If blnErrorsExist Then Exit For
  64.             '
  65.             ' Look for duplicate rows by applying a filter
  66.             ' and checking the number of rows.
  67.             '
  68.             DS.Tables("Customers").DefaultView.RowFilter = strSQL
  69.             intRows = DS.Tables("Customers").DefaultView.Count
  70.             DS.Tables("Customers").DefaultView.RowFilter = ""
  71.  
  72.             If intRows > 1 Then
  73.                 blnErrorsExist = True
  74.                 strMsg = "Duplicate row found. The Customer Name" & vbCrLf & _
  75.                              "and Customer Office must be unique."
  76.                 Exit For
  77.             End If
  78.         Next
  79.         '
  80.         ' If an error was found go to the bad row and issue a message.
  81.         '
  82.         If blnErrorsExist Then
  83.             DataGrid.Select(intRowIndex)
  84.             '
  85.             ' See text below.            
  86.             '
  87.             DataGrid.ScrollToRow(intRowIndex)
  88.             DataGrid.CurrentRowIndex = intRowIndex
  89.  
  90.             MessageBox.Show(strMsg,"Error Saving Data", _
  91.                 MessageBoxButtons.OK, MessageBoxIcon.Error)
  92.             Return
  93.         End If