在datagridview中的单元格中的多行上连接列
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了在datagridview中的单元格中的多行上连接列相关的知识,希望对你有一定的参考价值。
我在VB.NET中创建一个Windows窗体,并将datagridview设置为只读两列。
datagridview连接到SQL Server数据库,我引用了两个表。第一列是图像,第二列是4列的串联:Rank,LName,Title,FName。我将第二列设置为wrap,但实际上我需要在名称上方显示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
答案
您可以在撰写字符串时添加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
以上是关于在datagridview中的单元格中的多行上连接列的主要内容,如果未能解决你的问题,请参考以下文章
想要连接单元格中的多行数据及其值(如果存在)。如果没有可用的数据,则不应出现 Emp Id