使用ComboBox中的默认值从DataSet或DataReader填充ComboBox [英] populate ComboBox from DataSet or DataReader with a default value in ComboBox
问题描述
在谷歌的帮助下,我来到这里问这个。
我在Sql Server DB中有一个名为Agent的表,其中包含AgentId和AgentName列。使用Visual Studio 2008(VB.NET),我正在开发一个基于Windows的应用程序。我在Windows窗体中有一个ComboBox控件。我需要使用代理表填充此ComboBox控件。我使用了以下代码,但仍无法将Select Agent设置为默认选择值。
ComboBox.DataSource = DataSet1.Tables( 0 )
ComboBox.DisplayMember = AgentName
ComboBox.ValueMember = AgentId
当我使用添加选择代理时,
ComboBox.Items .Add( 选择代理)
,我得到错误, 设置DataSource属性时无法修改项目集合。
任何帮助都非常感谢。谢谢!
在下面尝试:
ComboBox.Items.Add( 选择代理)
ComboBox.DataSource = DataSet1.Tables( 0 )
ComboBox.DisplayMember = AgentName
ComboBox。 ValueMember = AgentId
ComboBox.DataBind() // 将此行添加到代码中
ComboBox.SelectedValue = 选择代理
请参阅以下链接
http://code.msdn.microsoft.com/windowsdesktop/ComboBox-Databinding-ab61d9d5 [ ^ ]
搞定了!检查以下链接
http://stackoverflow.com/a/ 194276分之14868976
Out of no help in Google, I came here to ask this.
I have a table named Agent with columns AgentId and AgentName in Sql Server DB. Using Visual Studio 2008(VB.NET), I am developing a windows based app. I have a ComboBox control in a windows form. I need this ComboBox control to be filled with the Agent Table. I used the following code, but still am not able to set "Select Agent" as the default selected value.
ComboBox.DataSource = DataSet1.Tables(0)
ComboBox.DisplayMember = "AgentName"
ComboBox.ValueMember = "AgentId"
When I add "Select Agent" using,
ComboBox.Items.Add("Select Agent")
, I get the Error, "Items collection cannot be modified when the DataSource property is set."
Any help is highly appreciated. Thanks!
try below:
ComboBox.Items.Add("Select Agent") ComboBox.DataSource = DataSet1.Tables(0) ComboBox.DisplayMember = "AgentName" ComboBox.ValueMember = "AgentId" ComboBox.DataBind() // Add This Line into Your Code ComboBox.SelectedValue = "Select Agent"
Refer below link
http://code.msdn.microsoft.com/windowsdesktop/ComboBox-Databinding-ab61d9d5[^]
Got it! check the link below
http://stackoverflow.com/a/14868976/194276
这篇关于使用ComboBox中的默认值从DataSet或DataReader填充ComboBox的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!