c# - 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";
}
}
}
}
解决方案
就像您在代码中通过 ID 进行搜索一样,您可以在表格中搜索现有的姓氏和名字。类似于 table.Select("FirstName = ? AND LastName = ?")。
推荐阅读
- python - 尝试在 Python 中使用 bcp 将 CSV 上传到数据库时出现问题
- performance - 如何在 ssrs 报告中显示大量数据
- c - SD 在分区或磁盘级别进行磨损均衡工作
- c++ - 在哪里可以找到对通用 c++ 模板参数的要求?
- string - 在 Pyspark 中将缺失的字符串值转换为 isNull
- node.js - MongoDB 向 NodeJS 中的现有用户插入数据
- sql - 动态 sql - 使用循环更新多行
- excel - Office js 多页加载项导航
- generics - Rust:缩短泛型类型界限
- windows - 来自 docker-compose 的 Docker 容器无法再连接到主机