ComboBox.DataSource Property
Definition
Important
Some information relates to prerelease product that may be substantially modified before it’s released. Microsoft makes no warranties, express or implied, with respect to the information provided here.
Gets or sets the data source for this ComboBox.
public:
property System::Object ^ DataSource { System::Object ^ get(); void set(System::Object ^ value); };
public object DataSource { get; set; }
public object? DataSource { get; set; }
member this.DataSource : obj with get, set
Public Property DataSource As Object
Property Value
An object that implements the IList interface or an Array. The default is null
.
Examples
The following example uses the DataSource property to display an array of weekdays in a ComboBox.
using System.Windows.Forms;
namespace ComboBox_DataSource_Sample
{
public partial class Form1 : Form
{
public Form1()
{
// Initialize an array with data to bind to the combo box.
var daysOfWeek =
new[] { "Monday", "Tuesday", "Wednesday",
"Thursday", "Friday", "Saturday",
"Sunday" };
// Initialize combo box
var comboBox = new ComboBox
{
DataSource = daysOfWeek,
Location = new System.Drawing.Point(12, 12),
Name = "comboBox",
Size = new System.Drawing.Size(166, 21),
DropDownStyle = ComboBoxStyle.DropDownList
};
// Add the combo box to the form.
this.Controls.Add(comboBox);
}
}
}
Imports System.Windows.Forms
Public Class Form1
Inherits System.Windows.Forms.Form
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) _
Handles MyBase.Load
Dim comboBox As New ComboBox
Dim daysOfWeek As String() = _
New String() {"Monday", "Tuesday", "Wednesday", _
"Thursday", "Friday", "Saturday", _
"Sunday"}
With comboBox
.DataSource = daysOfWeek
.Location = New System.Drawing.Point(12, 12)
.Name = "comboBox"
.Size = New System.Drawing.Size(166, 21)
.DropDownStyle = ComboBoxStyle.DropDownList
End With
Me.Controls.Add(comboBox)
End Sub
End Class
Remarks
A data source can be a database, a Web service, or an object that can later be used to generate data-bound controls. When the DataSource property is set, the items collection cannot be modified.
Applies to
GitHub에서 Microsoft와 공동 작업
이 콘텐츠의 원본은 GitHub에서 찾을 수 있으며, 여기서 문제와 끌어오기 요청을 만들고 검토할 수도 있습니다. 자세한 내용은 참여자 가이드를 참조하세요.
.NET