简体   繁体   English

在数据网格之间切换时为空指针

[英]Null pointer when switching between data grids

In a program I'm writing, there are two data grids on wpf form. 在我正在编写的程序中,wpf表单上有两个数据网格。 One contains pet owners and the other contains the actual pets. 一个包含宠物主人,另一个包含实际的宠物。 When the user clicks on an owner, the pet data grid is populated with pets they own through a SQL database. 当用户单击所有者时,宠物数据网格将通过SQL数据库填充他们拥有的宠物。 The currently selected owner ID is saved to a variable. 当前选择的所有者ID将保存到变量中。 The same is done when the user clicks on a pet, the pet ID is saved to a variable. 用户单击宠物时,将执行同样的操作,将宠物ID保存到变量中。

Now, the problem I'm having is when an owners pet is selected. 现在,我遇到的问题是当选择主人宠物时。 When clicking a different owner I get a null pointer exception and I'm not entirely sure why.' 单击其他所有者时,出现空指针异常,但我不确定原因。

Below is some of the code. 以下是一些代码。 It crashes on the PetDg_SelectionChanged method. 它在PetDg_SelectionChanged方法上崩溃。 Any help would be appreciated. 任何帮助,将不胜感激。 Thanks! 谢谢!

public partial class UpdateWindowNew : Window
{
    int currentOwnerPk;
    int currentPetPk;

    public UpdateWindowNew()
    {
        InitializeComponent();
    }

    public void Window_Loaded(object sender, RoutedEventArgs e)
    {
        string conString = ConfigurationManager.ConnectionStrings["conString"].ConnectionString;
        string cmdString = "empty";

        using (SqlConnection con = new SqlConnection(conString))
        {
            cmdString = "Select * from Owner";
            SqlCommand query = new SqlCommand(cmdString, con);
            SqlDataAdapter gridAdapter = new SqlDataAdapter(query);
            DataTable dt = new DataTable("Owner");
            gridAdapter.Fill(dt);
            ownerDg.ItemsSource = dt.DefaultView;
        }
    }

    private void ownerDG_SelectionChanged(object sender, SelectionChangedEventArgs e)
    {
        currentOwnerPk = (int)(ownerDg.SelectedItem as DataRowView).Row.ItemArray[0];
        OwnerFname.Text = (string)(ownerDg.SelectedItem as DataRowView).Row.ItemArray[1];
        OwnerLname.Text = (string)(ownerDg.SelectedItem as DataRowView).Row.ItemArray[2];

        showOwnedPets(currentOwnerPk);
    }

    private void PetDg_SelectionChanged(object sender, SelectionChangedEventArgs e)
    {
        currentPetPk = (int)(PetDg.SelectedItem as DataRowView).Row.ItemArray[0];
    }

Its very simple, because when you select another owner, pet DataGrid get empty and fire this exception, you need to check for null value for selected item in PetDg_SelectionChanged event handler: 这非常简单,因为当您选择另一个所有者时,pet DataGrid变空并引发此异常,因此您需要在PetDg_SelectionChanged事件处理程序中检查所选项目的空值:

currentPetPk = PetDg.SelectedItem is DataRowView item ?
    (int)item.Row.ItemArray[0] : -1;

SelectedItem is probably null. SelectedItem可能为null。 You should check for it, with the Null-conditional Operator '?.'. 您应该使用空条件运算符'?。'进行检查。

currentOwnerPk = (int)(ownerDg?.SelectedItem as DataRowView)?.Row.ItemArray[0] ?? -1;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM