2016-05-09 195 views
0

我一直在爲此工作了一個小時,但仍然不知道爲什麼在點擊對話框中的「否」按鈕後不刷新列表視圖的原因。C#刷新列表視圖

我有frmCompanyList(主窗體)和位於這裏的列表視圖接下來我有frmCompanyEntry(childform)。

我已經試過使用這個Refresh(),它不工作。但如果我點擊我的frmCompanyList中的「刷新」按鈕它的工作。

因此,這裏是我的代碼:

frmCompanyEntry:

DialogResult dialogResult = MessageBox.Show(Global._strInsertAgainMsg + "company?", Global._strTitleMsg, MessageBoxButtons.YesNo); 
if (dialogResult == DialogResult.Yes) 
{ 
    clearAll(); 
} 
else if (dialogResult == DialogResult.No) 
{ 
    this.Close(); 
    clearAll(); 

    frmCompanyList _company = new frmCompanyList(); 
    _company.PerformRefresh(); 
} 

那麼這裏的frmCompanyList:

public void PerformRefresh() 
{ 
    __toolCmbStatus.SelectedIndex = 1; 
    loadCompany(__toolTxtSearch.Text); 
} 

public void loadCompany(string _strSearch) 
{ 
    try 
    { 
     ListViewItem Item; 
     __lvwCompany.Items.Clear(); 

     string _strWhereStatement = "(fldCode LIKE '" + "%" + _strSearch + "%" + "' OR fldCompany LIKE '" + "%" + _strSearch + "%" + "' OR fldAddress LIKE '" + "%" + _strSearch + "%" + "' OR fldContactNo LIKE '" + "%" + _strSearch + "%" + "' OR fldContactPerson LIKE '" + "%" + _strSearch + "%" + "')"; 

     if (__toolCmbStatus.SelectedIndex.ToString() != "2") 
     { _strQry = "SELECT * FROM tblCompany WHERE " + _strWhereStatement + " AND fldActive = '" + __toolCmbStatus.SelectedIndex.ToString() + "' ORDER BY fldCompany ASC"; } 
     else 
     { _strQry = "SELECT * FROM tblCompany WHERE " + _strWhereStatement + " ORDER BY fldCompany ASC"; } 

     using (SQLConnect.SqlCommandEx _SQLCMD = new SQLConnect.SqlCommandEx(_strQry)) 
     { 
      DataTable dt = _SQLCMD.GetDataTable(); 
      __lblTotalRecord.Text = dt.Rows.Count.ToString(); 

      if (dt.Rows.Count == 0) 
      { 
       Item = new ListViewItem(""); 
       Item.SubItems.Add(Global._strEmptyMsg); 
       Item.SubItems[0].ForeColor = System.Drawing.Color.Red; 
       __lvwCompany.Items.Add(Item); 
      } 
      else 
      { 
       foreach (DataRow DR in dt.Rows) 
       { 
        Item = new ListViewItem(DR[0].ToString()); 
        Item.SubItems.Add(DR[2].ToString()); 
        Item.SubItems.Add(DR[3].ToString()); 
        Item.SubItems.Add(DR[4].ToString()); 
        Item.SubItems.Add(DR[5].ToString()); 
        Item.SubItems.Add(DR[1].ToString()); 

        if (DR[6].ToString() == "False") 
        { 
         Item.SubItems.Add("Inactive"); 
         Item.SubItems[6].ForeColor = System.Drawing.Color.Red; 

         for (int x = 0; x <= 6; x++) 
         { 
          Item.SubItems[x].Font = new Font(__lvwCompany.Font, FontStyle.Italic); 
         } 
         Item.UseItemStyleForSubItems = false; 
        } 
        else 
        { 
         Item.SubItems.Add("Active"); 
         Item.SubItems[6].ForeColor = System.Drawing.Color.Green; 
         Item.UseItemStyleForSubItems = false; 
        } 
        __lvwCompany.Items.Add(Item); 
       } 
      } 
     } 
    } 
    catch (Exception ex) { MessageBox.Show("Please contact your administrator. Error: " + ex, Global._strTitleMsg); } 
} 

回答

1

問題是與線。

frmCompanyList _company = new frmCompanyList(); 

你正在實例化窗體,調用方法加載數據,但實際上不顯示窗體。如果刷新後調用_company.Show(),則會看到刷新的列表(但是具有主窗體的另一個實例)

而不是重複的,您希望以已打開的格式刷新數據。在這種情況下,您必須將frmCompanyList(父表單)的引用傳遞給子表單(frmCompanyEntry)。你可以通過構造函數或某些屬性來實現。

在你frmCompanyEntry形式,增加財產

frmCompanyList _company = null; 

修改frmCompanyEnty構造這樣

public frmCompanyEntry(frmCompanyList parent) 
{ 
    this._company = parent; 
} 

,並從打開frmCompanyEntry時,從父,修改如下:

frmCompanyEntry _entry = new frmCompanyEntry(this); 
_entry.ShowDialog(); 

通過這種方式,您可以將引用傳遞給子表單,因此您可以refr在你的代碼中將其引發。 現有的代碼應該是那麼像這樣(請注意,該行frmCompanyList _company = new frmCompanyList()丟失):

DialogResult dialogResult = MessageBox.Show(Global._strInsertAgainMsg + "company?", Global._strTitleMsg, MessageBoxButtons.YesNo); 
if (dialogResult == DialogResult.Yes) 
{ 
    clearAll(); 
} 
else if (dialogResult == DialogResult.No) 
{ 
    this.Close(); 
    clearAll(); 

    _company.PerformRefresh(); 
} 
+0

感謝您的回答。但我有一個「父」的錯誤說不能默示將類型proj1.frmCompanyEntry轉換爲proj1.frmCompanyList。還在frmCompanyEntry中_entry = new frmCompanyEntry(this);說最好的重載方法匹配有一些無效的參數。 – user2826499

+0

哎呀,我的壞。構造函數中有錯誤。我編輯了一個答案。它必須像這樣'公共frmCompanyEntry(frmCompanyList父母)' – Nino

+0

哇!非常感謝!我已經放棄了這一點。我一直在這個工作近4個小時..非常感謝! – user2826499