Best Practices for Placing a DropDown or Combo in an WinGrid Cell
It is often desirable to place a dropdown list of choices in a cell of the grid. This has several advantages:
There are several different ways to attach a list of values to a grid column. The simplest of these is the ValueList property of the Column. The ValueList property of a column or cell can accept any object which implements the IValueList interface, such as a ValueList object or an UltraDropDown control. The UltraCombo control also implements IValueList, but it is generally desirable to use UltraDropDown instead of UltraCombo, because it has less overhead.
The ValueList is a very simple, light, and easy-to-use object. You can create a ValueList by using the ValueLists collection on the grid's DisplayLayout. Each ValueListItem has a DataValue and a DisplayText, so the ValueList can translate ID's into user-friendly text. The ValueList displays only a single column of values with no column header, much like the Microsoft ComboBox. ValueLists cannot be bound, they must be populated manually either at design-time or run-time.
ValueList in VB:
Private Sub UltraGrid1_InitializeLayout(ByVal sender As System.Object, _ ByVal e As Infragistics.Win.UltraWinGrid.InitializeLayoutEventArgs) _ Handles UltraGrid1.InitializeLayout Dim vl As ValueList If (Not e.Layout.ValueLists.Exists("MyValueList")) Then vl = e.Layout.ValueLists.Add("MyValueList") vl.ValueListItems.Add(1, "A") vl.ValueListItems.Add(2, "B") vl.ValueListItems.Add(3, "C") End If e.Layout.Bands(0).Columns("Int32 1").ValueList = e.Layout.ValueLists("MyValueList") End Sub
ValueList in C#:
private void ultraGrid1_InitializeLayout(object sender, Infragistics.Win.UltraWinGrid.InitializeLayoutEventArgs e) { ValueList vl; if ( ! e.Layout.ValueLists.Exists("MyValueList")) { vl = e.Layout.ValueLists.Add("MyValueList"); vl.ValueListItems.Add(1, "A"); vl.ValueListItems.Add(2, "B"); vl.ValueListItems.Add(3, "C"); } e.Layout.Bands[0].Columns["Int32 1"].ValueList = e.Layout.ValueLists["MyValueList"]; }
UltraDropdown is a much more powerful ValueList. It is essentially a dropdown UltraWinGrid (although it is limited to a single band). Just like the WinGrid, it must be bound and has a wide range of Appearances and features such as sorting, filtering and resizable columns. It can display multiple columns, just like the WinGrid. To translate ID's into user-friendly text, set the ValueMember and DisplayMember properties which detmermine the ID and text columns respectively.
UltraDropDown in VB:
Private Sub Form1_Load(ByVal sender As System.Object, _ ByVal e As System.EventArgs) Handles MyBase.Load Me.BindUltraDropDown() End Sub Private Sub BindUltraDropDown() Dim dt As DataTable = New DataTable() dt.Columns.Add("ID", GetType(Integer)) dt.Columns.Add("DisplayText", GetType(String)) dt.Rows.Add(New Object() {1, "A"}) dt.Rows.Add(New Object() {2, "B"}) dt.Rows.Add(New Object() {3, "C"}) dt.AcceptChanges() Me.UltraDropDown1.SetDataBinding(dt, Nothing) Me.UltraDropDown1.ValueMember = "ID" Me.UltraDropDown1.DisplayMember = "DisplayText" End Sub Private Sub UltraGrid1_InitializeLayout(ByVal sender As System.Object, _ ByVal e As Infragistics.Win.UltraWinGrid.InitializeLayoutEventArgs) _ Handles UltraGrid1.InitializeLayout e.Layout.Bands(0).Columns("Int32 1").ValueList = Me.UltraDropDown1 End Sub
UltraDropDown in C#:
private void Form1_Load(object sender, System.EventArgs e) { this.BindUltraDropDown(); } private void BindUltraDropDown() { DataTable dt = new DataTable(); dt.Columns.Add("ID", typeof(int)); dt.Columns.Add("DisplayText", typeof(string)); dt.Rows.Add(new object[] {1, "A"}); dt.Rows.Add(new object[] {2, "B"}); dt.Rows.Add(new object[] {3, "C"}); dt.AcceptChanges(); this.ultraDropDown1.SetDataBinding(dt, null); this.ultraDropDown1.ValueMember = "ID"; this.ultraDropDown1.DisplayMember = "DisplayText"; } private void ultraGrid1_InitializeLayout(object sender, Infragistics.Win.UltraWinGrid.InitializeLayoutEventArgs e) { e.Layout.Bands[0].Columns["Int32 1"].ValueList = this.ultraDropDown1; }
The UltraCombo can be used as a ValueList and it has essentially the same features as UltraDropDown. However, UltraCombo is intended for use as a standalone ComboBox. So while it can be used as a ValueList, there is no reason to do so. It has no advantage over UltraDropDown and has greater overhead.
In addition to being an IValueList, the UltraCombo is also an Editor. So it can also be assigned as the EditorControl of a column. This has certain advantages over the UltraDropDown, such as the ability to use a DataFilter or provide EditorButtons. But if you are not using these features, the extra overhead of UltraCombo means that the UltraDropDown is a better choice.
By default, when a ValueList is attached to a grid column, the user may choose an item from the list or may type into the cell. The grid will attempt to find the closest matching item on the list and fill in the cell (AutoComplete). If no match is found, the user may enter text that does not exist on the list. To prevent this, set the DropDownStyle property of the grid column to DropDownList. This will prevent the user from typing into the cell, although the keyboard will still function for searching through the list.