منتدى فيجوال بيسك لكل العرب | منتدى المبرمجين العرب

نسخة كاملة : ما هو الخطأ فى هذا الكود
أنت حالياً تتصفح نسخة خفيفة من المنتدى . مشاهدة نسخة كاملة مع جميع الأشكال الجمالية .
الصفحات : 1 2
لسلام عليكم ورحمه الله
احبتى فى الله 
لدى شاشة كاشير بسيطه اقوم بجلب الاصناف من داخل الداتا جريد
ولكن عند اضافه صنف مكرر فى نفس الفاتورة اريد جمع الكميه والغاء الصف الجديد ان كان مكرر فتظظهر اى رساله الخطأ الموضحه بالصور
جرب الطريقة التالية

PHP كود :
Private Sub DataGridView1_CellValueChanged(sender As ObjectAs DataGridViewCellEventArgsHandles DataGridView1.CellValueChanged
    
If e.ColumnIndex 0 Then ' نفترض أن العمود 0 يحتوي على كود الصنف
        Dim currentRow As DataGridViewRow = DataGridView1.Rows(e.RowIndex)
        Dim currentCode As String = currentRow.Cells(0).Value.ToString()

        For i = 0 To DataGridView1.RowCount - 1
            If i <> e.RowIndex Then
                Dim row As DataGridViewRow = DataGridView1.Rows(i)
                If row.Cells(0).Value IsNot Nothing AndAlso row.Cells(0).Value.ToString() = currentCode Then
                    ' 
صنف مكرر
                    Dim existingQty 
As Integer Val(row.Cells(2).Value' نفترض العمود 2 هو الكمية
                    Dim newQty As Integer = Val(currentRow.Cells(2).Value)
                    row.Cells(2).Value = existingQty + newQty

                    ' 
حذف الصف الجديد بأمان
                    Me
.BeginInvoke(New MethodInvoker(Sub()
                        DataGridView1.Rows.Remove(currentRow)
                    End Sub))
                    Exit For
                End If
            End If
        Next
    End 
If
End Sub 
الف شكر على تعبك ولكن لا يا صديقى لم ينجح الكود
وعليكم السلام ورحمة الله وبركاته

الكود التالي سيعمل معك  Wink
بإذن الله تعالى 

كود :
 Private Sub DataGridView1_CellEndEdit(ByVal sender As System.Object, ByVal e As System.Windows.Forms.DataGridViewCellEventArgs) Handles DataGridView1.CellEndEdit
       Dim pro_code As String = DataGridView1.CurrentRow.Cells(0).Value
       Dim pro_name As String = DataGridView1.CurrentRow.Cells(1).Value
       Dim QTY As String = DataGridView1.CurrentRow.Cells(2).Value
       Dim prise As Decimal = DataGridView1.CurrentRow.Cells(3).Value

       If pro_code = "" Then
           MsgBox("الرجاء عدم ترك خانه رقم الصنف فارغة")
           Return
       End If

       If part_no_Ex(pro_code) = False Then
           If MessageBox.Show("هذا الصنف غير موجود بقاعده البيانات هل تريد تسجيلة ؟؟", "Delete", MessageBoxButtons.YesNo, MessageBoxIcon.Warning) = DialogResult.No Then
               DataGridView1.Rows.Remove(DataGridView1.CurrentRow)
               Return
           End If
           add_pro.TextBox1.Text = pro_code
           add_pro.ShowDialog()
           Return
       End If

       If e.ColumnIndex = 0 Then

           For i As Integer = 0 To DataGridView1.RowCount - 1
               If i <> DataGridView1.CurrentRow.Index AndAlso DataGridView1.Rows(i).Cells(0).Value = pro_code Then

                   Dim existingQty As Integer = Val(DataGridView1.Rows(i).Cells(2).Value)
                   DataGridView1.Rows(i).Cells(2).Value = existingQty + 1

                   DataGridView1.Rows(i).Cells(4).Value = (existingQty + 1) * Val(DataGridView1.Rows(i).Cells(3).Value)

                   DataGridView1.Rows.Remove(DataGridView1.CurrentRow)
                   GoTo UpdateTotal
               End If
           Next

           If con.State = ConnectionState.Open Then
               con.Close()
           End If
           con.Open()
           Dim cmd As New OleDbCommand()
           cmd.Connection = con
           cmd.CommandText = "select * from product where pro_code =@pro_code"
           cmd.Parameters.AddWithValue("@pro_code", pro_code)

           Dim dr As OleDbDataReader = cmd.ExecuteReader
           While dr.Read
               pro_name = dr.Item(1)
               prise = dr.Item(5)
               DataGridView1.CurrentRow.Cells(1).Value = pro_name
               DataGridView1.CurrentRow.Cells(2).Value = 1
               DataGridView1.CurrentRow.Cells(3).Value = prise
               DataGridView1.CurrentRow.Cells(4).Value = prise
           End While
           dr.Close()
           con.Close()

       ElseIf e.ColumnIndex = 2 Then
           Dim total As Decimal
           total = QTY * prise
           DataGridView1.CurrentRow.Cells(4).Value = total
       End If

UpdateTotal:
       Dim sum_total As Decimal = 0
       For i As Integer = 0 To DataGridView1.RowCount - 1
           sum_total += Val(DataGridView1.Rows(i).Cells(4).Value)
       Next
       TextBox3.Text = sum_total

   End Sub
صديقى واخى princelovelorn عامل ايه يرب تكون فى نعمه
ساجرب الكود ان شاء الله
اخى الكريم ظهر لى هذا الخطأ عند حذف الصف عند تكرار كود الصنف

الكود يعمل جيداً أخي


طب تسمحلى تبعتلى السورس اللى شغال ده
يا ريت الفورمة علشان نتعلم منها
وتبقيكسبت ثواب في الجميع
(24-05-25, 07:59 PM)خالد كامل1 كتب : [ -> ]طب تسمحلى تبعتلى السورس اللى شغال ده

بالطبع أخي
في رسائلك

(24-05-25, 10:49 PM)atefkhalf2004 كتب : [ -> ]يا ريت الفورمة علشان نتعلم منها
وتبقيكسبت ثواب في الجميع

أرجو المعذرة منك أخي
هذا المشروع كان الأخ خالد أرسله لي خاص للتعديل عليه
فيجب على أستأذنه أولا
الصفحات : 1 2