تقييم الموضوع :
  • 0 أصوات - بمعدل 0
  • 1
  • 2
  • 3
  • 4
  • 5
[سؤال] الحفظ و التعديل و الحذف داخل datagridview and databases Sql server
#1
السلام عليكم ورحمة الله
من فضلكم عاوز كود للحفظ و التعديل و الحذف و البحث و الطباعه داخل Datagridview و يتم تحديث البيانات الى قاعده البيانات. 
ارجو الرد و جزاكم الله خيراً
الرد }}}
تم الشكر بواسطة:
#2
عزيزى

ارفق مثال ليتم التعديل علية
الرد }}}
تم الشكر بواسطة: ابراهيم ايبو
#3
Imports System.Data.SqlClient
Public Class frmcategory
----------------------------------------------------------------------
Private Sub Btadd_Click(sender As Object, e As EventArgs) Handles Btadd.Click
ClearControls()
CatID.Text = GetMaxID(" Category", "CatID")
End Sub

'التحكم بتسمية العمود وبعرضه
Public Sub DataGridViewHeaderText(DGV As DataGridView)
If DGV.RowCount > 0 Then
With (DGV)
.Columns("CatID").HeaderText = "رقم الصنف"
.Columns("CatID").Width = 50
.Columns("catname").HeaderText = "اسم الصنف"
.Columns("catname").Width = 300
End With
End If
End Sub
Public Sub ClearControls()
Me.CatID.Text = vbNullString
Me.CatName.Text = vbNullString
End Sub
Public Sub Selectall_Category(DGV As DataGridView)
Dim dt_Catgory As New DataTable
Dim da As New SqlDataAdapter
dt_Catgory.Clear()
da = New SqlDataAdapter("select * from Category ", con)
da.Fill(dt_Catgory)
DGV.DataSource = dt_Catgory
End Sub
Private Sub categoryfrm_Load(sender As Object, e As EventArgs) Handles MyBase.Load
Selectall_Category(DGV_Category)
DataGridViewHeaderText(DGV_Category)
End Sub

-------------------------------------------------------------------------------------------------------
Private Sub Btadit_Click(sender As Object, e As EventArgs) Handles Btadit.Click

Update_Category(CatName.Text, CatID.Text)
ClearControls()
CatID.Text = GetMaxID(" Category", "CatID")
Selectall_Category(DGV_Category)
DataGridViewHeaderText(DGV_Category)

End Sub


------------------------------------------------------------------------------------------------------------------------------------------------
Private Sub BtnDelete_Click(sender As Object, e As EventArgs) Handles BtnDelete.Click
If MessageBox.Show("متأكد من الحذف ولا غلطان", "طول بالك", MessageBoxButtons.YesNo, MessageBoxIcon.Stop) = vbNo Then
Exit Sub
Else
Delete_Catgory(DGV_Category)
End If
Selectall_Category(DGV_Category)
DataGridViewHeaderText(DGV_Category)
ClearControls()
CatID.Text = GetMaxID(" Category", "CatID")
End Sub

' متغير التعديل'
Public Sub Update_Category(ByVal Catname As String, ByVal CatID As Int32)
Dim cmd As New SqlCommand
With cmd
.Connection = Con
.CommandType = CommandType.Text
.CommandText = "Update Category set Catname = @Catname where CatID = @CatID"
.Parameters.Clear()
.Parameters.AddWithValue("@Catname", SqlDbType.VarChar).Value = Catname
.Parameters.AddWithValue("@CatID", SqlDbType.Int).Value = CatID
End With
If Con.State = 1 Then Con.Close()
Con.Open()
cmd.ExecuteNonQuery()
Con.Close()
MsgBox("تم التعديل ينجاح", MsgBoxStyle.Information, "وحدالله")
cmd = Nothing
End Sub
' متغير الحذف'
Public Sub Delete_Catgory(DGV_Category As DataGridView)
Dim position As Integer = DGV_Category.CurrentRow.Index
Dim ID_position As Integer = DGV_Category.Rows(position).Cells("CatID").Value
Dim cmd As New SqlCommand
With cmd
.Connection = con
.CommandType = CommandType.Text
.CommandText = "Delete from Category where CatID = @CatID "
.Parameters.Clear()
.Parameters.AddWithValue("@CatID", SqlDbType.Int).Value = ID_position
End With
If con.State = 1 Then con.Close()
con.Open()
cmd.ExecuteNonQuery()
con.Close()
MsgBox("تم التعديل ينجاح", MsgBoxStyle.Information, "وحدالله")
cmd = Nothing
End Sub
' متغير الاضافة'
Public Sub insert_Category(ByVal CatID As Int32, ByVal catname As String)

Dim cmd As New SqlCommand
With cmd
.Connection = Con
.CommandType = CommandType.Text
.CommandText = "insert into Category (CatID,catname)values( @CatID,@catname) "
.Parameters.Clear()
.Parameters.AddWithValue("@catname", SqlDbType.VarChar).Value = catname
.Parameters.AddWithValue("@CatID", SqlDbType.Int).Value = CatID

End With
If Con.State = 1 Then Con.Close()
Con.Open()
cmd.ExecuteNonQuery()
Con.Close()
MsgBox("تمت الاضافة ينجاح", MsgBoxStyle.Information, "وحدالله")
cmd = Nothing
End Sub
Private Sub Btseve_Click(sender As Object, e As EventArgs) Handles Btseve.Click
If CatName.Text = "" Then
MsgBox("يجب ادخال الصنف", MsgBoxStyle.Information, "صلي على النبي")
CatName.Focus()
Else
insert_Category(CatID.Text, CatName.Text)
ClearControls()
CatID.Text = GetMaxID("Category", "CatID")
Selectall_Category(DGV_Category)
DataGridViewHeaderText(DGV_Category)
Me.Refresh()
End If
End Sub

Private Sub DataGridViewHeaderText(p1 As String, p2 As String)
Throw New NotImplementedException
End Sub

Private Sub DGV_Category_Click(sender As Object, e As DataGridViewCellEventArgs)
Try
With DGV_Category()

Me.CatID.Text = .CurrentRow.Cells("CatID").Value.ToString()
Me.CatName.Text = .CurrentRow.Cells("Catname").Value.ToString()

End With

Catch ex As Exception
MessageBox.Show(ex.Message)
End Try
End Sub

Private Sub Btclos_Click(sender As Object, e As EventArgs) Handles Btclos.Click
Me.Close()
End Sub

' Private Sub Update_Category(dataGridView As DataGridView)
' Throw New NotImplementedException
' End Sub


End Class
الرد }}}
تم الشكر بواسطة:


المواضيع المحتمل أن تكون متشابهة .
الموضوع : الكاتب الردود : المشاهدات : آخر رد
  تغيير على كود الحفظ محمد خيري 6 170 أمس, 04:20 PM
آخر رد: محمد خيري
  ارجو التعديل على الدالة المرفقة - الدالة تعيد الرقم -1 new_programer 4 91 20-03-24, 01:18 PM
آخر رد: تركي الحلواني
  تعديل بيانات عمود DataGridView دفعة واحدة مصمم هاوي 2 164 05-03-24, 08:27 PM
آخر رد: مصمم هاوي
  فلترة datagridview بدون قاعدة بيانات صالح عبدالله 3 295 02-02-24, 04:07 PM
آخر رد: صالح عبدالله
  [VB.NET] مشكلة عند الحفظ Abdellatif 6 377 31-01-24, 02:47 AM
آخر رد: Abdellah
  [VB.NET] ظهور التاريخ غير مرتب بالأقدم في datagridview مبرمج صغير 1 4 304 26-01-24, 03:41 PM
آخر رد: atefkhalf2004
  [VB.NET] التاريخ في أداة أبو سامر لطباعة DataGridView مبرمج صغير 1 17 629 26-01-24, 01:52 AM
آخر رد: مبرمج صغير 1
  [VB.NET] مساعدة في استدعاء البيانات معينه من form الأول إلى form 2 بدون التعديل loay775 2 275 18-01-24, 05:04 PM
آخر رد: loay775
  مشكلة في datagridview Adata 4 310 17-01-24, 03:00 PM
آخر رد: aljzazy
  الحذف للسجل بشروط معينة محمد خيري 4 361 05-01-24, 04:46 AM
آخر رد: محمد خيري

التنقل السريع :


يقوم بقرائة الموضوع: بالاضافة الى ( 1 ) ضيف كريم