A typical problem in using a string array is that when you might want to display the array contents in a datagridview, but when I go to set the datasource to the array the resulting datagridview gives you the length of the strings not the actual strings themselves.
This is because the datagrid uses reflection to get the properties of the items returned by the IList implementation (the array). Since the only public property of the string is length, then that is what is returns. Its nice that it even gives you anything really and doesn't just throw an exception, but its not what we are looking for!
There are quite a few ways to get round this small problem and bind to the actual values.
binding to an arraylist of strings
Of course with Generics in .NET 2.0, a much easier implementation is to simply declare an object array of type List<string>, populate it with strings and bind to that instead. The List class is the generic equivalent of the ArrayList class. It implements the IList generic interface using an array whose size is dynamically increased as required, and no casting required.
s.Add(
GridView1.DataSource = s;
GridView1.DataBind();
A simple foreach iteration is possible on the List collection
foreach (string sOutput in s){
Label1.Text +=(sOutput);
}
It's also possible to bind to the items using inline code, in a bound Repeater for example, causing the bind event to occur when we reference the Containers DataItems in the Repeater, which triggers the onDataBinding event to fire behind the scenes.
Taking another simply generic List, this time of type int:
List
s.Add(1);
s.Add(22);
s.Add(33);
customers.DataSource = s;
customers.DataBind();
.....and referencing the container bound items directly in the repeater gives us a lot of formatting flexibility.
<asp:
<ItemTemplate>
<tr>
<td><%#Container.DataItem.ToString()%></td>
</tr>
</ItemTemplate>
</asp:
http://msdn2.microsoft.com/en-us/library/6sh2ey19.aspx
....and another useful link about how DataBinding works behind the scenes is Dotnet Dans site, which can be found at the following URL:
http://dotnetdan.com/articles/aspnet/DataBinding.htm
Another way of course to reference generic lists of data is to stuff your data into a generic DIctionary object, which gives you a lot of flexibility of what you extract from your string and gives you key based access to the values as well as being easy to bind them.
s.Add(1,
s.Add(2,
s.Add(3,
GridView1.DataSource = s.Values;
......and to iterate, its equally as simple!
I do like the dictionary generic object, its really flexible has many uses and as a key pair object that allows easy iteration can save you a lot of time when dealing with collections of data. I do however wish the developers had added a sortByValue method, but they missed that one.
It is possible to sort the dictionary based on its values but you need a few extra lines of code:
Dictionary<string, string> s = new Dictionary<string, string>();s.Add("1", "a Item");s.Add("2", "c Item");s.Add("3", "b Item");
List<KeyValuePair<string, string>> myList = new List<KeyValuePair<string, string>>(s);myList.Sort( delegate(KeyValuePair<string, string> firstPair, KeyValuePair<string, string> nextPair){ return firstPair.Value.CompareTo(nextPair.Value); } );
foreach (KeyValuePair<string, string> myKey in myList) { Response.Write(myKey.Key + " " + myKey.Value); }
Remember Me
The opinions expressed herein are my own personal opinions and do not represent my employer's view in any way.
E-mail