Centar za edukaciju-BiH



#1 13.06.2018 12:52
xl_kid Van mreze
Clan
Registrovan od:11.11.2008
Postovi:154


Predmet:Kako sprečiti dupli unos u DataGrid (dupli proizvod)
Imam perent formu u kojoj unosim kupca i slične podatke a u child DataGrid su proizvodi i količine. Kako da sprečim unos duplih proizvoda?
↑  ↓

#2 13.06.2018 17:34
zxz Van mreze
Administrator
Registrovan od:03.02.2009
Postovi:10,610


Predmet:Re: Kako sprečiti dupli unos u DataGrid (dupli proizvod)
Koja je baza podataka?
U koju bazu smijestas podatke?
Podrška samo putem foruma, jer samo tako i ostali imaju koristi od toga.
↑  ↓

#3 13.06.2018 20:16
Gjoreski Van mreze
Administrator
Registrovan od:02.02.2009
Postovi:1,828


Predmet:Re: Kako sprečiti dupli unos u DataGrid (dupli proizvod)
Pitajne br1 . Dali je vb6 ili neka od verzije Vb,Net a.
Pitajne Br2 . Koja konenekcija koristis.
Pitajne Br3. Jeli datagrid boun ili unbound.
i tako dalje do beskraja.

Najbolje postavi primer pa da resimeo.
↑  ↓

#4 15.06.2018 09:28
xl_kid Van mreze
Clan
Registrovan od:11.11.2008
Postovi:154


Predmet:Re: Kako sprečiti dupli unos u DataGrid (dupli proizvod)
1. - visual studio 2017
2. - Disappointed
3. - bound DataGrid

**************
Public Class Form1
Private Sub PorudzbineBindingNavigatorSaveItem_Click(sender As Object, e As EventArgs) Handles PorudzbineBindingNavigatorSaveItem.Click
Me.Validate()
Me.PorudzbineBindingSource.EndEdit()
Me.TableAdapterManager.UpdateAll(Me.Porudzbine_DataSet)
Me.PorudzbineDetaljiTableAdapter.Update(Me.Porudzbine_DataSet)

End Sub

Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
'TODO: This line of code loads data into the 'Porudzbine_DataSet.PorudzbineDetalji' table. You can move, or remove it, as needed.
Me.PorudzbineDetaljiTableAdapter.Fill(Me.Porudzbine_DataSet.PorudzbineDetalji)
'TODO: This line of code loads data into the 'Porudzbine_DataSet.Relacije' table. You can move, or remove it, as needed.
Me.RelacijeTableAdapter.Fill(Me.Porudzbine_DataSet.Relacije)
'TODO: This line of code loads data into the 'Porudzbine_DataSet.Kupci' table. You can move, or remove it, as needed.
Me.KupciTableAdapter.Fill(Me.Porudzbine_DataSet.Kupci)
'TODO: This line of code loads data into the 'Porudzbine_DataSet.Porudzbine' table. You can move, or remove it, as needed.
Me.PorudzbineTableAdapter.Fill(Me.Porudzbine_DataSet.Porudzbine)

CustomDataGridView1.Columns(4).DefaultCellStyle.Format = "N2"
CustomDataGridView1.Columns(5).DefaultCellStyle.Format = "N2"

End Sub

Private Sub Form1_KeyDown(sender As Object, e As KeyEventArgs) Handles MyBase.KeyDown
If e.KeyCode = Keys.Enter Then
Me.SelectNextControl(Me.ActiveControl, True, True, True, False) 'for Select Next Control
End If
End Sub

Private Sub NazivComboBox_LostFocus(sender As Object, e As EventArgs) Handles NazivComboBox.LostFocus
Dim naziv_dm As New Porudzbine_DataSetTableAdapters.KupciTableAdapter

KupacIDTextBox.Text = naziv_dm.id_dm_ScalarQuery(NazivComboBox.Text)

End Sub

Private Sub CustomDataGridView1_DataError(sender As Object, e As DataGridViewDataErrorEventArgs) Handles CustomDataGridView1.DataError
Dim view As DataGridView = CType(sender, DataGridView)
'view.Rows(e.RowIndex).Cells(e.ColumnIndex).ErrorText = "an error"
'e.ThrowException = False
End Sub

Private Sub CustomDataGridView1_CellEndEdit(sender As Object, e As DataGridViewCellEventArgs) Handles CustomDataGridView1.CellEndEdit

'MessageBox.Show(CustomDataGridView1.Rows(e.RowIndex).Cells(e.ColumnIndex).Value.ToString())
Dim proizvod_info As New Porudzbine_DataSetTableAdapters.ProizvodiTableAdapter
'Dim ukupna_tezina As New Porudzbine_DataSetTableAdapters.PorudzbineDetaljiTableAdapter

If (e.ColumnIndex = 0) Then

If Val(CustomDataGridView1.Rows(e.RowIndex).Cells(e.ColumnIndex).Value) > 0 Then

CustomDataGridView1.Rows(e.RowIndex).Cells(e.ColumnIndex + 1).Value = proizvod_info.naziv_proizvoda_ScalarQuery(CInt(Val(CustomDataGri
dView1.Rows(e.RowIndex).Cells(e.ColumnIndex).Value)))

End If

End If

Dim UkupnaTezina As DataGridView = DirectCast(sender, DataGridView)

UkupnaTezina(5, e.RowIndex).Value = Math.Round(UkupnaTezina(2, e.RowIndex).Value * proizvod_info.TezinaProizvoda_ScalarQuery(UkupnaTezina(0, e.RowIndex).Value) + UkupnaTezina(3, e.RowIndex).Value * proizvod_info.BrojKomPak_ScalarQuery(UkupnaTezina(0, e.RowIndex).Value) * Convert.ToDouble(proizvod_info.TezinaProizvoda_ScalarQuery(UkupnaTezina(0, e.RowIndex).Value)) + proizvod_info.TezinaProizvoda_ScalarQuery(UkupnaTezina(0, e.RowIndex).Value) * Math.Round((Convert.ToDouble(UkupnaTezina(4, e.RowIndex).Value) / Convert.ToDouble(proizvod_info.TezinaProizvoda_ScalarQuery(UkupnaTezina(0, e.RowIndex).Value)))), 2)

If IsDBNull(UkupnaTezina(0, e.RowIndex).Value) Then Exit Sub

' If CustomDataGridView1.RowCount > 1 Then
' Dim amount As Decimal = 0
' For index As Integer = 0 To CustomDataGridView1.RowCount - 1
' amount += Convert.ToDecimal(CustomDataGridView1.Rows(index).Cells(5).Value)
' Next
' TextBox1.Text = Val(amount)
' End If

End Sub

Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
BindingNavigatorAddNewItem.PerformClick()
Me.NazivComboBox.Select()
End Sub

End Class
↑  ↓

#5 15.06.2018 10:29
Gjoreski Van mreze
Administrator
Registrovan od:02.02.2009
Postovi:1,828


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
↑  ↓

#6 15.06.2018 12:20
xl_kid Van mreze
Clan
Registrovan od:11.11.2008
Postovi:154


Predmet:Re: Kako sprečiti dupli unos u DataGrid (dupli proizvod)
Našao sam na netu ovo rešenje.

******************
Private Sub CustomDataGridView1_CellValueChanged(sender As Object, e As DataGridViewCellEventArgs) Handles CustomDataGridView1.CellValueChanged

If CustomDataGridView1.Columns(e.ColumnIndex).Name = "ProizvodIDDataGridViewTextBoxColumn" Then

'Prevent duplicates
For i As Integer = 0 To CustomDataGridView1.RowCount - 1
For j As Integer = i + 1 To CustomDataGridView1.RowCount - 2
If CustomDataGridView1.Rows(i).Cells(0).Value = CustomDataGridView1.Rows(j).Cells(0).Value Then

MsgBox("Ovaj proizvod je unet.", MsgBoxStyle.Information, "Upozorenje")

'CustomDataGridView1.CurrentCell.Value = ""

Continue For
End If

Next
Next

End If

End Sub
*****************

Prijavi mi grešku o duplom unosu što je ok. Kako sada da stovim focus na prvo polje i nastavim sa unosom. Ako stavim #CustomDataGridView1.CurrentCell.Value = ""# dozvoli mi unos ali posle entera obriše tj vrati na "". Šta sad?
↑  ↓

#7 15.06.2018 12:34
Gjoreski Van mreze
Administrator
Registrovan od:02.02.2009
Postovi:1,828


Predmet:Re: Kako sprečiti dupli unos u DataGrid (dupli proizvod)
Moras da kazes na gridi da posle poruka koja dobijes dodaje se novi red
↑  ↓

Stranice (1):1


Sva vremena su GMT +01:00. Trenutno vrijeme: 9: 14 am.