كود :
Imports System.Data.SqlClient
Public Class Form1
Dim dtUint As New DataTable
'comboBox Add Item
Public query As String
Public command As SqlCommand
Public reader As SqlDataReader
'Public conn As New SqlConnection("Data Source=.\SQLEXPRESS;Initial Catalog=Accounts;Integrated Security=True")
Public conn As New SqlConnection("Data Source=.;Initial Catalog=Accounts;Integrated Security=True")
Private Function GetData(ByVal sql As String) As DataTable
Dim cmd As SqlCommand = New SqlCommand(sql, conn)
cmd.CommandType = CommandType.Text
Dim sda As SqlDataAdapter = New SqlDataAdapter(cmd)
Dim dt As DataTable = New DataTable
sda.Fill(dt)
Return dt
End Function
Private Sub AddUint_Combo()
Dim sda As New SqlDataAdapter("SELECT * FROM Uint_NameTbl", conn)
sda.Fill(dtUint)
With TryCast(dvg_purchas.Columns(2), DataGridViewComboBoxColumn)
.DataSource = dtUint
.DisplayMember = "UintName"
.ValueMember = "id"
End With
Dim dt As New DataTable
sda.Fill(dt)
With txtUintName
.DataSource = dt
.DisplayMember = "UintName"
.ValueMember = "id"
End With
'Next
End Sub
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
AddUint_Combo()
End Sub
Public Sub AddItemList()
'Try
' لاضافة بيانات الصنف الى الجريد
'If e.KeyCode = Keys.Enter Then
For i As Integer = 0 To dvg_purchas.Rows.Count - 1
If txtbarCode.Text = dvg_purchas.Rows(i).Cells(2).Value Then
' فى حالة الغاء عملية تعديل واضافة البيانات على اللى موجود سابقا / هيتم ادراج اخر البيانات و تجاهل البيانات السابقة
If MessageBox.Show(" اضغط نعم لتعديل السعر/ تم إدراج الصنف من قبل هل تريد التعديل ", "اسم الصنف / خطاء تكرار البيانات :" + dvg_purchas.CurrentRow.Cells(2).Value, MessageBoxButtons.YesNo) = Windows.Forms.DialogResult.Yes Then
dvg_purchas.Rows(i).Cells(3).Value = txtQuty.Text
dvg_purchas.Rows(i).Cells(4).Value = txtPrice.Text
Exit Sub
End If
' فى حالة وجود نفس البيانات بيتم التعديل عليها
'dvg_purchas.Rows(i).Cells(9).Value = Val(dvg_purchas.Rows(i).Cells(9).Value) + Val(txtQuty.Text) ' تعديل الكمية
'dvg_purchas.Rows(i).Cells(10).Value = Val(dvg_purchas.Rows(i).Cells(8).Value) * Val(dvg_purchas.Rows(i).Cells(9).Value)
'dvg_purchas.Rows(i).Cells(12).Value = Val(dvg_purchas.Rows(i).Cells(10).Value) - Val(dvg_purchas.Rows(i).Cells(11).Value) ' تعديل اجمالي صافي سعر الشراء
Exit Sub
End If
Next
dvg_purchas.Rows.Add()
dvg_purchas.Rows(dvg_purchas.Rows.Count - 1).Cells(0).Value = txtbarCode.Text '
dvg_purchas.Columns(0).Width = 60
dvg_purchas.Rows(dvg_purchas.Rows.Count - 1).Cells(1).Value = txtitmName.Text '
dvg_purchas.Columns(1).Width = 100
'dvg_purchas.Rows(dvg_purchas.Rows.Count - 1).Cells(2).Value = txtUintName.Text '
dvg_purchas.Rows(dvg_purchas.Rows.Count - 1).Cells(2).Value = txtUintName.SelectedValue '
dvg_purchas.Columns(2).Width = 80
dvg_purchas.Rows(dvg_purchas.Rows.Count - 1).Cells(3).Value = txtQuty.Text '
'dvg_purchas2.Columns(5).Width = 80
dvg_purchas.Rows(dvg_purchas.Rows.Count - 1).Cells(4).Value = txtPrice.Text
''dvg_purchas2.Columns(6).Width = 60
''dvg_purchas.Rows(dvg_purchas.Rows.Count - 1).Cells(7).Value = txtUintName.Text ' الوحدة
''dvg_purchas2.Columns(7).Width = 80
'dvg_purchas.Rows(dvg_purchas.Rows.Count - 1).Cells(8).Value = txtPurchas.Text ' س الشراء
''dvg_purchas2.Columns(8).Width = 60
dvg_purchas_CellEndEdit(dvg_purchas, New DataGridViewCellEventArgs(2, dvg_purchas.Rows.Count - 1))
'Catch ex As Exception
' MsgBox(ex.Message)
' conn.Close()
'End Try
End Sub
Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
AddItemList()
End Sub
'Private Sub txtUintName_DropDown(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles txtUintName.DropDown
' Try
' txtUintName.Items.Clear()
' 'فحص الاتصال بقاعدة البيانات
' If conn.State = ConnectionState.Closed Then conn.Open()
' 'query = "SELECT [AccountName] FROM Uint_NameTbl where [AccountCategory] ='uint '"
' query = "SELECT [UintName] FROM Uint_NameTbl "
' ''''' '" + txtInvType2.Text + "'"
' Command = New SqlCommand(query, conn)
' reader = Command.ExecuteReader()
' If reader.HasRows Then
' While reader.Read()
' txtUintName.Items.Add(reader(0))
' End While
' End If
' reader.Close()
' conn.Close()
' Catch ex As Exception
' MsgBox("The vindName entered is not identical.", MsgBoxStyle.Information + MsgBoxStyle.OkOnly, "Error: Unidentical txtVinName_DropDown")
' MsgBox(ex.Message)
' conn.Close()
' End Try
'End Sub
Private Sub dvg_purchas_CellEndEdit(ByVal sender As System.Object, ByVal e As System.Windows.Forms.DataGridViewCellEventArgs) Handles dvg_purchas.CellEndEdit
If conn.State = ConnectionState.Closed Then
conn.Open()
End If
'Dim a = dvg_purchas.CurrentRow.Index
Dim a = e.RowIndex
Dim dt As New DataTable
dt.Clear()
Dim s As String = dvg_purchas.Rows(a).Cells(2).Value
Dim ADP As New SqlDataAdapter("select id ,UintID,UintName,UintQty from Uint_NameTbl where id='" & s & "' ", conn)
ADP.Fill(dt)
conn.Close()
If dt.Rows.Count > 0 Then
'dvg_purchas.Rows(a).Cells("UintName").Value = dt.Rows(0)("UintQty").ToString
dvg_purchas.Rows(a).Cells(3).Value = dt.Rows(0)("UintQty").ToString
End If
End Sub
End Class