首页 > 解决方案 > C# 不应该接受具有名字和姓氏的重复 ID

问题描述

这是我的代码不应该接受 DataGridView 中的重复行

这是我的数据网格视图,它不采用重复的 ID,但采用剩余的字段来复制它不应该重复文件名、姓氏和职业

![多行 DGV 图像][1]

我需要这样的输出

![所需 DGV 的图像][2]

代码:

namespace Bind_DataGridView_Using_DataTable {
    public partial class Bind_DataGridView_Using_DataTable : Form {
        public Bind_DataGridView_Using_DataTable() {
            InitializeComponent();
        }
        DataTable table = new DataTable();
        private void BtnAdd_Click(object sender, EventArgs e) {

            string idText = IDTxt.Text.Trim();
            if (!string.IsNullOrEmpty(idText) && int.TryParse(idText, out int idValue)) {
                if ((table.Select("Id = " + idValue)).Length == 0) {
                    table.Rows.Add(idValue, fisrtTxt.Text.Trim(), SurNameTxt.Text.Trim(), ProfesTxt.Text.Trim());
                    //table.AcceptChanges();
                    dataGridView1.DataSource = table;
                    cleatTxts();
                }
                else {
                    MessageBox.Show("Person Id already Exist");
                }
            }
            else {
                label1.Text = "Person Id should not be empty && must be a valid int value";
            }
        }
    }
}

标签: c#

解决方案


就像您在代码中通过 ID 进行搜索一样,您可以在表格中搜索现有的姓氏和名字。类似于 table.Select("FirstName = ? AND LastName = ?")。


推荐阅读