如何避免在C#中重复输入数据到datagrid中 [英] How to avoid duplicate entery into datagrid in C#

查看:148
本文介绍了如何避免在C#中重复输入数据到datagrid中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

亲爱的,

我要将数据从文本框中输入到datagrid中,我想避免重复输入到datagrid中.任何及时的帮助将不胜感激.

我正在Windows应用程序中工作.

最好的问候,
Aftab

Hi Dear,

i am entring data into datagrid from textbox i want to avoid duplicate entry into datagrid. any prompt help will be appreciated.

I am working in windows application.

Best Regards,
Aftab

推荐答案

如果可能的话,请尝试在保存该数据的查询或集合/数据源中添加一些逻辑-可能会更容易.
If possible, try to put some logic in your query or collection / datasource that holds this data - it might be easier.


用数据表绑定数据网格,并使该列成为唯一/主键.这将在表中创建一个约束,这将阻止它添加重复的值

请参阅msdn中唯一密钥的示例代码

DataTable custTable = custDS.Tables ["Customers"];
UniqueConstraint custUnique =新的UniqueConstraint(新的DataColumn []
{custTable.Columns ["CustomerID"],
custTable.Columns ["CompanyName"]});
custDS.Tables ["Customers"].Constraints.Add(custUnique);
Bind the datagrid with a data table and make the column as unique / primary key. This will create a constraint in the table which will prevent it from adding duplicate values

see sample code for unique key from msdn

DataTable custTable = custDS.Tables["Customers"];
UniqueConstraint custUnique = new UniqueConstraint(new DataColumn[]
{custTable.Columns["CustomerID"],
custTable.Columns["CompanyName"]});
custDS.Tables["Customers"].Constraints.Add(custUnique);


这篇关于如何避免在C#中重复输入数据到datagrid中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆