According to section 2, we can obtain relevant information data from webService. If we want to query the database data, we should use the Select statement on the webservice side to query the table, and then use the GetXml () method of DataSet, obtain the corresponding XML format database table data. In this example, we simulate a series of database table data on the webService side. As follows:
<NewDataSet> <Table> <AddrName> Sichuan </AddrName> <CityName> Chengdu </CityName> <TelNum> 028 </TelNum> </Table> <AddrName> guangdong </AddrName> <CityName> Guangzhou </CityName> <TelNum> 020 </TelNum> </Table> <AddrName> Beijing </AddrName> <CityName> Beijing </CityName> <TelNum> 010 </TelNum> </Table> </NewDataSet>
In the XML data, we can clearly see that the table contains three fields: AddrName, CityName, and TelNum, and the table contains three rows of data.
When Silverlight receives this string of data, we need to declare an object class on the Silverlight side. This object class has three attributes: AddrName, CityName, and TelNum. Each time this object class is instantiated, the object class object is added to an object class set. In this way, this object class object set has three rows of data. Here we first look at the source code of the object class:
/// <Summary> // object class of city information /// </summary> public class CityInformation {private string _ AddrName; private string _ CityName; private string _ TelNum; public string AddrName {get {return _ AddrName;} set {_ AddrName = value ;}} public string CityName {get {return _ CityName ;}set {_ CityName = value ;}} public string TelNum {get {return _ TelNum;} set {_ TelNum = value ;}}}
Here, we receive XML data, parse it, and get the code of the object class object set as follows:
List<CityInformation> cityList = new List<CityInformation>();
// Declare the object class set to save the object class set using (XmlReader xReader = XmlReader. create (new StringReader (xmlStr) {xReader. read (); while (xReader. read () {try {xReader. readToFollowing ("AddrName"); string addrName = xReader. readElementContentAsString (); xReader. readToNextSibling ("CityName"); string cityName = xReader. readElementContentAsString (); xReader. readToNextSibling ("TelNum"); string telNum = xReader. readElementContentAsString ();
// Parse XML data of a row of database. CityInformation cityInfo = new CityInformation (); cityInfo. AddrName = addrName; cityInfo. CityName = cityName; cityInfo. TelNum = telNum; cityList. Add (cityInfo );
// Instantiate the object class and add the object class to the object class set. } Catch (Exception ex) {}} this. ShowCityList. ItemsSource = cityList; // bind the object class set to the DataGrid
In addition, we need to set the column template of the DataGrid here, because the automatically generated Columns cannot meet the needs of the actual project. So let's continue to look at the XAML source code:
<Sdk: DataGrid HorizontalAlignment = "Left" AutoGenerateColumns = "False" Margin = "28,71, 271 "Name =" ShowCityList "verticalignment =" Top "Height =" 324 "Width =" "> <sdk: DataGrid. columns> <sdk: Maid Header = "provincial capital" Binding = "{Binding AddrName}" IsReadOnly = "True" Width = "108"/> <sdk: dataGridTextColumn Header = "city" Binding = "{Binding CityName}" IsReadOnly = "True" Width = "108"/> <sdk: dataGridTextColumn Header = "telephone area code" Binding = "{Binding TelNum}" IsReadOnly = "True" Width = "108"/> </sdk: DataGrid. columns> </sdk: DataGrid>
Of course here we need to introduce Domain Name Space: xmlns: sdk = "http://schemas.microsoft.com/winfx/2006/xaml/presentation/sdk"
AutoGenerateColumns = "False" indicates that columns are not automatically generated in this DataGrid.
<Sdk: DataGridTextColumn Header = "provincial capital" Binding = "{Binding AddrName}" IsReadOnly = "True" Width = "108"/>. In this sentence, set the DataGrid column name to the provincial capital, it is bound to the AddrName attribute of the CityInformation object class. In addition, the default width of this column is 108.
So far, we have bound the data obtained from webService to the DataGrid. I believe there will be many similar columns online. Write it out here, show it to the person in need, and also show it to the TX that comes in touch with Silverlight. I hope it can be written in a simple and clear way.
In this example, VS2010 + Silverlight 4.0 is used as the development environment.
To download the source code, click SLReadXMLForDataGrid.rar.