vb.net 如何在DataTable中搜索特定记录?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/17366715/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-09 16:58:20  来源:igfitidea点击:

how to search DataTable for specific record?

vb.netwinformsdatatabledatarow

提问by Our Man in Bananas

Hi,

你好,

I have a windows form with 10 text fields and 1 combobox.

我有一个带有 10 个文本字段和 1 个组合框的 Windows 窗体。

When the user selects a record in the combo-box I want to find that record in my form datatable variable (called dtBranches) then populate my 10 textfields from the datarow.

当用户在组合框中选择一条记录时,我想在我的表单数据表变量(称为dtBranches)中找到该记录,然后从数据行填充我的 10 个文本字段。

I tried this:

我试过这个:

Dim dr As System.Data.DataRow
If mSortCode > 0 Then
    dr = dtBranches.Select("SortCode='" & mSortCode & "'")
    Me.txtBranch.Text = dr("Branch").ToString()
    Me.txtBankName.Text = dr("BankName").ToString()
    Me.txtBranchTitle.Text = dr("BranchTitle").ToString()
    Me.txtReference.Text = dr("Ref").ToString
    Me.txtAddr1.Text = dr("Address1").ToString
    Me.txtAddr2.Text = dr("Address2").ToString
    Me.txtAddr3.Text = dr("Address3").ToString
    Me.txtPostCode.Text = dr("PostCode").ToString
    Me.txtTelNo.Text = dr("TelephoneNumber").ToString
    Me.txtTown.Text = dr("Town").ToString
    Me.txtTelNo.Text = dr("TelephoneNumber").ToString
end if

but can't get it to compile...

但无法编译...

What's the correct and best way to do this please?

请执行此操作的正确和最佳方法是什么?

thanks

谢谢

Philip

菲利普

回答by Steve

DataTable.Selectreturns an array of DataRows. You need to declare an array to receive the result

DataTable.Select返回一个 DataRow 数组。您需要声明一个数组来接收结果

Dim dr() As System.Data.DataRow

Of course then you need to check if you have rows returned and address the first row in the array

当然,那么您需要检查是否返回了行并寻址数组中的第一行

dr = dtBranches.Select("SortCode='" & mSortCode & "'")
If dr.Length > 0 Then
    Me.txtBranch.Text = dr(0)("Branch").ToString()
    Me.txtBankName.Text = dr(0)("BankName").ToString()
    ...... and so on ...

回答by Tim Schmelter

I would use Linq-ToDataSetand the strongly typed Fieldmethod instead:

我会改用Linq-ToDataSet强类型Field方法:

Dim matches = From row In dtBranches
              Let SortCode = row.Field(Of String)("SortCode")
              Where SortCode = mSortCode
If matches.Any() Then
    Dim row = matches.First()
    Me.txtBranch.Text = row.Field(Of String)("Branch")
    Me.txtBankName.Text = row.Field(Of String)("BankName")
    Me.txtBranchTitle.Text = row.Field(Of String)("BranchTitle")
    Me.txtReference.Text = row.Field(Of String)("Ref")
    Me.txtAddr1.Text = row.Field(Of String)("Address1")
    Me.txtAddr2.Text = row.Field(Of String)("Address2")
    Me.txtAddr3.Text = row.Field(Of String)("Address3")
    Me.txtPostCode.Text = row.Field(Of String)("PostCode")
    Me.txtTelNo.Text = row.Field(Of String)("TelephoneNumber")
    Me.txtTown.Text = row.Field(Of String)("Town")
Else
    MesageBox.Show("SortCode not found.")
End If

If you want to compare case-insensitively, replace the Whereabove with:

如果要不区分大小写进行比较,请将Where上述内容替换为:

Where StringComparer.OrdinalIgnoreCase.Equals(SortCode, mSortCode)

By the way, you are assigning the telephone number twice.

顺便说一下,您分配了两次电话号码。