• 0
evry1falls

التعامل مع DataGridView متصلة بـ DBباستخدام Keyboard فقط من أجل من اجل الحفظ و التعديل و الحذف

سؤال

لدينا قاعدة بيانات متصلة بمشروع فيجوال بيزك 2010 به DataGrodView و نريد أن نجعل عمليات الحفظ و الحذف و التعديل و التنقل بين السجلات أسها باستخدام مفاتيح الكايبورد :

 

شكل الفورم :

1.PNG

 

الأكواد :

 

Imports System.Data.OleDbPublic Class Form1    '============================Visit Me=============================='    ' You may use this Application for any purpose, as you wish .    ' Don't forget to build your database as instructed at the tutorial    ' link here : http://adonetaccess2003.blogspot.com/2011/11/datagridview-add-edit-delete-into.html'>http://adonetaccess2003.blogspot.com/2011/11/datagridview-add-edit-delete-into.html    ' For any help using MS VB.Net along with any DB Engine, please    ' Feel free to visit me here : http://adonetaccess2003.blogspot.com    ' Or just : Google me ( Evry1falls - Your Issues ) '    ' Thanx again for using Evry1falls Solutions '    '=================================================================='    Public CN As New OleDb.OleDbConnection    Public RD As OleDbDataReader    Public DBS As New DataSet    Public Ckind As New OleDbCommand    Public Akind As New OleDbDataAdapter    Public CmDB As New OleDbCommandBuilder    Public DkindS As New DataTable    Public Rec As DataRow    Public Frec() As DataRow    Public N, I, CntRs, CntC As Integer    Public SqlStr, xC As String    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load        Dim connectionString As String = "Provider=Microsoft.Ace.OleDB.12.0 ; Data Source = " & Application.StartupPath & "\MyDB.accdb"        Dim sql As String = "SELECT * FROM Kinds"        Dim connection As New OleDbConnection(connectionString)        Dim dataadapter As New OleDbDataAdapter(sql, connection)        Dim ds As New DataSet()        connection.Open()        dataadapter.Fill(ds, "Kinds")        connection.Close()        DG1.DataSource = ds        DG1.DataMember = "Kinds"        DG1.ColumnHeadersBorderStyle = DataGridViewHeaderBorderStyle.Single        DG1.CellBorderStyle = DataGridViewCellBorderStyle.Single        DG1.Columns("Kind").AutoSizeMode = DataGridViewAutoSizeColumnMode.Fill        DG1.Columns("kind").HeaderCell.Value = "Sort Of Items"        Rec = Nothing        Frec = Nothing        PictureBox1.Hide()        Label1.Text = ""    End Sub    Private Sub DG1_CellBeginEdit(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewCellCancelEventArgs) Handles DG1.CellBeginEdit        If CN.State = ConnectionState.Open Then CN.Close()        CN.ConnectionString = "Provider=Microsoft.Ace.OleDB.12.0 ; Data Source = " & Application.StartupPath & "\MyDB.accdb"        CN.Open()        PictureBox1.Show()        Label1.Text = ""        PictureBox1.Image = My.Resources.loading        SqlStr = "Select * from Kinds"        Ckind.Connection = CN        Ckind.CommandText = SqlStr        With Akind            .SelectCommand = Ckind            .Fill(DBS, "Kinds")        End With        DkindS = DBS.Tables("Kinds")        CntC = DG1.Rows.Count        If DG1.Rows(DG1.CurrentCell.RowIndex).IsNewRow = False Then            xC = DG1.CurrentCell.Value.ToString        Else            xC = Nothing        End If    End Sub    Private Sub DG1_CellEndEdit(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewCellEventArgs) Handles DG1.CellEndEdit        If DG1.Rows.Count = CntC Then            Frec = DkindS.Select("Kind = '" & xC & "'")            If Frec.Length = 0 Then                Exit Sub            End If            Rec = Frec(0)            Rec.BeginEdit()            Rec!Kind = DG1.Columns(0).DataGridView(0, DG1.CurrentCell.RowIndex).Value            Rec.EndEdit()            If DBS.HasChanges = True Then                CmDB.DataAdapter = Akind                Akind.Update(DBS, "Kinds")                Label1.Text = "Editted Successfully"                PictureBox1.Hide()            End If            Exit Sub        End If        SqlStr = "Select * from Kinds"        Ckind.Connection = CN        Ckind.CommandType = CommandType.Text        Ckind.CommandText = SqlStr        With Akind            .SelectCommand = Ckind            .Fill(DBS, "Kinds")        End With        DkindS = DBS.Tables("Kinds")        If DG1.Columns(0).DataGridView(0, DG1.CurrentCell.RowIndex).Value Is DBNull.Value Then            PictureBox1.Hide()            Label1.Text = "لا يمكن اضافة نوع فارغ"            Exit Sub        End If        PictureBox1.Show()        Rec = DkindS.NewRow        Rec!KIND = DG1.Columns(0).DataGridView(0, DG1.CurrentCell.RowIndex).Value        DkindS.Rows.Add(Rec)        If DBS.HasChanges = True Then            CmDB.DataAdapter = Akind            Akind.Update(DBS, "Kinds")            Label1.Text = "تم التحديث"            PictureBox1.Hide()        End If    End Sub    Private Sub DG1_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles DG1.KeyDown        If CN.State = ConnectionState.Open Then CN.Close()        CN.ConnectionString = "Provider=Microsoft.Ace.OleDB.12.0 ; Data Source = " & Application.StartupPath & "\MyDB.accdb"        CN.Open()        If e.KeyCode = Keys.Delete Then            If DG1.Rows.GetRowCount(DataGridViewElementStates.Visible) = 1 Then                Label1.Text = "No Data Available"                Exit Sub            End If            If DG1.Rows(DG1.CurrentRow.Index).IsNewRow = True Then                Label1.Text = "Violation - Selected Couldn't be Deleted"                Exit Sub            End If            Dim xc As String            xc = DG1.Columns(0).DataGridView(0, DG1.CurrentCell.RowIndex).Value.ToString            MsgBox(xc)            SqlStr = "Delete From Kinds where (Kind) = '" & xc & "'"            Ckind.Connection = CN            Ckind.CommandText = SqlStr            CntRs = Ckind.ExecuteNonQuery            Label1.Text = (CntRs & " Record(S) Deleted Successfully")            DG1.Rows.Remove(DG1.CurrentCell.OwningRow)            DG1.Refresh()        End If    End SubEnd Class

 

المصدر الأصلى

0

شارك هذا الرد


رابط المشاركة
شارك الرد من خلال المواقع ادناه

0 إجابة على هذا السؤال .

لاتوجد إجابات على هذا السؤال حتى الآن .

من فضلك سجل دخول لتتمكن من التعليق

ستتمكن من اضافه تعليقات بعد التسجيل



سجل دخولك الان

  • يستعرض القسم حالياً   0 members

    لا يوجد أعضاء مسجلين يشاهدون هذه الصفحة .