Working on a password manager application and I've gotten it to the point where it will accept user data from a second form, write to an XML file, then on the first form parse through the xml data and populate a datagridview with all the user supplied account information. Working great so far, but I'd like to code in some functionality that updates the display of the current dataGridview if the user adds another account, without having to select the account group from the combo box again. Currently it only updates to show the newly added account after the user selects the account group a second time. How can I change my code to fix this? Code is as follows:
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);
}
}
}
And here is the code for the second form:
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();
}
}
}
}