I have an Add Customer UI which has a data grid applied to show the entries provided, but if I re select Add Customer while already showing it will add the entries again.
rows 1-3 are the original and 4-6 are duplicates. Does anyone know how to limit this from happening??
in my MainActivity i have a menu navigation with a Frame to populate the xaml pages. And below is the code to initialize the class.
private void AddCustomer_Click(object sender, RoutedEventArgs e)
{
Main.Content = new AddCustomerUI();
}
The Frame is called Main and then assigning the xaml page to its Main.content
public partial class AddCustomerUI : Page
{
public AddCustomerUI()
{
InitializeComponent();
SetupCustomer();
customerDataGrid.ItemsSource = App.customers;
}
private void SetupCustomer()
{
var customer = new Customer { FirstName = "Timothy", LastName = "Jennings", Email = "tim.jennings@gmail.com", Phone = "0275 202020",
Address = new Address { Street = "100 Burt Road", Suburb = "Howick", City = "Auckland", Country = "New Zealand" }
};
App.customers.Add(customer);
customer = new Customer { FirstName = "Brian", LastName = "Jones", Email = "bjones@gmail.com", Phone = "0275 903070",
Address = new Address { Street = "100 Vincent Road", Suburb = "St Lukes", City = "Auckland", Country = "New Zealand" }
};
App.customers.Add(customer);
App.customers.Add(new Customer { FirstName = "Terry", LastName = "Teo", Email = "tete@mana.com", Phone = "021 756 382",
Address = new Address { Street = "23 Ford St", City = "Auckland", Suburb = "Pakuranga", Country = "New Zealand" }
});
}
private void SaveCustomer_Click(object sender, RoutedEventArgs e)
{
if (string.IsNullOrWhiteSpace(fNameTxt.Text) || string.IsNullOrWhiteSpace(lNameTxt.Text) || string.IsNullOrWhiteSpace(emailTxt.Text)
|| string.IsNullOrWhiteSpace(phTxt.Text))
//MessageBox.Show("Empty Fields", "Please look at filling out all fields on the form.");
return;
var customer = new Customer
{
FirstName = fNameTxt.Text, LastName = lNameTxt.Text, Email = emailTxt.Text, Phone = phTxt.Text,
Address = new Address { Street = streetTxt.Text, Suburb = suburbTxt.Text, City = cityTxt.Text, Country = countryTxt.Text }
};
App.customers.Add(customer);
customerDataGrid.Items.Refresh();
ResetText();
}
private void ResetText()
{
fNameTxt.Text = string.Empty; lNameTxt.Text = string.Empty;
emailTxt.Text = string.Empty; phTxt.Text = string.Empty;
streetTxt.Text = string.Empty; cityTxt.Text = string.Empty;
suburbTxt.Text = string.Empty; countryTxt.Text = string.Empty;
}
}
The issue that you have is in SetupCustomer
, you're not clearling the list so you have multiple entries in your collection.
private void SetupCustomer()
{
App.customers.Clear();
var customer = new Customer { FirstName = "Timothy", LastName = "Jennings", Email = "tim.jennings@gmail.com", Phone = "0275 202020",
Address = new Address { Street = "100 Burt Road", Suburb = "Howick", City = "Auckland", Country = "New Zealand" }
};
App.customers.Add(customer);
customer = new Customer { FirstName = "Brian", LastName = "Jones", Email = "bjones@gmail.com", Phone = "0275 903070",
Address = new Address { Street = "100 Vincent Road", Suburb = "St Lukes", City = "Auckland", Country = "New Zealand" }
};
App.customers.Add(customer);
App.customers.Add(new Customer { FirstName = "Terry", LastName = "Teo", Email = "tete@mana.com", Phone = "021 756 382",
Address = new Address { Street = "23 Ford St", City = "Auckland", Suburb = "Pakuranga", Country = "New Zealand" }
});
}