在组合框中显示值 [英] Displaying values in a combobox

查看:71
本文介绍了在组合框中显示值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有两个组合框,我想根据第一个组合框中的所选项加载数据。我的代码有效,但它显示的结果就像一个字符数组,每行显示一个字母而不是每个字中的一个字行。



请帮忙。下面的代码将组合框中的值显示为每行中一个字母的数组,而不是每行中的整个单词。

这是我现在使用的代码

I have two combobox that i want to load data based on the selected item on the first combobox.The code i have works but it displays the result like an array of characters showing one letter in each line instead of a word in each line.

Please help out. The code below is displaying the values in the combobox as an array of one letter in each line instead of a whole word in each line.
This is the code i have used now

Public Class Form1
    Dim TextingContext As New TestingEntities
    Dim currentDepartment As New Department

    Private Sub ComboBox1_SelectedIndexChanged(sender As System.Object, e As System.EventArgs) Handles comFaculty.SelectedIndexChanged
        Dim selectedfaculty As Faculty = CType(comFaculty.SelectedItem, Faculty)
        Dim selectedFacultyID As Integer = selectedfaculty.Faculty_ID
        Dim dep As IQueryable(Of Department) = From p In TextingContext.Departments
                                             Where p.Faculty.Faculty_ID = selectedFacultyID
                                             Select p

        Dim selDepartment As List(Of Department) = dep.ToList
        currentDepartment = selDepartment.FirstOrDefault

        comDepartment.DisplayMember = "Department_Name"
        comDepartment.DataSource = currentDepartment.Department_Name.ToList
    End Sub

推荐答案

不应该选择部分选择 p.Department.Department_Name



一般情况下,调试和单步执行代码将帮助您了解错误。

代码将在您遇到错误的行中断。
Should''nt the select part be Select p.Department.Department_Name?

In general, debugging and stepping through your code will help you get an idea on the error.
Code will break on the line you have an error.


您的错误是在这一行:



comDepartment.DataSource = currentDepartment.Department_Name.ToList



据推测,Department_Name是一个字符串。将字符串转换为列表将产生一个包含字符串的字符列表。



我猜你想要的是:



comDepartment.DataSource = selDepartment
Your error is in this line:

comDepartment.DataSource = currentDepartment.Department_Name.ToList

Presumably, Department_Name is a string. Converting a string to a list will yield a list of characters that comprise the string.

I would guess that you intended:

comDepartment.DataSource = selDepartment


Public Sub Button1_Click(ByVal sender As System.Object,ByVal e As System .EventArgs)处理Button1.Click

ComboBox1.Items.Clear()

尝试

db.con.Open()

Dim da As New SqlDataAdapter(select * from student where id> 3,db.con)

Dim ds As New DataSet

da.Fill( ds,1)

对于i as Integer = 0到ds.Tables(1)。Rows.Count - 1

Me.ComboBox1.Items.Add (ds.Tables(1)。行(i)(0))

下一页

Catch ex As Exception

MsgBox(错误:+ ex.Message

最后

db.con.Close()

结束尝试

结束子





Kishor Makwana

软件工程师

Insight Softech

www.insightsoftech.com
Public Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
ComboBox1.Items.Clear()
Try
db.con.Open()
Dim da As New SqlDataAdapter("select * from student where id>3", db.con)
Dim ds As New DataSet
da.Fill(ds, "1")
For i As Integer = 0 To ds.Tables("1").Rows.Count - 1
Me.ComboBox1.Items.Add(ds.Tables("1").Rows(i)(0))
Next
Catch ex As Exception
MsgBox("Error : " + ex.Message)
Finally
db.con.Close()
End Try
End Sub


Kishor Makwana
Software Engineer
Insight Softech
www.insightsoftech.com


这篇关于在组合框中显示值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆