我正在使用Microsoft的Northwind数据库进行练习。我尝试将表中的值填充到Truedbgrid中。那仍然有效,但是之后我注意到3列有一个crlf
。我试图用REPLACE解决它,但是没有用。
我更改了(数据集中的查询)自
SELECT ContactID, ContactType, CompanyName, ContactName, ContactTitle, Address, City, Region, PostalCode, Country, Phone, Extension, Fax, HomePage, PhotoPath, Photo FROM dbo.Contacts
到
SELECT ContactID, ContactType, CompanyName, ContactName, ContactTitle, REPLACE(Address,CHAR(13)+CHAR(10),' '), City, Region, PostalCode, Country, Phone, Extension, Fax, HomePage, PhotoPath, Photo FROM dbo.Contacts
我不知道的另一种方式。我在Internet上搜索,如果有办法对truedbgrid说,请删除所有crlf
。但是我没找到东西。表格中的crlf
是不可见的。我无法删除。有什么想法吗?
我的VB.NET类代码:
Private Sub tdbgContactsParamentrieren()
Try
With tdbgContacts
.AllowAddNew = False
.AllowDelete = False
.AllowColMove = False
.AllowFilter = True
.AllowSort = True
.MultiSelect = MultiSelectEnum.None
.AllowUpdate = True
.AllowUpdateOnBlur = True
.FilterBar = True
.RowHeight = 22
.TabAction = TabActionEnum.ColumnNavigation
.MarqueeStyle = MarqueeEnum.HighlightCell
.Columns(0).Caption = "ID"
.Columns(1).Caption = "Type"
.Columns(2).Caption = "Company"
.Columns(3).Caption = "Name"
.Columns(4).Caption = "Title"
.Columns(5).Caption = "Address"
.Columns(6).Caption = "City"
.Columns(7).Caption = "Region"
.Columns(8).Caption = "Postal Code"
.Columns(9).Caption = "Country"
.Columns(10).Caption = "Phone"
.Columns(11).Caption = "Extension"
.Columns(12).Caption = "Fax"
.Columns(13).Caption = "Home Page"
.Columns(14).Caption = "Photo Path"
With .Splits(0)
.AlternatingRowStyle = True
.EvenRowStyle.BackColor = Color.LightYellow
.EvenRowStyle.VerticalAlignment = AlignVertEnum.Center
.OddRowStyle.VerticalAlignment = AlignVertEnum.Center
.ColumnCaptionHeight = 30
.DisplayColumns(0).Width = 42 'ID
.DisplayColumns(1).Width = 52 'Type
.DisplayColumns(2).Width = 218 'Company
.DisplayColumns(3).Width = 168 'Name
.DisplayColumns(4).Width = 168 'Title
.DisplayColumns(5).Width = 150 'Country
.DisplayColumns(6).Width = 257 'Address
.DisplayColumns(8).Width = 56 'PostalCode
.DisplayColumns(9).Width = 168 'City
.DisplayColumns(10).Width = 90 'Phone
.DisplayColumns(11).Width = 56 'Extension
.DisplayColumns(12).Width = 93 'Fax
.DisplayColumns(13).Width = 200 'HomePage
For index As Integer = 0 To .DisplayColumns.Count - 1
.DisplayColumns(index).HeadingStyle.HorizontalAlignment = AlignHorzEnum.Center
.DisplayColumns(index).Locked = True
Next
End With
End With
Catch ex As Exception
End Try
End Sub
那是在我进行更改之前。
当我这样做时:
SELECT ContactID, ContactType, CompanyName, ContactName, ContactTitle, REPLACE(Address,CHAR(13)+CHAR(10),' '), City, Region, PostalCode, Country, Phone, Extension, Fax, HomePage, PhotoPath, Photo FROM dbo.Contacts
或
SELECT ContactID, ContactType, CompanyName, ContactName, ContactTitle, REPLACE(REPLACE(Address, CHAR(13), ' '), CHAR(10), ' '), City, Region, PostalCode, Country, Phone, Extension, Fax, HomePage, PhotoPath, Photo FROM dbo.Contacts
或
SELECT ContactID, ContactType, CompanyName, ContactName, ContactTitle, REPLACE(REPLACE(cast(Address as nvarchar(max)), CHAR(13), ' '), CHAR(10), ' '), City, Region, PostalCode, Country, Phone, Extension, Fax, HomePage, PhotoPath, Photo FROM dbo.Contacts
编辑2:
我需要表格“联系人”。
答案 0 :(得分:1)
您尚未向我们确切说明如何将网格绑定到数据源,但是根据观察到的行为,似乎它在某种程度上依赖于SELECT查询返回的字段名称。 / p>
因此,如注释中所述,请尝试为REPLACE函数的输出赋予别名,使其具有可识别的名称:
REPLACE(REPLACE(cast(Address as nvarchar(max)), CHAR(13), ' '), CHAR(10), ' ') As Address,
现在,由于该列没有别名,所以我希望它没有绑定到网格中的“地址”列。
P.S。一个单独的问题,这是
Try
...
Catch ex As Exception
End Try
是反模式。捕获异常很好,但是如果这样做,则需要将异常详细信息记录到某个地方(例如,在Windows事件日志中或文件中)。否则,如果代码中出现问题,您将不知道它是什么,因此也就无法尝试对其进行修复。您正在丢弃重要的信息,这些信息旨在帮助您作为开发人员解决问题。