如何在不添加相同数据的情况下添加多个数据

时间:2016-11-12 04:47:26

标签: c#

如何在不添加相同数据的情况下添加多个数据? 我试图纠正它。

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);



        }

    }
}

如何在不添加相同数据的情况下添加多个数据?

1 个答案:

答案 0 :(得分: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);
}