DataGridViewComboBoxCell.DataSource 属性
定义
重要
一些信息与预发行产品相关,相应产品在发行之前可能会进行重大修改。 对于此处提供的信息,Microsoft 不作任何明示或暗示的担保。
获取或设置数据源,其数据包含下拉列表中可能会显示的选项。
public:
virtual property System::Object ^ DataSource { System::Object ^ get(); void set(System::Object ^ value); };
public virtual object DataSource { get; set; }
public virtual object? DataSource { get; set; }
member this.DataSource : obj with get, set
Public Overridable Property DataSource As Object
属性值
一个 IList 或 IListSource,它包含一个值集合,用于向下拉列表提供数据。 默认值是 null
。
例外
设置此属性时指定值不是 null
,并且不属于 IList 或 IListSource 类型。
示例
下面的代码示例演示如何使用 DataGridViewComboBoxColumn.DataSource 属性,该属性类似于此属性。 此示例是类概述主题中提供的更大示例的 DataGridViewComboBoxColumn 一部分。
private:
DataGridViewComboBoxColumn^ CreateComboBoxColumn()
{
DataGridViewComboBoxColumn^ column =
gcnew DataGridViewComboBoxColumn();
{
column->DataPropertyName = ColumnName::TitleOfCourtesy.ToString();
column->HeaderText = ColumnName::TitleOfCourtesy.ToString();
column->DropDownWidth = 160;
column->Width = 90;
column->MaxDropDownItems = 3;
column->FlatStyle = FlatStyle::Flat;
}
return column;
}
private:
void SetAlternateChoicesUsingDataSource(DataGridViewComboBoxColumn^ comboboxColumn)
{
{
comboboxColumn->DataSource = RetrieveAlternativeTitles();
comboboxColumn->ValueMember = ColumnName::TitleOfCourtesy.ToString();
comboboxColumn->DisplayMember = comboboxColumn->ValueMember;
}
}
private:
DataTable^ RetrieveAlternativeTitles()
{
return Populate("SELECT distinct TitleOfCourtesy FROM Employees");
}
String^ connectionString;
private:
DataTable^ Populate(String^ sqlCommand)
{
SqlConnection^ northwindConnection = gcnew SqlConnection(connectionString);
northwindConnection->Open();
SqlCommand^ command = gcnew SqlCommand(sqlCommand, northwindConnection);
SqlDataAdapter^ adapter = gcnew SqlDataAdapter();
adapter->SelectCommand = command;
DataTable^ table = gcnew DataTable();
adapter->Fill(table);
return table;
}
// Using an enum provides some abstraction between column index
// and column name along with compile time checking, and gives
// a handy place to store the column names.
enum class ColumnName
{
EmployeeID,
LastName,
FirstName,
Title,
TitleOfCourtesy,
BirthDate,
HireDate,
Address,
City,
Region,
PostalCode,
Country,
HomePhone,
Extension,
Photo,
Notes,
ReportsTo,
PhotoPath,
OutOfOffice
};
private DataGridViewComboBoxColumn CreateComboBoxColumn()
{
DataGridViewComboBoxColumn column =
new DataGridViewComboBoxColumn();
{
column.DataPropertyName = ColumnName.TitleOfCourtesy.ToString();
column.HeaderText = ColumnName.TitleOfCourtesy.ToString();
column.DropDownWidth = 160;
column.Width = 90;
column.MaxDropDownItems = 3;
column.FlatStyle = FlatStyle.Flat;
}
return column;
}
private void SetAlternateChoicesUsingDataSource(DataGridViewComboBoxColumn comboboxColumn)
{
{
comboboxColumn.DataSource = RetrieveAlternativeTitles();
comboboxColumn.ValueMember = ColumnName.TitleOfCourtesy.ToString();
comboboxColumn.DisplayMember = comboboxColumn.ValueMember;
}
}
private DataTable RetrieveAlternativeTitles()
{
return Populate("SELECT distinct TitleOfCourtesy FROM Employees");
}
string connectionString =
"Integrated Security=SSPI;Persist Security Info=False;" +
"Initial Catalog=Northwind;Data Source=localhost";
private DataTable Populate(string sqlCommand)
{
SqlConnection northwindConnection = new SqlConnection(connectionString);
northwindConnection.Open();
SqlCommand command = new SqlCommand(sqlCommand, northwindConnection);
SqlDataAdapter adapter = new SqlDataAdapter();
adapter.SelectCommand = command;
DataTable table = new DataTable();
table.Locale = System.Globalization.CultureInfo.InvariantCulture;
adapter.Fill(table);
return table;
}
// Using an enum provides some abstraction between column index
// and column name along with compile time checking, and gives
// a handy place to store the column names.
enum ColumnName
{
EmployeeId,
LastName,
FirstName,
Title,
TitleOfCourtesy,
BirthDate,
HireDate,
Address,
City,
Region,
PostalCode,
Country,
HomePhone,
Extension,
Photo,
Notes,
ReportsTo,
PhotoPath,
OutOfOffice
};
Private Function CreateComboBoxColumn() _
As DataGridViewComboBoxColumn
Dim column As New DataGridViewComboBoxColumn()
With column
.DataPropertyName = ColumnName.TitleOfCourtesy.ToString()
.HeaderText = ColumnName.TitleOfCourtesy.ToString()
.DropDownWidth = 160
.Width = 90
.MaxDropDownItems = 3
.FlatStyle = FlatStyle.Flat
End With
Return column
End Function
Private Sub SetAlternateChoicesUsingDataSource( _
ByVal comboboxColumn As DataGridViewComboBoxColumn)
With comboboxColumn
.DataSource = RetrieveAlternativeTitles()
.ValueMember = ColumnName.TitleOfCourtesy.ToString()
.DisplayMember = .ValueMember
End With
End Sub
Private Function RetrieveAlternativeTitles() As DataTable
Return Populate( _
"SELECT distinct TitleOfCourtesy FROM Employees")
End Function
Private connectionString As String = _
"Integrated Security=SSPI;Persist Security Info=False;" _
& "Initial Catalog=Northwind;Data Source=localhost"
Private Function Populate(ByVal sqlCommand As String) As DataTable
Dim northwindConnection As New SqlConnection(connectionString)
northwindConnection.Open()
Dim command As New SqlCommand(sqlCommand, _
northwindConnection)
Dim adapter As New SqlDataAdapter()
adapter.SelectCommand = command
Dim table As New DataTable()
table.Locale = System.Globalization.CultureInfo.InvariantCulture
adapter.Fill(table)
Return table
End Function
' Using an enum provides some abstraction between column index
' and column name along with compile time checking, and gives
' a handy place to store the column names.
Enum ColumnName
EmployeeId
LastName
FirstName
Title
TitleOfCourtesy
BirthDate
HireDate
Address
City
Region
PostalCode
Country
HomePhone
Extension
Photo
Notes
ReportsTo
PhotoPath
OutOfOffice
End Enum
注解
通常,通过 DataGridViewComboBoxColumn.DataSource 属性为整个单元格列设置此属性。
如果可能,将 设置为 DataSource 仅包含可能选择的源,例如选择列。 然后, DisplayMember 无需设置 属性。 但是,如果源更复杂,请设置为 DisplayMember 要从中检索可能选择的属性或列的名称。
如果 DataSource 设置为字符串数组,则 ValueMember 不需要设置 和 DisplayMember ,因为数组中的每个字符串都将用于值和显示。
更改 DataSource 属性会导致单元格重新初始化其 Items 集合并重绘自身。
更改 属性的值DataSource时,控件将尝试将 和 DisplayMember 属性值与新数据源一ValueMember起使用,如果找不到其值,则将每个属性null
设置为 。 在此过程中发生的任何异常将被忽略,除非它是以下关键异常之一:NullReferenceException、StackOverflowException、、OutOfMemoryException、ThreadAbortExceptionExecutionEngineException、 IndexOutOfRangeException或 AccessViolationException。
DataSource设置 后,会DataGridViewComboBoxCell附加到Disposed数据源的 事件。
DataGridViewComboBoxCell从 中分离DataGridView时,资源不会立即释放。 释放 DataGridViewComboBoxCell 附加的数据源时释放资源。 若要立即释放资源进行垃圾回收,请将 DataSource 属性设置为 null
。