本文介绍了当对象不为null时抛出NullReference的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是wpf的新手,我使用可编辑的comboBox(用于搜索).更改ComboBox中的文本时,下面的datagrid将显示搜索结果.当从数据网格中选择任何行时,其值将显示在文本框中以进行编辑.
当我在组合框中编写内容时,已实现的行显示在数据网格中..但是当我单击以选择一行时,应用程序将引发nullreference异常.
当单击按钮后出现dataGrid刷新逻辑时,我的应用程序正常工作.

dataGrid的"SelectionChange"事件的代码为:

I am new to wpf, i m using a editable comboBox (for search purpose). When text in ComboBox is changed, below datagrid displays the search result. when any row from the datagrid is selected its values are displayed in textboxes for editing.
when i write something in combobox the realated row are displayed in the data grid.. but when i, click to select a row, application throws a nullreference exception.
My application worked correctly when the dataGrid refreshing logic was behind a button click.

the code for "SelectionChange" Event of the dataGrid is:

private void CategoryRowSelected(object sender, System.Windows.Controls.SelectedCellsChangedEventArgs e)
		{
			ClearForm();

          if(CategoryDataGrid.SelectedItem!=null)
           {
           
             categoryMember = CategoryDataGrid.SelectedItem as CategoryTbl; // value assigned to the object
            // if (categoryMember != null)
             CategoryName.Text = categoryMember.CategoryName; //Exception thrown on this statement
             CategoryDescription.Text = categoryMember.CategoryDescription;
    
           } 		
        }
and code for the textChange event of ComboBox is:

private void RefreshDataGrid(object sender, System.Windows.Controls.TextChangedEventArgs e)
        {
            CategoryDataGrid.SelectedIndex = -1;           
            //CategoryDataGrid.ItemsSource = RefreshQuery;
            CategoryDataGrid.ItemsSource= Admin.RefreshCategoryDataGrid(NameCombo.Text);
        }



我希望很快得到答案....



I hope to get an answer sooon....

推荐答案


这篇关于当对象不为null时抛出NullReference的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-28 04:42