Home Upgrade Search Memberlist Extras Hacker Tools Award Goals Help Wiki Contact

.NET 

DataGridView coding help?

Hi everyone,
Say I have a DataGridView1 with some names and gender. Some of the names are duplicates.
How can I remove and keep the count of the names into a new, or the current datagridview? I'll attach a picture to show what I mean

[Image: ZrUArJC.png]

So DataGridView1 is on the left, with duplicate names.
I would like to remove the duplicates and count how many total, like the GridView on the right?
(Dec 28, 2018 02:09 PM)Ajumma Wrote:  Hi everyone,
Say I have a DataGridView1 with some names and gender. Some of the names are duplicates.
How can I remove and keep the count of the names into a new, or the current datagridview? I'll attach a picture to show what I mean

[Image: ZrUArJC.png]

So DataGridView1 is on the left, with duplicate names.
I would like to remove the duplicates and count how many total, like the GridView on the right?

You can use the linq to group rows duplicates by cell value ,and you will get what you want.Then you can put linq result in datatable and send to second datagridview or you can pass linq result directly to second datagridview.

https://docs.microsoft.com/en-us/dotnet/...using-linq
buy me a coffee eth:0x88621A52aa5Eb3b6503628369687111810186c58
(Dec 28, 2018 03:04 PM)itcorp Wrote:  You can use the linq to group rows duplicates by cell value ,and you will get what you want.Then you can put linq result in datatable and send to second datagridview or you can pass linq result directly to second datagridview.

https://docs.microsoft.com/en-us/dotnet/...using-linq

I think it's possible to do without LinQ, but I'm just having a hard time finishing the code to remove the items in GridView1 after counting it.
Only if you have a list that you're updating that you bind to the datagridview, otherwise you're using Linq for the data from the first list to get the count. Dictionary's do not bind directly either.

Code
using System;
using System.Collections;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Linq;
using System.Windows.Forms;

namespace WindowsFormsApp1
{
    public enum Gender { Male, Female }

    public class Person
    {
        public string Name { get; set; }
        public Gender Gender { get; set; }
    }

    public class PersonList : List<Person>, IListSource
    {
        public bool ContainsListCollection => true;
        public IList GetList() => this;
        public IList GetCountList() =>
            (from p in this
             group p by p.Name into g
             select new { Name = g.Key, Count = g.Count() }).ToList();
    }

    public partial class Form1 : Form
    {
        public Form1() => InitializeComponent();

        private readonly PersonList _people = new PersonList();

        private void Form1_Load(object sender, EventArgs e)
        {
            _people.Add(new Person() { Name = "Bob", Gender = Gender.Male });
            _people.Add(new Person() { Name = "Taylor", Gender = Gender.Male });
            _people.Add(new Person() { Name = "Taylor", Gender = Gender.Female });

            dataGridView1.DataSource = _people;
            dataGridView2.DataSource = _people.GetCountList();
        }
    }
}

[Image: MlN9dKc.png]

Then depending on whether you're allowing the datagridview to be edited expecting 2-way binding, things get a little more tricky. You may want to implement IRaiseItemChangedEvents, IBindingList as well, in which case it might be better to implement IList as well instead of inheriting from List<Person> to broadcast changes to the list for each.
Getting Started: C/C++
[C/C++/.NET/x86 ASM - Automation Programmer]
If the source will came from a database, you can use query and use GROUP BY.
[Image: signaturetwo.png]
https://stackoverflow.com/questions/3975...tagridview


https://stackoverflow.com/questions/1089...inq-in-net
you can use count statement like this in your query
================
SELECT name, count
FROM Persons
GROUP BY Name;
================

hope this helps
if you can poste your code so we can help you.


Possibly Related Threads...
Thread / Author Replies Views Last Post
[C#] by Nemo2478Jul 25, 2020 12:01 PM 1 179 Jul 25, 2020 12:01 PM
Last Post: glitchgxd
[.NET] by Takehito MorokuzuJun 9, 2019 03:51 PM 2 220 Jun 9, 2019 03:51 PM
Last Post: Hawk HF
[.NET] by monemdsNov 17, 2017 10:46 AM 2 215 Nov 17, 2017 10:46 AM
Last Post: monemds