0

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

加载并contactsListBySchoolIDViewSource创建页面后,我可以将其设置View为 null。加载页面后,如果单击该按钮,则在数据网格中未选择任何内容,应用程序运行良好。如果在选择数据网格中的单元格后单击该按钮,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());
        }
    }

谢谢!!

4

0 回答 0