Click here to Skip to main content
15,887,596 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Hello everyone ,

My sql table has some duplicate item like this :

id-- item_id -- user_name -- basket

1 1 - John - Empty
2 1 - John - order_1
3 2 - Mike - order_2
4 2 - Mike - order_3
5 2 - Mike - order_4
6 3 - Daniel - order_5
7 4 - Simon - order_6
8 4 - Simon - order_7
9 5 - Jane - order_8


when i take this data from sql to gridview , want to delete dublicate items in item id


my gridview should be like this :


id-- item_id -- user_name

1 1 - John
3 2 - Mike
6 3 - Daniel
7 4 - Simon
9 5 - Jane

How can i do this ?

I saw this post :

[^]


But in this post gridview has some empty cells , i don't want to see any empty cells.

Thank you.
Posted
Updated 14-Jul-20 21:25pm
Comments
Sergey Alexandrovich Kryukov 21-Oct-15 15:55pm    
I wonder, why not preventing creation of duplicate rows in first place?
—SA
[no name] 21-Oct-15 15:58pm    
From my Point of view you don't Need to remove duplicates from gridview, you should correct your logic!

The list of the result vs. data in table makes not a lot of sense, at least for me....

a.) Make sure your DB contains data which are useful
b.) Select the correct data from DB

This is not a gridview problem but a sql problem. Either your join is not correct or you have bad data. You'll want to fix it in SQL.
 
Share this answer
 
Comments
[no name] 21-Oct-15 16:48pm    
I agree, a 5.
kozmikadam 22-Oct-15 1:20am    
It's not my db. So we have a lot of data in this db , and i am trying to get this data from there. So I'm looking for a solution. I can't create a new db, i haven't any permissions for this.
ZurdoDev 22-Oct-15 7:05am    
So fix your SELECT statement.
I agree with Zurdodev's answer, change your sql statement to load data/populate the grid with the correct returned data excluding the duplicates by using the SQL SELECT DISTINCT Statement -

SQL
SELECT DISTINCT user_name
  FROM my_table
ORDER BY id
 
Share this answer
 
v2
Comments
CHill60 15-Jul-20 5:00am    
I'm going to counter the 1-vote as you've provided the actual means to fix the SQL statement - I suspect the downvote was because the question is 5 years old
Andre Oosthuizen 15-Jul-20 5:27am    
Oh my gosh, I never saw the date of the post, just the time stamp on the last post as it popped up at the top under unanswered questions, my bad. Thank you very much for the positive return, I should have checked tsk tsk!
Remove All Duplicate Rows from DataGridview
{
if (dataGridView1.Rows.Count >= 0)
{

for (int currentRow = 0; currentRow < dataGridView1.Rows.Count; currentRow++)
{
var rowToCompare = dataGridView1.Rows[currentRow]; // Get row to compare against other rows

// Iterate through all rows
//
foreach (DataGridViewRow row in dataGridView1.Rows)
{
if (rowToCompare.Equals(row))
{
continue;
}
// If row is the same row being compared, skip.

bool duplicateRow = true;

// Compare the value of all cells
if (rowToCompare.Cells[0].Value != null &&
rowToCompare.Cells[0].Value.ToString() != row.Cells[0].Value.ToString())
{
duplicateRow = false;

}


if (duplicateRow)
{
dataGridView1.Rows.RemoveAt(row.Index);
break;
}
}
}
}
}
catch
{
}
 
Share this answer
 
Comments
CHill60 15-Jul-20 4:59am    
Never leave empty catch blocks

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