绑定源不适用于 DataGridView 中具有两个或多个单词的列名


Dim bagentdata As New BindingSource
Dim myCommand As New MySqlCommand
Dim myAdapter As New MySqlDataAdapter
Dim myData As New DataTable
Dim SQL As String
Private Sub frmAgent_Load(sender As Object, e As EventArgs) Handles MyBase.Load
Try
'conn.Open()
ConnectDatabase()
SQL = ""
SQL = "SELECT ID, Name `Agent Name`, Address, Pincode, ContactNo, AltContactNo, SelfIPType, SelfIPRef, SelfIPA, SelfIPP, " & _
"IsIPOPD, IPOPDType, IPOPDValue, IsActive, CreatedBy, CreatedOn FROM tblagentmaster"
myCommand.Connection = Conn
myCommand.CommandText = SQL
myAdapter.SelectCommand = myCommand
myData.Clear()
myAdapter.Fill(myData)
Me.AgentDataView.DataSource = myData
Me.AgentDataView.DefaultCellStyle.Font = New Font("Arial", 10, FontStyle.Bold)
AgentDataView.ReadOnly = True
CloseDatabase()
Catch myerror As MySqlException
MessageBox.Show("Error: " & myerror.Message)
End Try
Clear.PerformClick()
End Sub
Private Sub Search_TextChanged(sender As Object, e As EventArgs) Handles Search.TextChanged
bagentdata.DataSource = myData
AgentDataView.DataSource = bagentdata
bagentdata.Filter = "[Agent Name] Like '%" & Search.Text & "%' " & _
" OR ContactNo = " & Val(Search.Text) & " OR ID = " & Val(Search.Text)
If Search.Text = "" Then
Clear.PerformClick()
End If
End Sub

按代理名称筛选不起作用,请任何需要此帮助的人。按ID或Contactno筛选工作正常。我有另一种形式的类似代码,它运行良好。

编辑:即使我更改SQL=";SELECT ID、名称、地址、Pincode、ContactNo、AltContactNo、SelfIPType、SelfIPRef、SelfIPA、SelfIPP、IsIPOPD、IPOPType、IPOPValue、IsActive、CreatedBy、CreatedOn FROM;和将文本过滤为bagentdata。Filter=";名称类似于"%"&搜索。文本"%’"然后"按名称筛选"也不起作用。下面添加另一个示例代码,其中过滤器是完美工作的:

Sub ClearAll(rindex As Integer)
Try
ConnectDatabase()
SQL = ""
SQL = "SELECT BillNo as BillNO, date(CaseCreatedOn) as 'Booked On', PatientName as `Patient Name` , UHID, PatientAge as Age, PatientSex as Gender FROM tblbooking " & _
"where BillNo in (SELECT DISTINCT BillNo FROM tblbookingdetails WHERE ItemStatus in (7,8)) ORDER BY 'Booked On', BillNO"
myCommand.Connection = Conn
myCommand.CommandText = SQL
myAdapter.SelectCommand = myCommand
myData.Clear()
myAdapter.Fill(myData)
allbilldtv.DataSource = myData
CloseDatabase()
Catch myerror As MySqlException
MessageBox.Show("Error: " & myerror.Message)
End Try
Try
allbilldtv.CurrentCell = allbilldtv.Rows(rindex).Cells(0)
allbilldtv_CellClick(Nothing, Nothing)
Catch err As ArgumentOutOfRangeException
MessageBox.Show("Report Generate Queue All Cleared...!!!", "©2022-25 Healeon™ - All Rights Reserved®", MessageBoxButtons.OK, MessageBoxIcon.Information)
End Try
End Sub
Private Sub CommonSearch_TextChanged(sender As Object, e As EventArgs) Handles CommonSearch.TextChanged
Try
allbilldata.DataSource = myData
allbilldtv.DataSource = allbilldata
allbilldata.Filter = "[Patient Name] Like '%" & CommonSearch.Text.ToUpper & "%' OR BillNO = " & Val(CommonSearch.Text)
If CommonSearch.Text = "" Then
ClearAll(0)
End If
Catch ex As Exception
MsgBox(ex.Message)
End Try
End Sub

根据BindingSource.Filter(此处(的文档:

如果基础数据源是DataSet、DataTable或DataView,可以使用的语法指定布尔表达式DataColumn.Expression属性。

然后根据DataColumn.Expression的文档(此处(:

创建表达式时,使用ColumnName属性引用列。

我建议这样做:

  1. 填充DataTable的数据时设置断点
  2. 跨步(F10快捷键(到下一行
  3. 检查DataTable的Columns属性

我怀疑正在发生的是Fill方法正在对列名进行按摩,以符合正确的命名约定,例如"代理人名称">变为";代理名称">。一旦有了DataColumn的实际名称,就可以在过滤器中使用它。

最新更新