1

How can add multiple data without add same data? I am tried to correct it.

public partial class SalaryCalculator : Form
{

    public SalaryCalculator()
    {
        InitializeComponent();
    }


    public Double basicAmount, houseRent, medicalAllowance, totalHouseRent, totalMedicalAllowance, totalSalary;
    public int id = 0;

    private void addButton_Click(object sender, EventArgs e)
    {

        ListViewItem newList = new ListViewItem((++id).ToString());


        string employeeName = nameTextBox.Text;

        double basicAmount = Convert.ToDouble(basicTextBox.Text);
        double houseRent = Convert.ToDouble(houseRentTextBox.Text);
        double medicalAllowance = Convert.ToDouble(medicalTextBox.Text);


        totalHouseRent = (basicAmount*houseRent)/100;
        totalMedicalAllowance = (basicAmount*medicalAllowance)/100;
        totalSalary = basicAmount + totalHouseRent + totalMedicalAllowance;
        totalTextBox.Text = totalSalary.ToString();


        newList.SubItems.Add(employeeName);
        newList.SubItems.Add(totalSalary.ToString());
        listView1.Items.Add(newList);



        }

    }
}

How can add multiple data without add same data?

Masoud
  • 8,020
  • 12
  • 62
  • 123
Masum
  • 1,037
  • 15
  • 32

1 Answers1

0

You can check if the listView1 contain the key you wish to add:

if (!listView1.Items.ContainsKey(employeeName) && !listView1.Items.ContainsKey(totalSalary.ToString()))
{
    newList.SubItems.Add(employeeName);
    newList.SubItems.Add(totalSalary.ToString());
    listView1.Items.Add(newList);
}
Yanga
  • 2,885
  • 1
  • 29
  • 32