TECHNOLOGIES
FORUMS
JOBS
BOOKS
EVENTS
INTERVIEWS
Live
MORE
LEARN
Training
CAREER
MEMBERS
VIDEOS
NEWS
BLOGS
Sign Up
Login
No unread comment.
View All Comments
No unread message.
View All Messages
No unread notification.
View All Notifications
Answers
Post
An Article
A Blog
A News
A Video
An EBook
An Interview Question
Ask Question
Forums
Monthly Leaders
Forum guidelines
bludz
NA
1
9.4k
Checking for duplicate values in datagridview against another datagridview
Oct 25 2012 3:58 AM
Hi,
I have a C# application which connects to a SQL db.
On my new form I have three datagridview's.
dgv1 = populated by click event
dgv2 = populated when form opens via sql command
dgv3 = populated when form opens via sql command
I need to check if the value in cell 1 on dgv1 if there is a duplicate cell value in cell 2 on dgv2. (For each row of dgv1 against each row of dgv2)
If there is a duplicate value in cell 2 on dgv2, I need to get the value of cell 1 (which is the ID for that record) and add it to a list.
This list is to be used later to update a sql command, (which adds the ID of the duplicate record) then updates that particular record in the SQL db.
Here is the code I have composed so far, but it is not counting correctly.
I have been using the counter for testing to check if it is actually finding any duplicates. I have been entering at least one record in dgv1, that I know is in the database, but after the method has finished, it displays that it has not found any duplicates. Could some one help me finish this coding or atleast advise where I am going wrong here?
public void btnCompareData_Click(object sender, EventArgs e)
{
if (!CheckedRows.Any())
{
// no labels to print; show error
MessageBox.Show("No labels selected.", "Empty selection", MessageBoxButtons.OK, MessageBoxIcon.Warning);
return;
}
else
{
DialogResult dr2 = MessageBox.Show("Are you sure you want to compare duplicate data.", "Compare", MessageBoxButtons.YesNo, MessageBoxIcon.Information);
if (dr2 == DialogResult.Yes)
{
List<string> duplicateRecords = new List<string>();
int dupCount = 0;
foreach (DataGridViewRow row in dgv1.Rows)
{
foreach (DataGridViewRow row2 in dgv2.Rows)
{
if (row.Cells["col1"].Value == row2.Cells["col2"].Value)
{
duplicateRecords.Add(row2.Cells["ID"].Value.ToString());
dupCount++;
}
}
}
MessageBox.Show("Completed.\r\rDuplicate records updated: " + dupCount.ToString(), "Compare", MessageBoxButtons.OK, MessageBoxIcon.Information);
}
if (dr2 == DialogResult.No)
{
//cancel message, do nothing
MessageBox.Show("Compare cancelled", "Compare", MessageBoxButtons.OK, MessageBoxIcon.Information);
}
}
}
//method to change the id in the sql command to update the record with the same ID
public void updateRecords(string ID)
{
//get the ID, then add the id to the SQL command to update the record with that ID in the database
string updateSQLRecord = "UPDATE tblAddress SET matchedDate = GETDATE() WHERE ID = '" + ID + "'";
//now pass the sql string to update the duplicate records.(not finished here yet)
}
When the method has finished, even if I have manually entered a value in dgv1 that I know is in dgv2, the counter still displays 0.
Please help :)
Reply
Answers (
0
)
Need PrintForm in VB.NET
Tif To txt Convert