Using the C1FlexGrid Control > Editing Cells > Value-Mapped Lists |
The ComboList property described above ensures that cell values are selected from a list. The value selected by the user is converted into the appropriate type for the column and stored in the grid, exactly as if the user had typed the value.
In many cases, cells can assume values from well-defined lists, but you want to display a user-friendly version of the actual value. For example, if a column contains product codes, you may want to store the code but display the product name instead.
This is accomplished with the DataMap property. This property contains a reference to an IDictionary object that establishes the mapping between what is stored in the grid and what is visible to the user (the IDictionary interface is defined in the System.Collections namespace, and is implemented by the Hashtable class among others).
For example, the code below creates a data map that contains color values and their names. The colors are stored in the grid, and their names are displayed to the user:
To write code in Visual Basic
Visual Basic |
Copy Code
|
---|---|
Dim dtMap As Hashtable = New Hashtable() dtMap.Add(Color.Red, "Apple") dtMap.Add(Color.Green, "Forest") dtMap.Add(Color.Blue, "Sky") dtMap.Add(Color.Black, "Coal") dtMap.Add(Color.White, "Snow") _flex.Cols(1).DataType = GetType(Color) _flex.Cols(1).DataMap = dtMap |
To write code in C#
C# |
Copy Code
|
---|---|
System.Collections.Hashtable dtMap = new System.Collections.Hashtable(); dtMap.Add(Color.Red, "Apple"); dtMap.Add(Color.Green, "Forest"); dtMap.Add(Color.Blue, "Sky"); dtMap.Add(Color.Black, "Coal"); dtMap.Add(Color.White, "Snow"); _flex.Cols[1].DataType = typeof(Color); _flex.Cols[1].DataMap = dtMap; |
Any class that implements IDictionary can be used as a DataMap. For example, Hashtable, ListDictionary, and SortedList all provide valid data maps. The difference is that when they are used in editable columns, the order of the items in the drop down list will depend on the class.
The SortedList class sorts items by key, Hashtable uses an arbitrary order, and ListDictionary keeps the order in which items were added to the list. Because of this, ListDictionary is usually the best choice for DataMaps.
Note that the keys in the data map must be of the same type as the cells being edited. For example, if a column contains short integers (Int16), then any data maps associated with the column should have short integer keys. Using regular integers (Int32) as keys would not work.
The example below shows the difference:
To write code in Visual Basic
Visual Basic |
Copy Code
|
---|---|
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load ' Sorts by key. Dim sl As New SortedList() sl.Add("0", "Zero") sl.Add("1", "One") sl.Add("2", "Two") sl.Add("3", "Three") ' Keeps Add order. Dim ld As New Specialized.ListDictionary() ld.Add(0, "Zero") ld.Add(1, "One") ld.Add(2, "Two") ld.Add(3, "Three") ' Arbitrary order. Dim ht As New Hashtable() ht.Add(0, "Zero") ht.Add(1, "One") ht.Add(2, "Two") ht.Add(3, "Three") _flex.Cols(1).DataMap = sl _flex.Cols(1).Caption = "SortedList" _flex.Cols(2).DataMap = ld _flex.Cols(2).Caption = "ListDictionary" _flex.Cols(3).DataMap = ht _flex.Cols(3).Caption = "HashTable" End Sub |
To write code in C#
C# |
Copy Code
|
---|---|
private void Form1_Load(object sender, System.EventArgs e); { // Sorts by key. System.Collections.SortedList sl = new System.Collections.SortedList(); sl.Add("0", "Zero"); sl.Add("1", "One"); sl.Add("2", "Two"); sl.Add("3", "Three"); // Keeps Add order. System.Collections.Specialized.ListDictionary ld = new System.Collections.Specialized.ListDictionary(); ld.Add(0, "Zero"); ld.Add(1, "One"); ld.Add(2, "Two"); ld.Add(3, "Three"); // Arbitrary order. System.Collections.Hashtable ht = new System.Collections.Hashtable(); ht.Add(0, "Zero"); ht.Add(1, "One"); ht.Add(2, "Two"); ht.Add(3, "Three"); _flex.Cols[1].DataMap = sl; _flex.Cols[1].Caption = "SortedList"; _flex.Cols[2].DataMap = ld; _flex.Cols[2].Caption = "ListDictionary"; _flex.Cols[3].DataMap = ht; _flex.Cols[3].Caption = "HashTable"; } |
Also, if the column's DataType property is set to an enumeration, the grid will automatically build and use a data map with the names of each value in the enumeration. For example, the following code creates an enumeration containing countries. The country values are stored in the grid, and their names are displayed to the user:
To write code in Visual Basic
Visual Basic |
Copy Code
|
---|---|
Private Enum Countries NewYork Chicago NewOrleans London Paris End Enum Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load _flex.Cols(1).DataType = GetType(Countries) End Sub |
To write code in C#
C# |
Copy Code
|
---|---|
private enum Countries { NewYork, Chicago, NewOrleans, London, Paris } private void Form1_Load(object sender, EventArgs e) { _flex.Cols[1].DataType = typeof(Countries); } |