---
title: "Compare value of datagrid with value of selected from database"  
description: "Compare value of datagrid with value of selected from database"  
author: "Demir ACAR"  
published: 2014-05-24  
updated: 2014-05-27  
canonical: https://www.mindstick.com/forum/2021/compare-value-of-datagrid-with-value-of-selected-from-database  
category: "c#"  
tags: ["c#"]  
reading_time: 2 minutes  

---

# Compare value of datagrid with value of selected from database

```
  for (int i = 0; i < dataGridView1.RowCount-1; i++)                if (radioButton1.Checked == true)                {                    string strConn = @"Data Source='" + dbconfig.tb1 + "';Initial Catalog='" + dbconfig.tb2 + "';user='" + dbconfig.tb3 + "';pwd='" + dbconfig.tb4 + "'";                    SqlConnection conn = new SqlConnection(strConn);                    conn.Open();                    SqlCommand cmd = new SqlCommand("SELECT CODE  FROM LG_" + dbconfig.tb5 + "_table", conn);                    cmd.ExecuteNonQuery();                    SqlDataReader reader = cmd.ExecuteReader();                    while (reader.Read())                    {                        if (reader["CODE"].ToString() != dataGridView1.Rows[i++].Cells[2].Value.ToString())                        {                            MessageBox.Show(dataGridView1.Rows[i].Cells[2].Value.ToString());                        }                                        }                    conn.Close();            }        }
```

## Replies

### Reply by Demir ACAR

I fill to datagrid with excel and want to check data in datagrid and sql value,

For example , My Grid Results

GRID_DATA DB_DATA

ABD GERMANY

GERMANY ABD

ITALY UKRAINE

So I want to catch ITALY due to does not exists database.

Thanks in advance

```
if(radioButton1.Checked == true)            {for(inti = 0; i < dataGridView1.Rows.Count; i++)      {stringstrConn = @"Data Source='"+ dbconfig.tb1 + "';Initial Catalog='"+ dbconfig.tb2 + "';user='"+ dbconfig.tb3 + "';pwd='"+ dbconfig.tb4 + "'";SqlConnectionconn = newSqlConnection(strConn);conn.Open();SqlCommandcmd = newSqlCommand("SELECT CODE  FROM TABLE where CODE='"+ dataGridView1.Rows[i].Cells[2].Value.ToString() + "'", conn);SqlDataReaderreader = cmd.ExecuteReader();while(reader.Read()){varsonuc = dataGridView1.Rows[i].Cells[2].Value;MessageBox.Show("Total"+ dataGridView1.RowCount.ToString() + " Unit Records"+ "\n"+ "\n"+ "mismatched"+ sonuc, "Result", MessageBoxButtons.OK, MessageBoxIcon.Information);   }                    conn.Close();                    reader.Close();                    }                                    }
```

\

Note : I fill to grid with excel , no datatables or others.

### Reply by Manmohan Jha

Mr. Birol\
\
Please elaborate your question...


---

Original Source: https://www.mindstick.com/forum/2021/compare-value-of-datagrid-with-value-of-selected-from-database

Copyright © MindStick Software Pvt. Ltd. This Markdown version is provided for developers, AI systems, and offline reading.
