Member 12741312 Ответов: 1

Дубликат значения в 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 .....


Что я уже пробовал:

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

         }

Karthik_Mahalingam

можете ли вы опубликовать скриншот своей сетки?
imgur.com

1 Ответов

Рейтинг:
1

madhav_jain

вы можете модифицировать код, как и сделать отдельный вызов для выбора и привязки.

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


Member 12741312

Я думаю, это то же самое, что я написал выше . Я хочу, чтобы оба они запускались, но сначала дублировали проверку, а затем выбирали запрос.