Click here to Skip to main content
15,615,280 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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");


        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();

         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();

Updated 17-Apr-17 21:05pm
Karthik_Mahalingam 18-Apr-17 9:44am    
can you post a screenshot of your grid?

1 solution

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

Share this answer
Member 12741312 18-Apr-17 5:38am    
I think ,this is same of that I posted above . I want both will run but first duplicate check and then select query.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900