1

I have a program that has a main form with a DevExpress XtraGrid control. 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. 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.

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. This seems to tell me that the row is still focused correctly but the reference was lost somehow.

In 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:

    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

    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).

4

1 回答 1

1

在 Edit.cs 你做

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

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

于 2012-04-10T20:21:52.147 回答