2010-07-10 75 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(); 
    }