我在VB.NET中创建一个Windows窗体,并将datagridview设置为只读两列。
datagridview连接到SQL Server数据库,我引用了两个表。第一列是图像,第二列是4列的串联:Rank,LName,Title,FName。我将第二列设置为wrap,但实际上我需要在名称上方显示Rank,所有这些都在同一个单元格中。
示例:
(Rank)人力资源总监
(姓名)Doe,Jane
目前显示为:
人力资源总监Doe,
简
以下是我目前的代码:
Imports System.Data.SqlClient
Public Class some_digital_directory
Dim conDatabase As New SqlConnection("Server=servername;Database=dbname;Integrated Security=True")
Private Sub some_digital_directory_Load(sender As Object, e As EventArgs) Handles MyBase.Load
Dim comSQL As SqlCommand = conDatabase.CreateCommand()
comSQL.CommandText = "SELECT B.IMAGE01, A.RANK + ' ' + A.LNAME + ' ' + A.TITLE + ', ' + A.FNAME AS NAME
FROM EMPMAST AS A LEFT JOIN PICTURE AS B ON A.PERNO=B.PERNO
WHERE A.HIDE <>'TRUE' AND NAME <>' '
ORDER BY A.LNAME, A.FNAME, A.MNAME"
Dim adptSQL As New SqlDataAdapter()
Dim tblEmployees As New DataTable()
adptSQL.SelectCommand = comSQL
conDatabase.Open()
adptSQL.Fill(tblEmployees)
conDatabase.Close()
dgvEmployees.AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.Fill
dgvEmployees.RowTemplate.Height = 150
dgvEmployees.ScrollBars = ScrollBars.Vertical
dgvEmployees.DataSource = tblEmployees
Dim imgPerson As New DataGridViewImageColumn()
imgPerson = dgvEmployees.Columns(0)
imgPerson.ImageLayout = DataGridViewImageCellLayout.Zoom
答案 0 :(得分:0)
您可以在撰写字符串时添加CRLF,而不是
A.RANK + ' ' + A.LNAME + ' ' + A.TITLE + ', ' + A.FNAME AS NAME
你会有
A.RANK + ' ' + A.LNAME + CHAR(13) + CHAR(10) + A.TITLE + ', ' + A.FNAME AS NAME
虽然在我看来你在LNAME列中有TITLE,反之亦然。
编辑:根据您的评论,您可能需要:
A.RANK + CHAR(13) + CHAR(10) + A.TITLE + ', ' + A.FNAME AS NAME
所以你最终会得到:
Dim connStr As String = "Server=servername;Database=dbname;Integrated Security=True"
Private Sub some_digital_directory_Load(sender As Object, e As EventArgs) Handles MyBase.Load
Using conDatabase As New SqlConnection(connStr)
Using comSQL As SqlCommand = conDatabase.CreateCommand()
comSQL.CommandText = "SELECT B.IMAGE01, A.RANK + CHAR(13) + CHAR(10) + A.TITLE + ', ' + A.FNAME AS NAME
FROM EMPMAST AS A LEFT JOIN PICTURE AS B ON A.PERNO=B.PERNO
WHERE A.HIDE <>'TRUE' AND NAME <>' '
ORDER BY A.LNAME, A.FNAME, A.MNAME"
Dim adptSQL As New SqlDataAdapter()
Dim tblEmployees As New DataTable()
adptSQL.SelectCommand = comSQL
adptSQL.Fill(tblEmployees)
End Using
End Using
' rest of code here
End Sub