'Initializing data table before use'

'Initializing data table before use'

我该如何解决这个问题?

我正在做一个简单的数据库项目,我有一个搜索字段我编写了以下代码用于在 DataGridView 中搜索和显示数据但是 空引用异常不断出现,因为我正在初始化 table 什么都没做,但如果我不这样做,我会收到“在初始化之前使用”警告。

问题是:如何正确初始化 table。

Private Sub txtSearch_TextChanged(ByVal sender As System.Object, ByVal e As    System.EventArgs) Handles txtSearch.TextChanged
    Dim searchText As String = txtSearch.Text
    Dim matchText As String = ""
    Dim rowMatch As Boolean = False
    Dim foundRows As DataTable = Nothing 'this initializig causes the   problem of null exception,But how can i Initialize it then?????


For Each rw As DataRow In dataSet.Tables(0).Rows

        Dim cl As String = rw.Item(1).ToString '  this cell is FirstName  Cell
        If searchText.Length > cl.ToString.Length Then
            matchText = cl.ToString
        Else
            matchText = cl.ToString.Substring(0, searchText.Length)
        End If

        'bellow it adds the Found Row (rw) to the table 
        If (searchText.Equals(matchText) And searchText <> "" And Not foundRows Is Nothing) Then foundRows.Rows.Add(rw)

Next


    'to shows data if the search text field is not empty then show the found matching rows
    If (searchText <> "") Then
        contactView.DataSource = foundRows
    Else ' else show the original tavle again 
        contactView.DataSource = dataSet.Tables(0)
    End If
    contactView.Refresh() 'refresh 
End Sub

here is the screenshot of the form

我尝试使用

Dim foundRows As DataTable = New DataTable 

但它显示 ArgumentException

这一行已经属于另一个Table

i also tried this

但是您看不到任何效果,请帮助!

是的 我终于明白了

我在列中遇到问题

Dim foundRows As DataTable = New DataTable("PseuContact")  'this initializig causes the problem of null exception,But how can i Initialize it then?????
   foundRows.Columns.Add("ID")
    foundRows.Columns.Add("First Name")
    foundRows.Columns.Add("Last Name")
    foundRows.Columns.Add("Phone Number")
    foundRows.Columns.Add("Mobile Number")
    foundRows.Columns.Add("Email Address")

声明新列时初始化列很重要 table

这是最好的