2012-04-10 25 views
1

我有一個帶有DevExpress XtraGrid控件的主窗體的程序。它有很多來自我的數據庫的數據。我在主窗體上有編輯窗體和編輯按鈕來編輯選定的行。我能夠將所選對象的信息拖入Edit表單中,但由於某種原因,我在運行UPDATE命令時再次遇到問題。我指的是我的主窗體上的選定行爲gridView1.GetFocusedRow(),這對我的showAttributes方法非常合適,但不再有效。在表單之間切換時的空引用

我的代碼如下。它正在返回一個異常,因爲'call'爲空。只是要注意幾件事情:如果我只是編輯第一行,我已經試過做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()也爲空)。

+0

可能的重複[什麼是.NET中的NullReferenceException?](http://stackoverflow.com/questions/4660142/what-is-a-nullreferenceexception-in-net) – 2012-04-10 20:21:15

回答

1

在Edit.cs你做

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

這將創建一個新的主而不顯示它,然後嘗試從一個GridView明顯emtpy得到OBJ。 在顯示的所有代碼中重複出現相同的錯誤。
另外,它對創建和使用CallLog實例非常困惑。

+0

這似乎是問題所在。我在第一行創建了一個不包含任何內容的新表單。但是我無法將Main傳遞給Main的構造函數......您能否使用解決方案引用文章?我試過Google搜索「c#preserve main form」,結果並不多... CallLog是一個帶有屬性的類。我使用'作爲CallLog;'因爲GetFocusedRow()方法返回一個對象,我必須將它轉換爲一個CallLog對象。 – Paul 2012-04-10 23:41:05

+1

如果這是您的應用程序的主要形式,並且您確定只能在應用程序生命週期內實例化主表單的一個實例,則建議您在Program類中添加一個靜態公共屬性,並返回您通過Application.Run方法顯示的主窗體實例。這樣,您就可以隨時使用此靜態屬性訪問主窗體實例。 – Uranus 2012-04-12 02:36:51

+0

在'Application.SetCompatibleTextRenderingDefault(false);'運行之前,無法在Program.cs中創建公共靜態主實例;它會返回一個異常。如果你把它放在後面,但是在App.run之前,它不能公開並且無用。 – Paul 2012-04-12 16:12:47