且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

使用存储过程中的数据填充组合框!

更新时间:2023-02-07 12:31:08

最简单的解决方案是设置ItemsSource在组合框上等于你有j的数据表你好。然后将DisplayMemberPath和SelectedValuePath属性定义为您希望的列的名称A)作为显示成员,b)是选定的值。



或者(和我的首选方法),是创建一个对象类型的类,然后根据数据集中的结果填充一组对象。然后使用对象集合来填充组合框。



但是,这通常是因为我正在使用在我的组合框中选中的对象做各种事情。 / BLOCKQUOTE>

<pre lang="vb">

I have a stored procedure that I mus fill in the combobox!
The procedure contains "SELECT ID, name from TblData" and below I will post my codei've done so far!
And I would like someone to help me continue :S Ehere is my error

Dim n As New KonekcijaBaza
       Dim con As New SqlConnection
       con = n.GetCon()

       Dim cmd As New SqlCommand
       cmd.CommandText = "[AP245_IsplatniListi].[PolniComboDirekcii]"
       cmd.CommandType = CommandType.StoredProcedure


       Dim da As New SqlDataAdapter
       Dim ds As New DataSet

       da.SelectCommand = cmd
       da.Fill(ds)




and in teh foram load I have

Dim dbl As New DBLevel
       Dim dv As New DataView
       dv = dbl.PolniCombodirekcija().Tables(0).DefaultView

       cmbDirekci.DisplayMemberPath = "direkcija""
       cmbDirekci.SelectedValue = "sifra_direkcija""

The simplest solution is to set the ItemsSource on the combo box equal to the the data table you have just filled. Then define the DisplayMemberPath and SelectedValuePath properties as the names of the columns you wish to A) be the display member and b) be the selected value.

Alternatively (and my preferred approach), is to create a Class of your object type, then populate a collection of of your objects based on the results in the dataset. then use the object collection to populate the combo box.

However, that is generally because I am doing various things with the object that gets selected in my combo boxes.