将数据绑定到Silverlight DataGrid时出现问题

问题描述:


我正在开发一个使用WCF服务与数据库进行交互的Silverlight Web应用程序.我将数组列表从DAL层返回到Xaml.cs,但无法将其绑定到Datagrid.
我的代码部分如下:

Hi
I am developing a Silverlight web application that uses WCF services to interact with database. I return a arraylist from DAL layer to Xaml.cs but could not bind it to Datagrid.
My Code section is as follows:

if (e.Error != null)
{
    //Error Condition
}
else
{
    dataGrid1.ItemsSource = e.Result;
}



如果我使用datagrid1.ItemSource = e.Result; ..
,则数据未绑定 请在这方面提出您的宝贵建议.

谢谢
Naresh.



The data is not getting binded if I use datagrid1.ItemSource = e.Result;..
Please give your valuable suggestions in this regard.

Thanks
Naresh.

e.result包含什么?是收藏吗?

注意:Silverlight 不支持数组列表,您需要使用ObservableCollection .

任何IEnumerable都可用作项源,而不仅仅是ObservableCollection.
What does e.result contain? Is it a collection?

Note: Arraylists are not supported in Silverlight and you need to use an ObservableCollection.

Any IEnumerable can be used as an itemssource, not just ObservableCollection.


您需要启用AutoGenerateColumns
You need to Either enable AutoGenerateColumns
dataGrid1.AutoGenerateColumns = true;



或者,您可以添加列并指定绑定



OR you can Add column and specify binding

<sdk:DataGrid AutoGenerateColumns="False" Height="185" HorizontalAlignment="Left" Margin="33,66,0,0" Name="dataGrid1" VerticalAlignment="Top" Width="325">
            <sdk:DataGrid.Columns>
                <sdk:DataGridTextColumn Binding="{Binding}" CanUserReorder="True" CanUserResize="True" CanUserSort="True" Header="Test" Width="Auto" />
            </sdk:DataGrid.Columns>
        </sdk:DataGrid>