2010-07-10 99 views
1

在密碼管理器應用程序上工作,我已經知道它將接受來自第二個表單的用戶數據,寫入XML文件,然後在第一個表單上解析xml數據並使用所有數據填充datagridview用戶提供的帳戶信息。目前爲止工作得很好,但是我想用一些功能編寫代碼,以便在用戶添加另一個帳戶時更新當前dataGridview的顯示,而無需再次從組合框中選擇帳戶組。目前只有在用戶第二次選擇帳戶組後纔會更新顯示新添加的帳戶。我怎樣才能改變我的代碼來解決這個問題?代碼如下:如何在更改XML文件時自動更新datagridview?

public partial class Form1 : Form 
    { 
    public string fileName = "passfile.xml"; 
    public DataSet ds = new DataSet("Account List"); 
    public DataTable accounts = new DataTable("Accounts"); 
    public Form1() 
    { 
     InitializeComponent(); 
     accountGroupsBox.Enabled = false; 
     menuStrip1.Enabled = false; 
     button2.Enabled = false; 
     Types type = new Types(); 
     this.accountGroupsBox.Items.AddRange(type.accountTypes); 
     accounts.Columns.AddRange(new DataColumn[] { 
      new DataColumn("Username"), 
      new DataColumn("Password"), 
      new DataColumn("Description")}); 
     dataGridView1.DataSource = accounts; 


    } 

    private void addNewPasswordToolStripMenuItem_Click(object sender, EventArgs e) 
    { 
     Form2 addAccount = new Form2(this); 
     addAccount.Show(); 
    } 

    private void S_Click(object sender, EventArgs e) 
    { 
     accountGroupsBox.Enabled = true; 
     menuStrip1.Enabled = true; 
     button2.Enabled = true; 
     label2.Text = "Interface Unlocked"; 

    } 

    private void accountGroupsBox_SelectedIndexChanged(object sender, EventArgs e) 
    { 
     accounts.Clear(); 
     XmlDocument doc = new XmlDocument(); 
     doc.Load(fileName); 
     foreach (XmlNode node in doc.GetElementsByTagName("Account")) 
     { 
      if (node["AccountType"].InnerText == accountGroupsBox.SelectedItem.ToString()) 
      { 
       DataRow row = accounts.Rows.Add(
       node["Username"].InnerText, 
       node["Password"].InnerText, 
       node["Description"].InnerText); 
      } 
     } 


    } 

這裏是第二種形式的代碼:

public partial class Form2 : Form 
    { 
    Form1 f; 
    public Form2(Form1 fr1) 
    { 
     InitializeComponent(); 
     f = new Form1(); 
     f = fr1; 
     Types types = new Types(); 
     this.comboBox1.Items.AddRange(types.accountTypes); 
    } 

    private void button1_Click(object sender, EventArgs e) 
    { 
     string fileName = "passfile.xml"; 
     XmlDocument file = new XmlDocument(); 
     XmlTextReader read = new XmlTextReader(fileName); 

     if (File.Exists(fileName)) 
     { 
      XmlDocument doc = new XmlDocument(); 
      doc.Load(fileName); 
      XmlElement account = doc.CreateElement("Account"); 
      XmlElement type = doc.CreateElement("AccountType"); 
      type.InnerText = comboBox1.SelectedItem.ToString(); 
      XmlElement userName = doc.CreateElement("Username"); 
      userName.InnerText = textBox1.Text; 
      XmlElement passWord = doc.CreateElement("Password"); 
      passWord.InnerText = textBox2.Text; 
      XmlElement desc = doc.CreateElement("Description"); 
      desc.InnerText = textBox3.Text; 
      account.AppendChild(type); 
      account.AppendChild(userName); 
      account.AppendChild(passWord); 
      account.AppendChild(desc); 
      doc.DocumentElement.InsertAfter(account, doc.DocumentElement.LastChild); 
      doc.Save(fileName); 
      f.dataGridView1.Update(); 
      this.Close(); 

     } 
     else 
     { 
      XmlDocument doc = new XmlDocument(); 
      XmlElement account = doc.CreateElement("Account"); 
      XmlElement type = doc.CreateElement("AccountType"); 
      type.InnerText = comboBox1.SelectedItem.ToString(); 
      XmlElement userName = doc.CreateElement("Username"); 
      userName.InnerText = textBox1.Text; 
      XmlElement passWord = doc.CreateElement("Password"); 
      passWord.InnerText = textBox2.Text; 
      XmlElement desc = doc.CreateElement("Description"); 
      desc.InnerText = textBox3.Text; 
      account.AppendChild(type); 
      account.AppendChild(userName); 
      account.AppendChild(passWord); 
      account.AppendChild(desc); 
      doc.AppendChild(account); 
      doc.Save(fileName); 
      this.Close(); 

     } 
    } 
} 

}

+0

要觸發從更新哪裏? – Marko 2010-07-10 03:04:39

+0

我想要第二個窗體上的按鈕來觸發更新。在用戶添加另一個帳戶後的IE瀏覽器中,如果主窗體上選定的帳戶類型與剛剛創建的帳戶類型相同,則顯示帳戶的dataGridView將自動反映添加到該組的新帳戶。 – Stev0 2010-07-10 18:28:17

回答

0

最好的解決辦法,因爲我看到它樹立了一個文件系統觀察和聆聽其事件:

這裏是一個關於如何設置它的示例代碼(將它添加到窗體設計視圖後)

private void SetFileWatcher() 
    { 

     fileSystemWatcher1.Path = System.IO.Path.GetDirectoryName(fileName); 
     fileSystemWatcher1.Filter = System.IO.Path.GetFileName(fileName); 
     fileSystemWatcher1.EnableRaisingEvents = true; 
    } 

,這是對事件的示例,您可以添加到等待文件更改:

void fileSystemWatcher1_Changed(object sender, System.IO.FileSystemEventArgs e) 
    { 
     // for your code example, you can write the function here 
     button1.PerformClick(); 
    } 
相關問題