简体   繁体   English

Null forms之间切换时参考

[英]Null reference when switching between forms

I have a program that has a main form with a DevExpress XtraGrid control.我有一个程序,它的主窗体带有 DevExpress XtraGrid 控件。 It has many rows with data from my DB.它有很多行,其中包含来自我的数据库的数据。 I have an Edit form and an edit button on the main form to edit the selected row.我在主窗体上有一个编辑窗体和一个编辑按钮来编辑选定的行。 I was able to pull the info for the selected object into the Edit form fine, but for some reason I am having trouble doing it again when I run the UPDATE command.我能够将所选 object 的信息很好地拉入编辑表单,但由于某种原因,当我运行更新命令时再次执行此操作时遇到问题。 I'm referring to the selected row on my main form as gridView1.GetFocusedRow() and this worked perfectly for my showAttributes method, but no longer works.我将主窗体上的选定行称为gridView1.GetFocusedRow() ,这对我的 showAttributes 方法非常有效,但不再有效。

My code follows.我的代码如下。 It is returning an exception because 'call' is null. Just to note a few things: I've tried doing main.gridView1.Focus() and main.gridView1.FocusRowHandle(0) if I'm just editing the first row - neither fixed the problem.它正在返回一个异常,因为“呼叫”是 null。请注意以下几点:如果我只是编辑第一行,我已经尝试执行main.gridView1.Focus()main.gridView1.FocusRowHandle(0) -都没有解决问题。 This seems to tell me that the row is still focused correctly but the reference was lost somehow.这似乎告诉我该行仍然正确聚焦,但引用不知何故丢失了。

In Main.cs在 Main.cs 中

    private void btnEdit_Click(object sender, EventArgs e)
    {
        //This is the key, that lets you access an attribute of the selected row. 
        Object obj = gridView1.GetFocusedRow();

        //1) Show NewEdit Form
        Edit edit = new Edit();
        edit.Show();

        //2) Display properties of this object from DB
        edit.showAttributes(obj);
    }

In Edit.cs:在 Edit.cs 中:

    private void btnSubmit_Click(object sender, EventArgs e)
    {
        Main main = new Main();
        Object obj = main.gridView1.GetFocusedRow();
        try
        {
            performUpdate(obj);
        }
        catch (Exception ex)
        {
            MessageBox.Show("Error: " + ex);
        }
    }

    public void performUpdate(Object call1)
    {
        Main main = new Main();
        CallLog call = new CallLog();
        call = call1 as CallLog;

        executeSQLUpdate(call.Oid);
        main.xpServerCollectionSource1.Reload();
    }

Here is the showAttributes code - does the same thing but works这是 showAttributes 代码 - 做同样的事情但有效

    public void showAttributes(Object call1)
    {
        try
        {
            Main main = new Main();
            CallLog call = new CallLog();
            call = call1 as CallLog;

            txtCompany.Text = call.CompanyName;
            txtFirst.Text = call.FirstName;
            txtMiddle.Text = call.MiddleName;
            txtLast.Text = call.LastName;
            txtPhone.Text = call.PhoneNumber;
            txtFax.Text = call.Fax;
            txtEmail.Text = call.Email;
            txtAttention.Text = call.Attention;
            txtCareOf.Text = call.CareOf;
            txtAddress1.Text = call.Address1;
            txtAddress2.Text = call.Address2;
            txtCity.Text = call.City;
            txtState.Text = call.State;
            txtZip.Text = call.ZipCode;
            txtProvince.Text = call.Province;
            txtCountry.Text = call.Country;
            txtMessage.Text = call.Message;
            txtResponse.Text = call.Response;

            if (call.VIP == 1) { chkVIP.Checked = true; } else { chkVIP.Checked = false; }
            if (call.ThreatCall == 1) { chkThreat.Checked = true; } else { chkThreat.Checked = false; }
            if (call.FollowUp == 1) { chkFollowUp.Checked = true; } else { chkFollowUp.Checked = false; }
            if (call.EscalationRequired == 1) { chkEscalation.Checked = true; } else { chkEscalation.Checked = false; }
        }
        catch (Exception ex)
        {
            MessageBox.Show("Error: " + ex);
            return;
        }
    }

Also... I have tried doing this several other ways, without parameters, in different locations, etc. The problem is main.gridView1.GetFocusedRow() returns null. Also, running it as a series of methods from the main form does not work either ( gridView1.GetFocusedRow() is also null).另外...我已经尝试过其他几种方法,没有参数,在不同的位置等。问题是main.gridView1.GetFocusedRow()返回 null。此外,将它作为主窗体中的一系列方法运行不会工作( gridView1.GetFocusedRow()也是空的)。

In Edit.cs you do在 Edit.cs 中你做

    Main main = new Main(); 
    Object obj = main.gridView1.GetFocusedRow(); 

this will create a new Main without displaying it and then try to get an obj from a gridView obviously emtpy.这将创建一个新的 Main 而不显示它,然后尝试从 gridView 显然是空的获取 obj。 The same error is repeated in all the code shown.在显示的所有代码中重复相同的错误。
Also, it's very confused the creation and use of CallLog instances.此外,创建和使用 CallLog 实例非常混乱。

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

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