14 Replies - 8741 Views - Last Post: 15 August 2012 - 01:15 PM Rate Topic: -----

#1 handsup  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 7
  • Joined: 09-August 12

[How To] Remove duplicates, Copy rows from dataGridView

Posted 09 August 2012 - 12:07 PM

I have some questions about dataGridView control

A- How can I paste from clipboard to datagridview
B- How can I copy selected row to clipboard
C- How can I remove rows which not starts with "http://"
D- How can I remove dupliate rows

I had 10 problems like that, I figured out 6 of them.
I did much research about these 4 issues, but I can't find any useful information.
Finally I realized that; dataGridView is the most hard to use control in C# visual studio.

Help me please. I really need help urgently. Thanks.

Is This A Good Question/Topic? 0
  • +

Replies To: [How To] Remove duplicates, Copy rows from dataGridView

#2 modi123_1  Icon User is online

  • Suitor #2
  • member icon



Reputation: 9571
  • View blog
  • Posts: 36,243
  • Joined: 12-June 08

Re: [How To] Remove duplicates, Copy rows from dataGridView

Posted 09 August 2012 - 12:24 PM

Well... what have you tried, or reasoning for how to do each of those?
Was This Post Helpful? 0
  • +
  • -

#3 Skydiver  Icon User is offline

  • Code herder
  • member icon

Reputation: 3662
  • View blog
  • Posts: 11,466
  • Joined: 05-May 12

Re: [How To] Remove duplicates, Copy rows from dataGridView

Posted 09 August 2012 - 01:05 PM

If have had tried to create simple DataGridView, you would have discovered that A and B are built into the DataGridView control and work right out of the box.

But then, I could also be lying to you. :devil2: You'll need to put together some code to find out.

As for filtering the rows for particular data and duplicates. The DataGridView will display whatever you give it in the DataSource property. It's up to you what that data will be.
Was This Post Helpful? 0
  • +
  • -

#4 CasiOo  Icon User is online

  • D.I.C Lover
  • member icon

Reputation: 1423
  • View blog
  • Posts: 3,165
  • Joined: 05-April 11

Re: [How To] Remove duplicates, Copy rows from dataGridView

Posted 09 August 2012 - 01:57 PM

For D you should take a look at the HashSet class :)
Was This Post Helpful? 0
  • +
  • -

#5 Skydiver  Icon User is offline

  • Code herder
  • member icon

Reputation: 3662
  • View blog
  • Posts: 11,466
  • Joined: 05-May 12

Re: [How To] Remove duplicates, Copy rows from dataGridView

Posted 09 August 2012 - 02:44 PM

View PostCasiOo, on 09 August 2012 - 01:57 PM, said:

For D you should take a look at the HashSet class :)


Good suggestion! Unfortunately, the class doesn't implement IList, IListSource, IBindingList, IBindingListView or any other interface that the DataGridView's DataSource needs to display data.

We're not completely hosed, though, one could easily create a class that implements an IListSource and that contains a HashSet. (And no, setting DataSource = hashSet.ToList() is not a solution unless the DataGridView is set to be read only.)
Was This Post Helpful? 0
  • +
  • -

#6 CasiOo  Icon User is online

  • D.I.C Lover
  • member icon

Reputation: 1423
  • View blog
  • Posts: 3,165
  • Joined: 05-April 11

Re: [How To] Remove duplicates, Copy rows from dataGridView

Posted 09 August 2012 - 03:02 PM

Why would you not consider DataSource = hashSet.ToList() a solution?

Edit: Oh now I see :D

This post has been edited by CasiOo: 09 August 2012 - 03:08 PM

Was This Post Helpful? 0
  • +
  • -

#7 handsup  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 7
  • Joined: 09-August 12

Re: [How To] Remove duplicates, Copy rows from dataGridView

Posted 09 August 2012 - 06:19 PM

I can't understand you all really :( is there no specific code to do this?

I tried some codes like this:

          List<string> list = new List<string>();
            for (int i = 0; i < dataGridView1.Rows.Count; i++ )
            {
                string str = dataGridView1.Rows[i].Cells[0].Value.ToString();
                if (!list.Contains(str))
                    list.Add(dataGridView1.Rows[i].Cells[0].Value.ToString());
                else
                {
                    dataGridView1.Rows.Remove(dataGridView1.Rows[i]);
                }

            }


but it didnt work

This post has been edited by JackOfAllTrades: 14 August 2012 - 04:48 PM
Reason for edit:: Added code tags

Was This Post Helpful? 0
  • +
  • -

#8 Skydiver  Icon User is offline

  • Code herder
  • member icon

Reputation: 3662
  • View blog
  • Posts: 11,466
  • Joined: 05-May 12

Re: [How To] Remove duplicates, Copy rows from dataGridView

Posted 09 August 2012 - 06:33 PM

What was not working? Did you step through your code and see the Remove() calls happening?
Was This Post Helpful? 1
  • +
  • -

#9 handsup  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 7
  • Joined: 09-August 12

Re: [How To] Remove duplicates, Copy rows from dataGridView

Posted 14 August 2012 - 02:19 PM

Hi...

My new method to paste from clipboard to datagridview is:
                StreamWriter sw = new StreamWriter(@"Config/paste");
                string t = Clipboard.GetText();
                sw.Write(t);
                sw.Close();
                StreamReader sr = new StreamReader(@"Config/paste");
                dataGridView1.Rows.Clear();
                while (sr.Peek() >= 0)
                {
                    DataGridViewRow row = dataGridView1.Rows[dataGridView1.Rows.Add()];
                    row.Cells["Column1"].Value = sr.ReadLine();
                }
                sr.Close();
                File.Delete(@"Config/paste");


It works great and TOOO fast for me. But I wonder that what do you guys think of my new method? Is this a good method? Thanks
Was This Post Helpful? 0
  • +
  • -

#10 Skydiver  Icon User is offline

  • Code herder
  • member icon

Reputation: 3662
  • View blog
  • Posts: 11,466
  • Joined: 05-May 12

Re: [How To] Remove duplicates, Copy rows from dataGridView

Posted 14 August 2012 - 02:29 PM

Is that intentional that paste will destroy the current data and start adding rows and putting all the data into the first column? What if the original selected text that was put into the clipboard was multiple columns and didn't start at the top left corner of the grid?
Was This Post Helpful? 0
  • +
  • -

#11 handsup  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 7
  • Joined: 09-August 12

Re: [How To] Remove duplicates, Copy rows from dataGridView

Posted 14 August 2012 - 02:48 PM

don't worry it is intentional.
Was This Post Helpful? 0
  • +
  • -

#12 handsup  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 7
  • Joined: 09-August 12

Re: [How To] Remove duplicates, Copy rows from dataGridView

Posted 14 August 2012 - 03:36 PM

hey guys I still cant remove duplicate rows from datagridview :( give me some codes to do this please :(
Was This Post Helpful? 0
  • +
  • -

#13 handsup  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 7
  • Joined: 09-August 12

Re: [How To] Remove duplicates, Copy rows from dataGridView

Posted 14 August 2012 - 04:21 PM

I solved remove duplicates problem now man. it was very easy too..
Was This Post Helpful? 0
  • +
  • -

#14 Skydiver  Icon User is offline

  • Code herder
  • member icon

Reputation: 3662
  • View blog
  • Posts: 11,466
  • Joined: 05-May 12

Re: [How To] Remove duplicates, Copy rows from dataGridView

Posted 14 August 2012 - 04:32 PM

Good for you! Did you stick with your use of the List or did you choose something else?
Was This Post Helpful? 1
  • +
  • -

#15 handsup  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 7
  • Joined: 09-August 12

Re: [How To] Remove duplicates, Copy rows from dataGridView

Posted 15 August 2012 - 01:15 PM

I used list control for that!
List<string> list = new List<string>();
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1