ArrayList, Hashtable and SortedList
I was looking for something that will allow me to databind listbox (and dropdown box later) to the data retrieved from a report; in particular, list of all database fields available, like the Field Explorer Database Fiedls list in Crystal Designer.
Initially, I've used ArrayList. That's a nice usefull collection, but problem is you can't achieve "separation of content and presentation". Both value and text for listItems are the same. You end up having something like this:
Not really user friendly, isn't it?
Ok, so I've looked at the available collections in .NET, and there is a better one called hashtable. It's a key-value collection of pair, and you can specify databinding of key and value portion.
lstAvailableDatabaseFields.DataSource = GetAvailableDatabaseFields();
lstAvailableDatabaseFields.DataTextField="value";
lstAvailableDatabaseFields.DataValueField ="key";
lstAvailableDatabaseFields.DataBind();
And, another problem arises - it's not sorted, and it's NOT sortable. To get my list of fields nicely sorted, I had to use different collection - SortedList. It's a combination of ArrayList and HashTable, so exactly suited for my purpose...
Here is the code:
Nice, isn't it?
Initially, I've used ArrayList. That's a nice usefull collection, but problem is you can't achieve "separation of content and presentation". Both value and text for listItems are the same. You end up having something like this:
Not really user friendly, isn't it?
Ok, so I've looked at the available collections in .NET, and there is a better one called hashtable. It's a key-value collection of pair, and you can specify databinding of key and value portion.
lstAvailableDatabaseFields.DataSource = GetAvailableDatabaseFields();
lstAvailableDatabaseFields.DataTextField="value";
lstAvailableDatabaseFields.DataValueField ="key";
lstAvailableDatabaseFields.DataBind();
And, another problem arises - it's not sorted, and it's NOT sortable. To get my list of fields nicely sorted, I had to use different collection - SortedList. It's a combination of ArrayList and HashTable, so exactly suited for my purpose...
Here is the code:
public SortedList GetAvailableDatabaseFields()
{
string strShortFieldName;
SortedList sortedList = new SortedList();
Tables tables = _reportClientDocument.DatabaseController.Database.Tables;
foreach(CrystalDecisions.ReportAppServer.DataDefModel.Table table in tables)
{
foreach(Field field in table.DataFields)
{
char period = '.';
int periodIndex = field.FormulaForm.IndexOf(period);
// parse out table and field name
strShortFieldName = field.FormulaForm.Substring(periodIndex+1, field.FormulaForm.Length-(periodIndex+2));
sortedList.Add(field.FormulaForm, strShortFieldName);
}
}
return sortedList;
}
private void lstAvailableDatabaseFields_Load(object sender, System.EventArgs e)
{
if(!IsPostBack)
{
lstAvailableDatabaseFields.DataSource = GetAvailableDatabaseFields();
lstAvailableDatabaseFields.DataTextField = "value";
lstAvailableDatabaseFields.DataValueField ="key";
lstAvailableDatabaseFields.DataBind();
}
}
Nice, isn't it?
0 Comments:
Post a Comment
<< Home