DataGridViewComboBoxCell.DataSource Eigenschap
Definitie
Belangrijk
Bepaalde informatie heeft betrekking op een voorlopige productversie die aanzienlijk kan worden gewijzigd voordat deze wordt uitgebracht. Microsoft biedt geen enkele expliciete of impliciete garanties met betrekking tot de informatie die hier wordt verstrekt.
Hiermee haalt u de gegevensbron op waarvan de gegevens de mogelijke selecties bevatten die worden weergegeven in de vervolgkeuzelijst.
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
Waarde van eigenschap
Een IList of IListSource die een verzameling waarden bevat die worden gebruikt voor het leveren van gegevens aan de vervolgkeuzelijst. De standaardwaarde is null.
Uitzonderingen
De opgegeven waarde bij het instellen van deze eigenschap is niet null en is niet van het type IList noch IListSource.
Voorbeelden
In het volgende codevoorbeeld ziet u het gebruik van de DataGridViewComboBoxColumn.DataSource eigenschap, die vergelijkbaar is met deze eigenschap. Dit voorbeeld maakt deel uit van een groter voorbeeld dat beschikbaar is in het overzichtsonderwerp van de DataGridViewComboBoxColumn klas.
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
Opmerkingen
Deze eigenschap wordt doorgaans ingesteld voor een hele kolom met cellen via de DataGridViewComboBoxColumn.DataSource eigenschap.
Stel indien mogelijk in DataSource op een bron die alleen de mogelijke selecties bevat, zoals een kolom met selecties. Vervolgens hoeft de DisplayMember eigenschap niet te worden ingesteld. Maar als de bron ingewikkelder is, stelt u DisplayMember de naam van de eigenschap of kolom in om mogelijke selecties op te halen.
Als DataSource deze is ingesteld op een tekenreeksmatrix, hoeft u deze niet in te stellen omdat ValueMemberDisplayMember elke tekenreeks in de matrix wordt gebruikt voor zowel waarde als weergave.
Als u de DataSource eigenschap wijzigt, wordt de verzameling opnieuw geïnitialiseerd Items en opnieuw getekend.
Wanneer u de waarde van de DataSource eigenschap wijzigt, probeert het besturingselement de ValueMember waarden en DisplayMember eigenschapswaarden met de nieuwe gegevensbron te gebruiken en wordt elke eigenschap null ingesteld op als de waarde ervan niet kan worden gevonden. Eventuele uitzonderingen die tijdens dit proces optreden, worden genegeerd, tenzij het een van de volgende kritieke uitzonderingen is: NullReferenceException, , StackOverflowException, OutOfMemoryException, ThreadAbortException, ExecutionEngineExceptionof IndexOutOfRangeExceptionAccessViolationException.
Wanneer de DataSource gegevensbron is ingesteld, wordt deze DataGridViewComboBoxCell gekoppeld aan de Disposed gebeurtenis van de gegevensbron. Wanneer de DataGridViewComboBoxCell resource is losgekoppeld van de DataGridViewresource, wordt deze niet onmiddellijk vrijgegeven. De DataGridViewComboBoxCell resource wordt vrijgegeven wanneer de gekoppelde gegevensbron wordt verwijderd. Als u de resource voor garbagecollection onmiddellijk wilt vrijgeven, stelt u de DataSource eigenschap in op null.