![]() |
|
[سؤال] الحفظ و التعديل و الحذف داخل datagridview and databases Sql server - نسخة قابلة للطباعة +- منتدى فيجوال بيسك لكل العرب | منتدى المبرمجين العرب (http://vb4arb.com/vb) +-- قسم : قسم لغة الفيجوال بيسك VB.NET (http://vb4arb.com/vb/forumdisplay.php?fid=182) +--- قسم : قسم اسئلة VB.NET (http://vb4arb.com/vb/forumdisplay.php?fid=183) +--- الموضوع : [سؤال] الحفظ و التعديل و الحذف داخل datagridview and databases Sql server (/showthread.php?tid=36880) |
الحفظ و التعديل و الحذف داخل datagridview and databases Sql server - mo.fathi - 10-11-20 السلام عليكم ورحمة الله من فضلكم عاوز كود للحفظ و التعديل و الحذف و البحث و الطباعه داخل Datagridview و يتم تحديث البيانات الى قاعده البيانات. ارجو الرد و جزاكم الله خيراً RE: الحفظ و التعديل و الحذف داخل datagridview and databases Sql server - mmali127 - 10-11-20 عزيزى ارفق مثال ليتم التعديل علية RE: الحفظ و التعديل و الحذف داخل datagridview and databases Sql server - naserflaha71 - 05-07-21 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 |