اذهب إلى المحتوى
  • 0

كيف أبحث عن قيمة داخل Datagridview في تطبيق Vb.net؟

Badraoui

السؤال

لدي Datagridview على تطبيق لسطح مكتب باستخدام Vb.net، وقد ملأتها عن طريق جلب البيانات من قاعدة بيانات sql:

 Private Sub TextBox1_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles txtSearch.TextChanged
    Connection.Open()
    Dim dataTable As New DataTable
    Dim dataSet As New DataSet
    dataSet.Tables.Add(dataTable)
    Dim dataAdapter As New OleDbDataAdapter
    Dim SQLQuery As String
    SQLQuery = <sql> 
            SELECT * 
                 FROM Students
                 WHERE StudentFirstName = @StudentFirstName
                </sql> .Value

    dataAdapter = New OleDbDataAdapter(SQLQuery, Connection)
    dataAdapter.SelectCommand.Parameters.Add("@StudentFirstName", SqlDbType.Text).Value = txtStudentFirstname.Text
    dataAdapter.Fill(dataTable)
    dgrStudentDatabaseViewer.DataSource = dataTable.DefaultView
    ShowItems()
    Connection.Close()
End Sub

وأريد البحث عن قيمة معين داخل Datagridview، وهذا هو الكود:

Private Sub ShowItems() ' the following delcleration are used for displaying the contents of the table
    Dim dataAdapter As New OleDbDataAdapter
    Dim DataTable As New DataTable
    Dim DataSet As New DataSet
    Dim SQLQuery As String = <sql>SELECT * FROM Students</sql>
    DataSet.Tables.Add(DataTable)
    dataAdapter = New OleDbDataAdapter(SQLQuery, Connection)
    dataAdapter.Fill(DataTable) ' fills the content from the database into the table in vb net
    dgrStudentDatabaseViewer.DataSource = DataTable.DefaultView
    Connection.Close()
End Sub

لكن عند البحث لا يظهر أي شيء، فما العمل؟

رابط هذا التعليق
شارك على الشبكات الإجتماعية

Recommended Posts

  • 0

الملاحظ هو أنك أنشأت DB Objects جديد مرة أخرى، في حين أن DataAdapter و CommandBuilder يجب ملؤها مرة أخرى، مما يعني تكرار الكود، وكتابته بشكل كثيف، لذا أقترح عليك كتابة الكود بالشكل التالي:

  • تعريف المتغيرات عند إنشاء  النافذة Form:
Public Class Form1
    ' declare some persistant DB objects
    Private myDT As DataTable
    Private myDA As OleDbDataAdapter
    Private myCB As OleDbCommandBuilder
    Private myStudentsDataView As DataView

    Private dbConnStr As String = "(your connection string)"
  • تعريف وإنشاء الكائنات objects:
' initialize the objects
Dim sql = "SELECT * FROM Students"

' this is the ONLY place you use NEW 
' with these objects
myDT = New DataTable
myDA = New OleDbDataAdapter

' create, use and dispose of the connection each time you need one
' even simpler is to use a GetConnection method like I linked 
' to you previously
Using con As OleDbConnection = New OleDbConnection(dbConnStr)
    Using cmd As New OleDbCommand(sql, con)
        con.Open()

        myDA.SelectCommand = cmd
        myDA.Fill(myDT)

        ' "teach" the DA how to Update and Add:
        myDA.UpdateCommand = myCB.GetUpdateCommand
        myDA.InsertCommand = myCB.GetInsertCommand

        myStudentsDataView = myDT.DefaultView

        dgvStudents.DataSource = myStudentsDataView
    End Using

End Using

 

  • وظيفة إضافة Student جديد:
Private Sub AddStudent()
    ' no need to create DataAdapter
    ' because it has form level Scope
    Dim dr = myDT.NewRow
    dr.Item("FirstName") = textbox1.text
    dr.Item("LastName") = textbox2.text
    ' etc etc

    ' add the new row to the datatable
    myDT.Rows.Add(dr)

    ' create a new connection every time
    Using dbcon As New OleDbConnection(dbConnStr)
        ' maybe do this to check:
        If myDA.InsertCommand Is Nothing Then
            myDA.InsertCommand = myCB.GetInsertCommand
        End If

        ' with a persistent DA and CB, this is all you 
        ' need to add a row:
        myDA.Update(myDT)
    End Using

End Sub
  • وظيفة البحث:
Private Sub Search(txt As String)
    myStudentsDataView.RowFilter = String.Format("LastName = {0}", txt)
  • لإلغاء البحث:
myStudentsDataView = myDT.DefaultView

 

رابط هذا التعليق
شارك على الشبكات الإجتماعية

انضم إلى النقاش

يمكنك أن تنشر الآن وتسجل لاحقًا. إذا كان لديك حساب، فسجل الدخول الآن لتنشر باسم حسابك.

زائر
أجب على هذا السؤال...

×   لقد أضفت محتوى بخط أو تنسيق مختلف.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   جرى استعادة المحتوى السابق..   امسح المحرر

×   You cannot paste images directly. Upload or insert images from URL.

  • إعلانات

  • تابعنا على



×
×
  • أضف...