如何在不添加相同数据的情况下添加多个数据
本文关键字:添加 数据 情况下 | 更新日期: 2023-09-27 18:17:45
如何添加多个数据而不添加相同的数据?我正在努力纠正它。
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);
}
}
}
如何添加多个数据而不添加相同的数据?
您可以检查listView1是否包含您希望添加的键:
if (!listView1.Items.ContainsKey(employeeName) && !listView1.Items.ContainsKey(totalSalary.ToString()))
{
newList.SubItems.Add(employeeName);
newList.SubItems.Add(totalSalary.ToString());
listView1.Items.Add(newList);
}