Sorx Sorx - 4 months ago 12
Vb.net Question

VB.Net Datatable into Dropdowlist using 2 rows as one Item

I got a DataTable filled with forename and surname and I want to display both as one item in a DropdownList

Dim strSql As String = "SELECT * FROM Personen"
dtb.Clear()
Using dad As New OleDbDataAdapter(strSql, connection)
dad.Fill(dtb)
End Using
ddlName.Items.Clear()
ddlName.DataSource = dtb.DefaultView
ddlName.DataTextField = "Nachname"
ddlName.DataValueField = "sozNr"
ddlName.DataBind()


Thats what I have right now but it only displays the surname("Nachname")
So my question is how to display the forename + surname as one Item in the Dropdownlist?

Answer

You can create a calculated column in the DataTable that is the union of these two fields and display it in the DropDownList column

dtb.Columns.Add("DataTextField", GetType(String), "forename + ' ' + surname")
ddlName.DataTextField = "DataTextField"
ddlName.DataValueField = "sozNr"