Various Data Sources
RadListView is a component that displays data obtained from a database or other data source. The data is presented in a set of predefined layouts or a custom layout you defined for the listview and its items.
There are three ways to bind the listview control to a database:
-
Using declarative data source controls - By using declarative data source controls (introduced in ASP.NET 2.x and ASP.NET 35), you can bind the grid declaratively, with no coding necessary.
-
Using the NeedDataSource event - RadListView calls this event each time it needs a data source.
-
ASP.NET ListView-like binding - You can set the DataSource property manually in the control declaration. This property specifies the database, which is then used as a source for the grid.
When binding RadListView by setting the DataSource property, you can set the DataSource property to an instance of any of the following types:
- DataSet , DataTable , or DataView
- Array of DataRow
- Any object collection that implements the IListSource , IList , IEnumerable , or ICustomTypeDescriptor interface.>
You can use a wide variety of data sources for RadListView structure generation (the only requirement is that these custom objects implement the ITypedList/IEnumarable/ICustomTypeDescriptor interfaces). The code in the next paragraph demonstrates how to:
-
use ArrayList object for Telerik RadListView structure generation
-
bind the listview to list of custom objects (Generic list)
-
bind RadListView to a DataReader
<asp:ScriptManager ID="ScriptManager1" runat="server" />
<br />
<telerik:RadFormDecorator RenderMode="Lightweight" ID="RadFormDecorator1" runat="server" />
<table id="Table1" cellspacing="1" cellpadding="11" border="0" rules="rows" style="width: 95%;">
<tr>
<td style="white-space: nowrap">
<b>Binding RadListView to:</b>
</td>
<td>
</td>
</tr>
<tr>
<td valign="top">
<h3>
Array list:</h3>
</td>
<td>
<telerik:RadListView ID="RadListView1" runat="server" ItemPlaceholderID="CityItemsHolder"
OnNeedDataSource="RadListView1_NeedDataSource">
<LayoutTemplate>
<fieldset style="width: 300px;">
<legend>Cities</legend>
<asp:Panel ID="CityItemsHolder" runat="server" />
</fieldset>
</LayoutTemplate>
<ItemTemplate>
<span>
<%#<legacyBold xmlns="http://ddue.schemas.microsoft.com/authoring/2003/5"> Container.DataItem </legacyBold>%></span>
</ItemTemplate>
<ItemSeparatorTemplate>
<span style="color: Olive; font-weight: bold;"> :: </span>
</ItemSeparatorTemplate>
</telerik:RadListView>
</td>
</tr>
<tr>
<td valign="top">
<h3>
Generic list:</h3>
</td>
<td>
<telerik:RadListView ID="RadListView2" runat="server" ItemPlaceholderID="ProductsHolder"
OnNeedDataSource="RadListView2_NeedDataSource">
<LayoutTemplate>
<fieldset style="width: 700px;">
<legend>Products</legend>
<asp:Panel ID="ProductsHolder" runat="server" />
</fieldset>
</LayoutTemplate>
<ItemTemplate>
<div style="float: left">
<table cellpadding="0" cellspacing="0" style="width: 230px;">
<tr>
<td style="width: 20%;">
Name:
</td>
<td style="width: 80%; padding-left: 5px;">
<%#<legacyBold xmlns="http://ddue.schemas.microsoft.com/authoring/2003/5"> (Container.DataItem as Telerik.Web.Examples.Grid.MyBusinessObject).Name </legacyBold>%>
</td>
</tr>
<tr>
<td>
Price:
</td>
<td style="width: 80%; padding-left: 5px;">
<%#<legacyBold xmlns="http://ddue.schemas.microsoft.com/authoring/2003/5"> String.Format("{0:C}",(Container.DataItem as Telerik.Web.Examples.Grid.MyBusinessObject).UnitPrice) </legacyBold>%>
</td>
</tr>
<tr>
<td>
Date:
</td>
<td style="width: 80%; padding-left: 5px;">
<%#<legacyBold xmlns="http://ddue.schemas.microsoft.com/authoring/2003/5"> String.Format("{0:d}", (Container.DataItem as Telerik.Web.Examples.Grid.MyBusinessObject).Date) </legacyBold>%>
</td>
</tr>
<tr>
<td>
Available:
</td>
<td style="width: 80%; padding-left: 5px;">
<%#<legacyBold xmlns="http://ddue.schemas.microsoft.com/authoring/2003/5"> ((bool)DataBinder.Eval(Container.DataItem,"Discontinued") == false ? "Yes" : "No") </legacyBold>%>
</td>
</tr>
<tr>
<td>
<hr />
</td>
<td>
<hr />
</td>
</tr>
</table>
</div>
</ItemTemplate>
</telerik:RadListView>
</td>
</tr>
<tr>
<td valign="top">
<h3>
DataReader:</h3>
</td>
<td>
<telerik:RadListView ID="RadListView3" runat="server" ItemPlaceholderID="ShippersHolder"
OnNeedDataSource="RadListView3_NeedDataSource" OnDataBound="RadListView3_DataBound">
<LayoutTemplate>
<fieldset style="width: 300px;">
<legend>Shippers</legend>
<asp:Panel ID="ShippersHolder" runat="server" />
</fieldset>
</LayoutTemplate>
<ItemTemplate>
<table width="100%">
<colgroup>
<col title="Shipper name" />
<col title="Phone" />
</colgroup>
<tr>
<td style="width: 50%">
<%#<legacyBold xmlns="http://ddue.schemas.microsoft.com/authoring/2003/5"> Eval("CompanyName") </legacyBold>%>
</td>
<td style="width: 50%">
<%#<legacyBold xmlns="http://ddue.schemas.microsoft.com/authoring/2003/5"> Eval("Phone") </legacyBold>%>
</td>
</tr>
</table>
</ItemTemplate>
</telerik:RadListView>
</td>
</tr>
</table>
protected void RadListView1_NeedDataSource(object source, Telerik.Web.UI.RadListViewNeedDataSourceEventArgs e)
{
ArrayList list = new ArrayList();
list.Add("New York");
list.Add("Madrid");
list.Add("Rome");
list.Add("Tokio");
list.Add("Moscow");
RadListView1.DataSource = list;
}
protected void RadListView2_NeedDataSource(object source, Telerik.Web.UI.RadListViewNeedDataSourceEventArgs e)
{
MyBusinessObjectCollection col = new MyBusinessObjectCollection();
List<MyBusinessObject> productsCollection = col.Select(1,9);
this.RadListView2.DataSource = productsCollection;
}
SqlConnection conn = null;
SqlDataReader reader = null;
protected void RadListView3_NeedDataSource(object source, Telerik.Web.UI.RadListViewNeedDataSourceEventArgs e)
{
RadListView3.DataSource = ReadRecords("SELECT ShipperID, CompanyName, Phone FROM Shippers");
}
private SqlDataReader ReadRecords(string query)
{
String ConnString = ConfigurationManager.ConnectionStrings["NorthwindConnectionString"].ConnectionString;
conn = new SqlConnection(ConnString);
conn.Open();
SqlCommand cmd = new SqlCommand(query, conn);
reader = cmd.ExecuteReader();
return reader;
}
protected void RadListView3_DataBound(object sender, System.EventArgs e)
{
reader.Close();
conn.Close();
}
using System;
using System.Collections.Generic;
using Telerik.Web.UI;
using System.ComponentModel;
namespace Telerik.Web.Examples.Grid
{
public class MyBusinessObjectCollection
{
const int _maxItems = 100000;
string[] names = new string[]{ "C?te de Blaye", "Boston Crab Meat", "Singaporean Hokkien Fried Mee", "Gula Malacca", "Rogede sild", "Spegesild", "Zaanse koeken", "Chocolade", "Maxilaku", "Valkoinen suklaa" };
double[] prizes = new double[] { 23.2500, 9.0000, 45.6000, 32.0000, 14.0000, 19.0000, 263.5000, 18.4000, 3.0000, 14.0000 };
DateTime[] dates = new DateTime[] { new DateTime(2007, 5, 10), new DateTime(2008, 9, 13), new DateTime(2008, 2, 22), new DateTime(2009, 1, 2), new DateTime(2007, 4, 13), new DateTime(2008, 5, 12), new DateTime(2008, 1, 19), new DateTime(2008, 8, 26), new DateTime(2008, 7, 31), new DateTime(2007, 7, 16) };
bool[] bools = new bool[] { true, false, true, false, true, false, true, false, true, false };
public MyBusinessObjectCollection()
{
}
public int SelectCount()
{
return _maxItems;
}
public List<MyBusinessObject> Select()
{
if (System.Web.HttpContext.Current.Cache["data"] == null)
{
System.Web.HttpContext.Current.Cache["data"] = Select(0, _maxItems);
}
return (List<MyBusinessObject>)System.Web.HttpContext.Current.Cache["data"];
}
public List<MyBusinessObject> Select(int startRowIndex, int maximumRows)
{
Random Random1 = new Random();
List<MyBusinessObject> list = new List<MyBusinessObject>();
int i = startRowIndex;
while (i < Math.Min(_maxItems, startRowIndex + maximumRows))
{
MyBusinessObject Product1 = new MyBusinessObject();
Product1.ID = i;
Product1.Name = names[Random1.Next(9)];
Product1.UnitPrice = prizes[Random1.Next(9)];
Product1.Date = dates[Random1.Next(9)];
Product1.Discontinued = bools[Random1.Next(9)];
list.Add(Product1);
i++;
}
return list;
}
}
public class MyBusinessObject
{
private int _id;
private string _name;
private double _unitPrice;
private DateTime _date;
private bool _discontinued;
public MyBusinessObject()
{
}
public MyBusinessObject(int _id, string _name, double _unitPrice, DateTime _date, bool _discontinued)
{
this._id = _id;
this._name = _name;
this._unitPrice = _unitPrice;
this._date = _date;
this._discontinued = _discontinued;
} public int ID
{
get { return _id; }
set { _id = value; }
}
public string Name
{
get { return _name; }
set { _name = value; }
}
public double UnitPrice
{
get { return _unitPrice; }
set { _unitPrice = value; }
} public DateTime Date
{
get { return _date; }
set { _date = value; }
} public bool Discontinued
{
get { return _discontinued; }
set { _discontinued = value; }
}
}
public class GridGenericSorter<T> : IComparer<T>
{
GridSortExpression _expression = null;
PropertyDescriptor _descriptor = null;
public GridGenericSorter(GridSortExpression expression)
{
_descriptor = TypeDescriptor.GetProperties(GetType().GetGenericArguments()[0]).Find(expression.FieldName, true);
_expression = expression;
}
public int Compare(T o1, T o2)
{
IComparable comparable1 = (IComparable)_descriptor.GetValue(o1);
IComparable comparable2 = (IComparable)_descriptor.GetValue(o2);
if (_expression.SortOrder == GridSortOrder.Ascending)
{
return comparable1.CompareTo(comparable2);
} else
{
return comparable2.CompareTo(comparable1);
}
}
}
public class GridGenericFilterer<T>
{
GridFilterExpression _expression = null;
PropertyDescriptor _descriptor = null;
Type _propertyType = null;
GridKnownFunction _filterFunction;
public GridGenericFilterer(GridFilterExpression expression)
{
_expression = expression;
_descriptor = TypeDescriptor.GetProperties(GetType().GetGenericArguments()[0]).Find(expression.FieldName, true);
_propertyType = _descriptor.PropertyType;
_filterFunction = (GridKnownFunction)Enum.Parse(typeof(GridKnownFunction), _expression.FilterFunction);
}
public bool Filter(T item)
{
object value = _descriptor.GetValue(item);
string stringValue = value.ToString();
if (_propertyType == typeof(String))
{
if (_filterFunction == GridKnownFunction.Contains)
{
return stringValue.Contains(_expression.FieldValue);
}
else if (_filterFunction == GridKnownFunction.DoesNotContain)
{
return !stringValue.Contains(_expression.FieldValue);
}
else if (_filterFunction == GridKnownFunction.StartsWith)
{
return stringValue.StartsWith(_expression.FieldValue);
}
else if (_filterFunction == GridKnownFunction.EndsWith)
{
return stringValue.EndsWith(_expression.FieldValue);
}
else if (_filterFunction == GridKnownFunction.EqualTo)
{
return stringValue.Equals(_expression.FieldValue);
}
else if (_filterFunction == GridKnownFunction.NotEqualTo)
{
return !stringValue.Equals(_expression.FieldValue);
}
else if (_filterFunction == GridKnownFunction.IsEmpty)
{
return stringValue.Equals(String.Empty);
}
else if (_filterFunction == GridKnownFunction.NotIsEmpty)
{
return !stringValue.Equals(String.Empty);
}
else if (_filterFunction == GridKnownFunction.GreaterThan)
{
return stringValue.CompareTo(_expression.FieldValue) > 0;
}
else if (_filterFunction == GridKnownFunction.GreaterThanOrEqualTo)
{
return stringValue.CompareTo(_expression.FieldValue) >= 0;
}
else if (_filterFunction == GridKnownFunction.LessThan)
{
return stringValue.CompareTo(_expression.FieldValue) < 0;
}
else if (_filterFunction == GridKnownFunction.LessThanOrEqualTo)
{
return stringValue.CompareTo(_expression.FieldValue) <= 0;
}
}
else if (_propertyType == typeof(Boolean))
{
if (_filterFunction == GridKnownFunction.EqualTo)
{
return Boolean.Parse(stringValue).Equals(Boolean.Parse(_expression.FieldValue));
}
else if (_filterFunction == GridKnownFunction.NotEqualTo)
{
return !Boolean.Parse(stringValue).Equals(Boolean.Parse(_expression.FieldValue));
}
}
else if (_propertyType == typeof(DateTime))
{
if (_filterFunction == GridKnownFunction.EqualTo)
{
return DateTime.Parse(stringValue).Equals(DateTime.Parse(_expression.FieldValue));
}
else if (_filterFunction == GridKnownFunction.NotEqualTo)
{
return !DateTime.Parse(stringValue).Equals(DateTime.Parse(_expression.FieldValue));
}
else if (_filterFunction == GridKnownFunction.GreaterThan)
{
return DateTime.Parse(stringValue).CompareTo(DateTime.Parse(_expression.FieldValue)) > 0;
}
else if (_filterFunction == GridKnownFunction.GreaterThanOrEqualTo)
{
return DateTime.Parse(stringValue).CompareTo(DateTime.Parse(_expression.FieldValue)) >= 0;
}
else if (_filterFunction == GridKnownFunction.LessThan)
{
return DateTime.Parse(stringValue).CompareTo(DateTime.Parse(_expression.FieldValue)) < 0;
}
else if (_filterFunction == GridKnownFunction.LessThanOrEqualTo)
{
return DateTime.Parse(stringValue).CompareTo(DateTime.Parse(_expression.FieldValue)) <= 0;
}
}
else if (IsNumeric(_propertyType))
{
if (_filterFunction == GridKnownFunction.EqualTo)
{
return Decimal.Parse(stringValue).Equals(Decimal.Parse(_expression.FieldValue));
}
else if (_filterFunction == GridKnownFunction.NotEqualTo)
{
return !Decimal.Parse(stringValue).Equals(Decimal.Parse(_expression.FieldValue));
}
else if (_filterFunction == GridKnownFunction.GreaterThan)
{
return Decimal.Parse(stringValue).CompareTo(Decimal.Parse(_expression.FieldValue)) > 0;
}
else if (_filterFunction == GridKnownFunction.GreaterThanOrEqualTo)
{
return Decimal.Parse(stringValue).CompareTo(Decimal.Parse(_expression.FieldValue)) >= 0;
}
else if (_filterFunction == GridKnownFunction.LessThan)
{
return Decimal.Parse(stringValue).CompareTo(Decimal.Parse(_expression.FieldValue)) < 0;
}
else if (_filterFunction == GridKnownFunction.LessThanOrEqualTo)
{
return Decimal.Parse(stringValue).CompareTo(Decimal.Parse(_expression.FieldValue)) <= 0;
}
}
if (_filterFunction == GridKnownFunction.IsNull)
{
return value.Equals(null);
}
else if (_filterFunction == GridKnownFunction.NotIsNull)
{
return !value.Equals(null);
}
return true;
}
static bool IsNumeric(Type type)
{
if (!type.IsEnum)
{
switch (Type.GetTypeCode(type))
{
case TypeCode.Char:
case TypeCode.SByte:
case TypeCode.Byte:
case TypeCode.Int16:
case TypeCode.UInt16:
case TypeCode.Int32:
case TypeCode.UInt32:
case TypeCode.Int64:
case TypeCode.UInt64:
case TypeCode.Single:
case TypeCode.Double:
case TypeCode.Decimal:
return true;
}
}
return false;
}
}
}