我一直在爲此工作了一個小時,但仍然不知道爲什麼在點擊對話框中的「否」按鈕後不刷新列表視圖的原因。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); }
}
感謝您的回答。但我有一個「父」的錯誤說不能默示將類型proj1.frmCompanyEntry轉換爲proj1.frmCompanyList。還在frmCompanyEntry中_entry = new frmCompanyEntry(this);說最好的重載方法匹配有一些無效的參數。 – user2826499
哎呀,我的壞。構造函數中有錯誤。我編輯了一個答案。它必須像這樣'公共frmCompanyEntry(frmCompanyList父母)' – Nino
哇!非常感謝!我已經放棄了這一點。我一直在這個工作近4個小時..非常感謝! – user2826499