The classes in the C1.Silverlight.Data assembly are a subset of those in the System.Data namespace. This means that you can use your existing ADO.NET-based code in Silverlight applications. For example, the code below shows how you can create and populate a DataTable object using C1.Silverlight.Data:
C# |
Copy Code |
---|---|
//Create DataTable DataTable dt = new DataTable(); // Add some columns dt.Columns.Add("ID", typeof(int)); dt.Columns.Add("First Name", typeof(string)); dt.Columns.Add("Last Name", typeof(string)); dt.Columns.Add("Active", typeof(bool)); // Set primary key column DataColumn dc = dt.Columns["ID"]; dc.AutoIncrement = true; dc.ReadOnly = true; dt.PrimaryKey = dc; // single column, not array // Add some rows for (int i = 0; i < 10; i++) { dt.Rows.Add(i, "First " + i.ToString(), "Last " + i.ToString(), i % 2 == 0); } |
If you've used ADO.NET, the code should look very familiar. The only difference is that the PrimaryKey property on ADO.NET DataTable objects is a vector of columns, while in C1.Silverlight.Data it is a single column instead.
Once you have built the DataTable, you can bind it to other controls using the table's DefaultView property:
C# |
Copy Code |
---|---|
//Bind table to a control DataGrid dg = new DataGrid(); dg.ItemsSource = dt.DefaultView; LayoutRoot.Children.Add(dg); |
Note: You must import the C1.Silverlight.Data.dll (contains the C1Data classes) and System.Windows.Controls.Data.dll (contains Microsoft's DataGrid control) namespaces for the above code to work. |
The DefaultView property returns a DataView object associated with the table. Again, this is exactly the same mechanism used in ADO.NET. The DataView object implements an IEnumerable interface that allows it to be used as a data source for bindable controls and for LINQ queries.
Because the classes in the C1.Silverlight.Data assembly are a subset of those in the System.Data namespace, we will not describe them in detail here. If you are not familiar with these classes, please check the descriptions of the System.Data.DataSet and System.Data.DataTable classes on MSDN.