Hack Forums
[.NET] DataGridView coding help? - Printable Version

+- Hack Forums (https://hackforums.net)
+-- Forum: Tech (https://hackforums.net/forumdisplay.php?fid=444)
+--- Forum: Coding (https://hackforums.net/forumdisplay.php?fid=151)
+---- Forum: Software Development (https://hackforums.net/forumdisplay.php?fid=118)
+---- Thread: [.NET] DataGridView coding help? (/showthread.php?tid=5923619)



DataGridView coding help? - Ajumma - Dec 28, 2018

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?


RE: DataGridView coding help? - itcorp - Dec 28, 2018

(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/visual-basic/programming-guide/language-features/linq/how-to-count-sum-or-average-data-by-using-linq


RE: DataGridView coding help? - Ajumma - Dec 28, 2018

(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/visual-basic/programming-guide/language-features/linq/how-to-count-sum-or-average-data-by-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.


RE: DataGridView coding help? - 0xDEAD10CC - Dec 28, 2018

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.


RE: DataGridView coding help? - Vyxn - Dec 28, 2018

If the source will came from a database, you can use query and use GROUP BY.


RE: DataGridView coding help? - aneeshkollam5 - Dec 29, 2018

https://stackoverflow.com/questions/39756443/return-a-list-of-distinct-values-from-datagridview


https://stackoverflow.com/questions/10899568/count-distinct-values-of-a-column-in-datagridview-using-linq-in-net


RE: DataGridView coding help? - Fanus47213 - Dec 29, 2018

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.