使用ADO.Net从数据库绑定silverlight中的datagrid

时间:2013-05-13 07:27:46

标签: wcf silverlight c#-4.0

我正在尝试使用ADO.Net将SilverLight PivotGrid绑定到SQL数据库。为此,我添加了一个WCF服务,它与我的本地数据库进行交互,我将该数据传递给我的silverlight项目,我能够将其接收到我的PivotGrid控件的DataSource属性中,但是我无法绑定它到pivotGrid控件。

以下是我在WCF服务中编写的方法

public DataSet GetData()
{
    string connStr = "Server=SYS3248\\SQLEXPRESS; Database=EmployeeDB;    
    Integrated Security=true;";
    SqlConnection conn = new SqlConnection(connStr);
    SqlDataAdapter da = new SqlDataAdapter("select * from Employee", conn);
    DataSet ds = new DataSet();
    da.Fill(ds);
    return ds;
}

这是我的.Xaml文件

<UserControl x:Class="OptimityV5.MainPage"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    mc:Ignorable="d"
    d:DesignHeight="300" d:DesignWidth="400" 
    xmlns:sdk="http://schemas.microsoft.com/winfx/2006/xaml/presentation/sdk" 
    xmlns:dxpg="http://schemas.devexpress.com/winfx/2008/xaml/pivotgrid">

    <Grid x:Name="LayoutRoot" Background="White">

        <dxpg:PivotGridControl x:Name="pivotGridControl1">
           <dxpg:PivotGridControl.Fields>
           <dxpg:PivotGridField Name="Names" FieldName="EmpName" Area="RowArea"/>
           </dxpg:PivotGridControl.Fields>
        </dxpg:PivotGridControl>

    </Grid>
</UserControl>

下面是我的.xaml.cs

public MainPage()
{
    InitializeComponent();
    WcfReference.Service1Client obj = new WcfReference.Service1Client();
    obj.GetDataAsync(10);
        obj.GetDataTableDataAsync();
        obj.GetDataTableDataCompleted += 
        new EventHandler<WcfReference.GetDataTableDataCompletedEventArgs>
            (obj_GetDataTableDataCompleted);

}

void obj_GetDataTableDataCompleted(object sender, 
WcfReference.GetDataTableDataCompletedEventArgs e)
{
    //pivotGridControl1.DataSource = e.Result.Any1.ToString();

    var data = e.Result.Any1.ToString();
    XDocument xdoc = XDocument.Parse(data);
    List<string> lstNodes = new List<string>();

    foreach (XNode item in xdoc.Descendants("EmpName"))
    {
        lstNodes.Add(item.ToString());
    }

    pivotGridControl1.DataSource = lstNodes;
   // pivotGridControl1.DataContext = lstNodes;

}

我能够在pivotGridControl1的数据源属性中看到数据,但它没有绑定到控件。

有人可以帮助解决这个问题。

先谢谢。

0 个答案:

没有答案