11. februar 2005 - 10:16Der er
29 kommentarer og 1 løsning
Dropdownlist (.NET)
hvordan indsætter men data fra en database, eksempel vi skal hente ID'et som skal sættes til value i DropDownlisten, hvor den derefter udskriver "Program_navn"(En kolonne i vores tabel) som passer til ID 1 = Microsoft 2 = IBM osv osv osv
du kan bruge følgende kode. jeg har trukket customers tabellen ind fra northwind. herefter har jeg genereret et dataset "Dataset11"
Private Sub Form1_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles MyBase.Load SqlDataAdapter1.Fill(DataSet11) With ComboBox1 .DataSource = DataSet11.Customers() .DisplayMember = DataSet11.Customers.ContactNameColumn.ColumnName .ValueMember = DataSet11.Customers.CustomerIDColumn.ColumnName
End With End Sub jeg kan evt. sende dig et test projekt
Sub Button1_Click(sender As Object, e As EventArgs)
Dim connectionString As String = "server='192.168.1.4'; trusted_connection=true; database='Licenser'" Dim dbConnection As System.Data.IDbConnection = New System.Data.SqlClient.SqlConnection(connectionString)
Dim queryString As String = "SELECT [Programmer].[ID] FROM [Programmer]" Dim dbCommand As System.Data.IDbCommand = New System.Data.SqlClient.SqlCommand dbCommand.CommandText = queryString dbCommand.Connection = dbConnection
Dim dataAdapter As System.Data.IDbDataAdapter = New System.Data.SqlClient.SqlDataAdapter dataAdapter.SelectCommand = dbCommand Dim dataSet As System.Data.DataSet = New System.Data.DataSet dataAdapter.Fill(dataSet) With DropDownList1.DataSource = DataSet.Tables("ID") DropDownList1.DisplayMember = ("Program_Navn")
Sub Button1_Click(sender As Object, e As EventArgs)
Dim connectionString As String = "server='192.168.1.4'; trusted_connection=true; database='Licenser'" Dim dbConnection As System.Data.IDbConnection = New System.Data.SqlClient.SqlConnection(connectionString)
Dim queryString As String = "SELECT [Programmer].[ID], [Program_Navn] FROM [Programmer]" Dim dbCommand As System.Data.IDbCommand = New System.Data.SqlClient.SqlCommand dbCommand.CommandText = queryString dbCommand.Connection = dbConnection
Dim dataAdapter As System.Data.IDbDataAdapter = New System.Data.SqlClient.SqlDataAdapter dataAdapter.SelectCommand = dbCommand Dim dataSet As System.Data.DataSet = New System.Data.DataSet dataAdapter.Fill(dataSet) With DropDownList1 .DataSource = DataSet.Tables(0) .DisplayMember = ("Program_Navn") .ValueMember = "ID" End With End Sub
ovenstående kode burde du kunne skifte ud med din nuværende /Esben
With bliver brugt til at forkorte din kode... dvs. hvis du skrive "with dropdown1" så kan du benytte dig af alle de properties din dropdown udbyder inden for with blokken. /Esben
Tilladte BB-code-tags: [b]fed[/b] [i]kursiv[/i] [u]understreget[/u] Web- og emailadresser omdannes automatisk til links. Der sættes "nofollow" på alle links.