【问题标题】:DataGridView should not acccept duplicate ID with first and last nameDataGridView 不应接受具有名字和姓氏的重复 ID
【发布时间】:2021-01-21 12:10:31
【问题描述】:

这是我的代码不应该接受 DataGridView 中的重复行,但在我的代码中,它是例外:

不应接受重复行THis is my datagridview it's not taking duplicate ID's but taking remaining fields to duplicate it should not duplicate filename, Lastname, and profession also

I need output like this

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";
            }
        }
    }
}

【问题讨论】:

  • 看图片...我不明白你为什么要删除 2 GHK SRYU DHGHJ ...和... 4 RTY RYY1 OIO?如果新 ID 与表 (table.Select("Id = " + idValue)).Length == 0 中的现有 ID 相同,则代码正在“检查”......并且没有一个 ID 相同! (1,2,3,4)……所有的ID都不一样,为什么不加呢?如果您说名称相同,那么代码不应该也检查相同的名称吗?正如我在您之前的问题中所描述的那样,您将不得不弄清楚一些“特殊”处理。
  • 要求是这样的,不仅ID保持列值也是唯一的,它不应该重复。请帮我解决这个问题
  • 在对 ID 进行检查后,您似乎需要对名字和姓氏添加检查。如果ID是重复的,那么名字是什么都没关系……不要添加。如果ID不重复,则需要检查名字和姓氏,如果发现重复名称则不要添加,否则添加新的id和名称。
  • 我是新手,你能帮我做吗?
  • 在 ID 检查之后添加一个额外的检查......类似于...... (table.Select("FirstName = " + fisrtTxt.Text.Trim() + "AND LastName = " + SurNameTxt.Text.Trim()) ).Length == 0

标签: c# winforms datagridview


【解决方案1】:

按这样的列区分表

table = table
    .AsEnumerable()
    .GroupBy(r => new 
    { 
        FirstName = r.Field<string>("FirstName"), 
        LastName = r.Field<string>("LastName"), 
        Profession = r.Field<string>("Profession") 
    })
    .Select(group => group.First())
    .CopyToDataTable();

【讨论】:

  • 你能试试这个吗? table = table.AsEnumerable().GroupBy(r =&gt; new { FirstName = r.Field&lt;string&gt;("FirstName"), LastName = r.Field&lt;string&gt;("LastName"), Profession = r.Field&lt;string&gt;("Profession") }).Select(group =&gt; group.First()).CopyToDataTable();
【解决方案2】:

我不确定这有什么困难。使用两个if 语句,而不是一个复合if 语句...If ID 不重复,if 名字和姓氏不重复然后添加行。您需要记住,如果 ID 是重复的,则无需检查名称。

if ((table.Select("Id = " + idValue)).Length == 0) {
  if ((table.Select("FirstName = '" + firstTxt.Text.Trim() +
                    "' AND LastName = '" + SurNameTxt.Text.Trim() + "'")).Length == 0) {
     // add  the row 
  }
}

Note the single quote " ' " before/after the text box values.

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-06-30
    • 2016-02-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多