尝试将DataGrid VeiwSource.View设置为null时出现错误消息

时间:2013-09-26 21:38:51

标签: c# wpf datagrid

我正在尝试设置绑定数据网格,以便不选择任何内容。

加载页面并创建contactsListBySchoolIDViewSource后,我可以将其设置为View为空。 加载页面后,如果单击按钮,应用程序运行正常,则在数据网格中未选择任何内容。 如果在选择了数据网格中的单元格后单击按钮,则try..catch不会捕获错误,但我收到一条消息框,指出:   "索引超出了数组的范围。" 如果我点击“确定”'在消息框上,我想在数据网格中没有选择任何内容。

' contactsViewSource'并没有给我任何问题。

我正在使用VS 2012,WPF,C#和.nrt 4.5。

我的问题是如何阻止消息框显示

<Page.Resources >
    <!-- Bound to a "details view" -->
    <CollectionViewSource x:Key="contactsViewSource" Source="{Binding Contacts, Source={StaticResource mTAdminDataSet}}"/>

<!-- Bound to a datagrid -->
    <CollectionViewSource x:Key="contactsListBySchoolIDViewSource" Source="{Binding ContactsListBySchoolID, Source={StaticResource mTAdminDataSet}}"/>

</Page.Resources>


    private void Page_Loaded(object sender, RoutedEventArgs e)
    {
        try
        {

    MTAdmin.MTAdminDataSetTableAdapters.ContactsListBySchoolIDTableAdapter mTAdminDataSetContactsListBySchoolIDTableAdapter = new MTAdmin.MTAdminDataSetTableAdapters.ContactsListBySchoolIDTableAdapter();

    mTAdminDataSetContactsListBySchoolIDTableAdapter.Fill(mTAdminDataSet.ContactsListBySchoolID, schoolId);
    contactsListBySchoolIDViewSource = ((System.Windows.Data.CollectionViewSource)(this.FindResource("contactsListBySchoolIDViewSource")));
    contactsListBySchoolIDViewSource.View.MoveCurrentTo(null);


    MTAdmin.MTAdminDataSetTableAdapters.ContactsListBySchoolIDTableAdapter mTAdminDataSetContactsListBySchoolIDTableAdapter = new MTAdmin.MTAdminDataSetTableAdapters.ContactsListBySchoolIDTableAdapter();
    mTAdminDataSetContactsListBySchoolIDTableAdapter.Fill(mTAdminDataSet.ContactsListBySchoolID, schoolId);
    contactsListBySchoolIDViewSource = ((System.Windows.Data.CollectionViewSource)(this.FindResource("contactsListBySchoolIDViewSource")));
    contactsListBySchoolIDViewSource.View.MoveCurrentTo(null);

    //other code.....

        }
        catch (Exception ex)
        {
            MessageBox.Show(ex.Message);
        }

}

    private void contactsButton_Click(object sender, RoutedEventArgs e)
    {
        try
        {
    //bound to grid with details(textboxes)
            contactsViewSource.View.MoveCurrentTo(null);

    //bound to the datagrid

 // THIS IS THE LINE OF CODE THROWING THE ERROR 
            contactsListBySchoolIDViewSource.View.MoveCurrentTo(null);
        }
        catch (Exception ex)
        {
            MessageBox.Show(ex.ToString());
        }
    }

谢谢!

0 个答案:

没有答案