在表单之间切换时为空引用

时间:2012-04-10 20:15:06

标签: c# gridview null devexpress xtragrid

我有一个程序,它有一个带有DevExpress XtraGrid控件的主窗体。它有很多行包含来自我的数据库的数据。我在主窗体上有一个编辑表单和一个编辑按钮来编辑选定的行。我能够将所选对象的信息拉到编辑表单中,但由于某种原因,我在运行UPDATE命令时无法再次执行此操作。我将主表单上的选定行称为gridView1.GetFocusedRow(),这对我的showAttributes方法非常有效,但不再有效。

我的代码如下。它返回一个异常,因为'call'为null。只是要注意一些事情:如果我只是编辑第一行,我已经尝试过main.gridView1.Focus()main.gridView1.FocusRowHandle(0) - 既没有解决问题。这似乎告诉我该行仍然正确聚焦,但参考文献以某种方式丢失了。

在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);
    }

在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();
    }

这是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;
        }
    }

...还 我已经尝试过这几种方式,没有参数,在不同的位置,等等。问题是main.gridView1.GetFocusedRow()返回null。此外,从主窗体中将其作为一系列方法运行也不起作用(gridView1.GetFocusedRow()也为空)。

1 个答案:

答案 0 :(得分:1)

在Edit.cs中你做

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

这将创建一个新的Main而不显示它,然后尝试从gridView获取obj显然是emtpy。 在所有显示的代码中重复相同的错误 此外,它对CallLog实例的创建和使用非常困惑。