Binding Datasource View to Datagrid in WPF

守給你的承諾、 提交于 2019-12-25 08:49:55

问题


I am trying to get my data to show in a DataGrid. I am using SQL Server 2012 and Visual Studio 2010 and working with a WPF application.

I created a new datasource, I chose a "View" that I created in SQL Server. I selected the dropdown on that View in the data pane. I chose the dropdown and clicked on DataGrid. Then I dragged it to a user control. When I run the application, the headers show but the result set does not. When I run the view in SQL server it returns a result set. What am I doing wrong?

This is the XAML in the User Control

    <UserControl.Resources>
    <CollectionViewSource x:Key="myviewsViewSource" d:DesignSource="{d:DesignInstance my:myview, CreateList=True}" />
</UserControl.Resources>
<Grid DataContext="{StaticResource myviewsViewSource}">
    <DataGrid AutoGenerateColumns="False" EnableRowVirtualization="True" Height="200" HorizontalAlignment="Left" ItemsSource="{Binding}" Margin="561,121,0,0" Name="myviewsDataGrid" RowDetailsVisibilityMode="VisibleWhenSelected" VerticalAlignment="Top" Width="400">
        <DataGrid.Columns>
            <DataGridTextColumn x:Name="mnemonicColumn" Binding="{Binding Path=Mnemonic}" Header="Mnemonic" Width="SizeToHeader" />
            <DataGridTextColumn x:Name="nameColumn" Binding="{Binding Path=Name}" Header="Name" Width="SizeToHeader" />
            <DataGridTextColumn x:Name="toolColumn" Binding="{Binding Path=Tool}" Header="Tool" Width="SizeToHeader" />
            <DataGridTextColumn x:Name="filterColumn" Binding="{Binding Path=Filter}" Header="Filter" Width="SizeToHeader" />
            <DataGridTemplateColumn x:Name="createdColumn" Header="Created" Width="SizeToHeader">
                <DataGridTemplateColumn.CellTemplate>
                    <DataTemplate>
                        <DatePicker SelectedDate="{Binding Path=Created, Mode=TwoWay, ValidatesOnExceptions=true, NotifyOnValidationError=true}" />
                    </DataTemplate>
                </DataGridTemplateColumn.CellTemplate>
            </DataGridTemplateColumn>
            <DataGridTextColumn x:Name="typeColumn" Binding="{Binding Path=Type}" Header="Type" Width="SizeToHeader" />
        </DataGrid.Columns>
    </DataGrid>
</Grid>        

Here is my C# code:

    public partial class ScanControl : UserControl
{
    public ScanControl()
    {
        InitializeComponent();
    }       

    private void UserControl_Loaded_1(object sender, RoutedEventArgs e)
    {
        if (!System.ComponentModel.DesignerProperties.GetIsInDesignMode(this))
        {
            //Load your data here and assign the result to the CollectionViewSource.
            System.Windows.Data.CollectionViewSource myCollectionViewSource = (System.Windows.Data.CollectionViewSource)this.Resources["myviewsViewSource"];
        }
    }
}        

回答1:


You are missing a few things in order to data bind.

1) Make sure you have set your DataContext to DataContext="{Binding RelativeSource={RelativeSource Self}}"

2) You need to change the ItemsSource binding in your DataGrid to: ItemsSource="{Binding SomePropertyName}"

3) You need to implement `INotifyPropertyChanged'

4) To implement this property use the following:

    #region INotifyPorpertyChanged Memebers 

    public event PropertyChangedEventHandler PropertyChanged;

    protected void NotifyPropertyChanged(string propertyName = "")
    {
        if (PropertyChanged != null)
            PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
    }

    #endregion

5) Make an property of type ObservableCollection of the type of object the the DB is returning. This property is the property the DataGrid is bounded to.

ex:

private ObservableCollection<SomeDataType> _myPrivateData;
public ObservableCollection<SomeDataType> SomePropertyName { get { return _myPrivateData; } set    { _myPrivateData= value; NotifyPropertyChanged("SomePropertyName"); } }

That takes care of the data binding part. Now every time you reset the collection that DataGrid is bound to your DataGrid will update because the NotifyPropertyChanged is called.



来源:https://stackoverflow.com/questions/26871254/binding-datasource-view-to-datagrid-in-wpf

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!