datagridview中的重复值 [英] Duplicate value in datagridview

查看:107
本文介绍了datagridview中的重复值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

i want to check the duplicate values in the datagridview . and if their is duplicate value then error message to user. if no duplicate value then select query will run . for that i am using following code , but needs some modification for meeting my requirement .....













value in the DGV is entered through Combobox which is being displayed in first column in the DGV .

Thanks in advance .....





我尝试过:





What I have tried:

for (int i = 1; i <= dataGridView1.Rows.Count - 1; i++)
        {
            if (comboBox1.Text == dataGridView1.Rows[i].Cells[0].Value)
            {
                MessageBox.Show("entered value already exist in table");
                return;
            }


        }


                con.Open();
        string sql = "select USERID,PARTY , PARTY_NO,DATE from dgvdata where id ='" + comboBox1.Text + "'";
        System.Data.SqlClient.SqlDataAdapter dataadapter = new System.Data.SqlClient.SqlDataAdapter(sql, con);


        DataTable dt = new DataTable();


        dataadapter.Fill(dt);
         dataGridView1.Rows[index].Cells["USERID"].Value = dt.Rows[0]["USERID"].ToString();
        dataGridView1.Rows[index].Cells["Party"].Value = dt.Rows[0]["Party"].ToString();
        dataGridView1.Rows[index].Cells["Party_No"].Value = dt.Rows[0]["Party_No"].ToString();
        dataGridView1.Rows[index].Cells["Date"].Value = dt.Rows[0]["DATE"].ToString();
                 con.close();

         }

推荐答案

你可以修改代码,并单独调用select和bind。



u can modify the code like and make a separate call for select and bind.

var g = dataGridView1.Rows;
           foreach (GridViewRow item in g)
           {
               if (item.Cells[0].Text.Equals(comboBox1.Text))
               {
               MessageBox.Show("entered value already exist in table");
               return;
               }


           }
 callselectquery(comboBox1.Text);


这篇关于datagridview中的重复值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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