'宣言 Public Sub SetDataBinding( _ ByVal dataSource As Object, _ ByVal dataMember As String _ )
public void SetDataBinding( object dataSource, string dataMember )
Private Sub Form1_Load(sender As Object, e As System.EventArgs) ' Create a DataTable. Dim tbl As DataTable = Me.CreateData() ' Assign the DataTable as the UltraOptionSet's data source. Me.ultraOptionSet1.DataSource = tbl ' Since the underlying data list is directly on the data source, there ' is no need to specify a sub-object to get the data from. Setting this ' property to an empty string is not necessary to do. If the data source ' were a DataSet, instead of a DataTable, then it would be necessary to ' specify the name of the DataTable to retrieve the values from. Me.ultraOptionSet1.DataMember = "" ' NOTE: Instead of setting the DataSource and DataMember properties separately, ' it is possible to set them both at once. Use the SetDataBinding method to ' perform both assignments in an atomic operation, like so: ' ' this.ultraOptionSet1.SetDataBinding( tbl, "" ); ' Indicate that values from the "Name" column should be displayed ' in the dropdown and edit portions of the control. Me.ultraOptionSet1.DisplayMember = "Name" ' Indicate that the UltraOptionSet's Value property should return ' values from the "ID" column. Me.ultraOptionSet1.ValueMember = "ID" ' Attach a handler to the InitializeDataItem event so that when ValueListItems ' are created to represent values in the data source, value-specific formatting ' will be applied to them. AddHandler Me.ultraOptionSet1.InitializeDataItem, AddressOf Me.ultraOptionSet1_InitializeDataItem End Sub Private Function CreateData() As DataTable ' Create a DataTable. Dim tbl As New DataTable("FoodItems") ' Supply schematic information. tbl.Columns.Add("ID", GetType(Integer)) tbl.Columns.Add("Name") tbl.Columns.Add("IsOnSale", GetType(Boolean)) ' Supply content. tbl.Rows.Add(New Object() {1, "Crackers", False}) tbl.Rows.Add(New Object() {2, "Beef Jerky", True}) tbl.Rows.Add(New Object() {3, "Strawberry Sherbert", False}) Return tbl End Function Private Sub ultraOptionSet1_InitializeDataItem(sender As Object, e As Infragistics.Win.InitializeDataItemEventArgs) ' Obtain a reference to the object in the data source's underlying list ' which the new ValueListItem represents. Dim view As DataRowView = CType(e.ValueListItem.ListObject, DataRowView) ' If the food item is on sale, set the ValueListItem's background color to golden. If CBool(view.Row("IsOnSale")) Then e.ValueListItem.Appearance.BackColor = Color.Gold End If End Sub
private void Form1_Load(object sender, System.EventArgs e) { // Create a DataTable. DataTable tbl = this.CreateData(); // Assign the DataTable as the UltraOptionSet's data source. this.ultraOptionSet1.DataSource = tbl; // Since the underlying data list is directly on the data source, there // is no need to specify a sub-object to get the data from. Setting this // property to an empty string is not necessary to do. If the data source // were a DataSet, instead of a DataTable, then it would be necessary to // specify the name of the DataTable to retrieve the values from. this.ultraOptionSet1.DataMember = ""; // NOTE: Instead of setting the DataSource and DataMember properties separately, // it is possible to set them both at once. Use the SetDataBinding method to // perform both assignments in an atomic operation, like so: // // this.ultraOptionSet1.SetDataBinding( tbl, "" ); // Indicate that values from the "Name" column should be displayed // in the dropdown and edit portions of the control. this.ultraOptionSet1.DisplayMember = "Name"; // Indicate that the UltraOptionSet's Value property should return // values from the "ID" column. this.ultraOptionSet1.ValueMember = "ID"; // Attach a handler to the InitializeDataItem event so that when ValueListItems // are created to represent values in the data source, value-specific formatting // will be applied to them. this.ultraOptionSet1.InitializeDataItem += new Infragistics.Win.InitializeDataItemHandler( this.ultraOptionSet1_InitializeDataItem ); } private DataTable CreateData() { // Create a DataTable. DataTable tbl = new DataTable( "FoodItems" ); // Supply schematic information. tbl.Columns.Add( "ID", typeof(int) ); tbl.Columns.Add( "Name" ); tbl.Columns.Add( "IsOnSale", typeof(bool) ); // Supply content. tbl.Rows.Add( new object[]{ 1, "Crackers", false } ); tbl.Rows.Add( new object[]{ 2, "Beef Jerky", true } ); tbl.Rows.Add( new object[]{ 3, "Strawberry Sherbert", false } ); return tbl; } private void ultraOptionSet1_InitializeDataItem( object sender, Infragistics.Win.InitializeDataItemEventArgs e ) { // Obtain a reference to the object in the data source's underlying list // which the new ValueListItem represents. DataRowView view = e.ValueListItem.ListObject as DataRowView; // If the food item is on sale, set the ValueListItem's background color to golden. if( view != null && (bool)view.Row["IsOnSale"] ) e.ValueListItem.Appearance.BackColor = Color.Gold; }