Hide Duplicate Field Values in a GridView

https://www.experts-exchange.com/questions/25136887/Hide-duplicate-field-values-in-a-GridView.html

anyone knows this in C# / .nets / .aspx?

I have an appointment table, one appointment slots consists of 2 groups.
Each group can only have a maximum no. of 10 people.
I want to show the appointment date, time and group number once,
and the names of the people in the group will be shown.

e.g.

Date    Time    Group       Name
------------------------------------------------
xx         xx          1                Katy
                                             John
                                             Sam

xx         xx           2               James
                                             Rick
                                             Mike
Anon nYmousAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

it_saigeDeveloperCommented:
Personally I would use linq and group the items; e.g. -

Form1.cs -
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Windows.Forms;

namespace EE_Q29076788
{
    public partial class Form1 : Form
    {
        readonly IEnumerable<dynamic> appointments = (from i in Enumerable.Range(0, 20) select new { ID = i, Name = $"Person{i}", Group = (i % 2) == 0 ? 1 : 2, Date = new DateTime(2018, 1, 5), Time = new DateTime(2018, 1, 5, 8, (i % 2 == 0) ? 0 : 30, 0) });

        public Form1()
		{
			InitializeComponent();
		}

        private void OnLoad(object sender, EventArgs e)
        {
            dataGridView1.DataSource = (from appointment in appointments
                                        group appointment by new { appointment.Date, appointment.Time, appointment.Group }
                                        into groups
                                        select new
                                        {
                                            Date = groups.FirstOrDefault().Date.ToString("MM/dd/yyyy"),
                                            Time = groups.FirstOrDefault().Time.ToString("hh:mm:ss tt"),
                                            Group = groups.FirstOrDefault().Group,
                                            Name = string.Join(Environment.NewLine, (from item in groups select item.Name))
                                        }).ToList();
        }
    }
}

Open in new window

Form1.Designer.cs -
namespace EE_Q29076788
{
	partial class Form1
	{
		/// <summary>
		/// Required designer variable.
		/// </summary>
		private System.ComponentModel.IContainer components = null;

		/// <summary>
		/// Clean up any resources being used.
		/// </summary>
		/// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>
		protected override void Dispose(bool disposing)
		{
			if (disposing && (components != null))
			{
				components.Dispose();
			}
			base.Dispose(disposing);
		}

		#region Windows Form Designer generated code

		/// <summary>
		/// Required method for Designer support - do not modify
		/// the contents of this method with the code editor.
		/// </summary>
		private void InitializeComponent()
		{
            this.dataGridView1 = new System.Windows.Forms.DataGridView();
            ((System.ComponentModel.ISupportInitialize)(this.dataGridView1)).BeginInit();
            this.SuspendLayout();
            // 
            // dataGridView1
            // 
            this.dataGridView1.AllowUserToAddRows = false;
            this.dataGridView1.AllowUserToDeleteRows = false;
            this.dataGridView1.AutoSizeColumnsMode = System.Windows.Forms.DataGridViewAutoSizeColumnsMode.Fill;
            this.dataGridView1.AutoSizeRowsMode = System.Windows.Forms.DataGridViewAutoSizeRowsMode.AllCells;
            this.dataGridView1.ColumnHeadersHeightSizeMode = System.Windows.Forms.DataGridViewColumnHeadersHeightSizeMode.AutoSize;
            this.dataGridView1.Dock = System.Windows.Forms.DockStyle.Fill;
            this.dataGridView1.Location = new System.Drawing.Point(0, 0);
            this.dataGridView1.Name = "dataGridView1";
            this.dataGridView1.ReadOnly = true;
            this.dataGridView1.RowHeadersVisible = false;
            this.dataGridView1.SelectionMode = System.Windows.Forms.DataGridViewSelectionMode.FullRowSelect;
            this.dataGridView1.Size = new System.Drawing.Size(611, 366);
            this.dataGridView1.TabIndex = 0;
            // 
            // Form1
            // 
            this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
            this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
            this.ClientSize = new System.Drawing.Size(611, 366);
            this.Controls.Add(this.dataGridView1);
            this.Name = "Form1";
            this.Text = "Form1";
            this.Load += new System.EventHandler(this.OnLoad);
            ((System.ComponentModel.ISupportInitialize)(this.dataGridView1)).EndInit();
            this.ResumeLayout(false);

		}

        #endregion

        private System.Windows.Forms.DataGridView dataGridView1;
    }
}

Open in new window

Which produces the following output -Capture.PNG
-saige-
Dustin SaundersDirector of OperationsCommented:
Or, you could just have the data pull in your select results (table with Date,Time,Group, and Name columns):
SELECT t1.[date],t1.[time],t1.[group],members = STUFF((
	SELECT ',' + t2.[name]
		FROM ee29076788 t2
		WHERE t2.[group] = t1.[group]
		AND t2.[date] = t1.[date]
		AND t2.[time] = t1.[time]
		FOR XML PATH('')),1,1,'')
FROM ee29076788 t1
GROUP BY t1.[date],t1.[time],t1.[group]

Open in new window


Table:
1518_table1.png
Result:
1518_table2.png
Anon nYmousAuthor Commented:
Are there other methods?
I need it to be in separate roles
CompTIA Security+

Learn the essential functions of CompTIA Security+, which establishes the core knowledge required of any cybersecurity role and leads professionals into intermediate-level cybersecurity jobs.

Dustin SaundersDirector of OperationsCommented:
Can you clarify what you mean by 'separate roles'?
Anon nYmousAuthor Commented:
This is what i mean:
example.PNG
Dustin SaundersDirector of OperationsCommented:
Well, so it's better to include that information on the initial post so you get a better result the first time (you've added new conditions).

What is IC?  What is Gender?

Am I understanding that you don't now want each name grouped into a single row?
Anon nYmousAuthor Commented:
Yes, I do not want each name to be grouped into a single role.

Gender in my sql is char (1), F or M. (female or male)

IC is a unique id for each person
Dustin SaundersDirector of OperationsCommented:
Well, quick and dirty you can just 'clean up' your DGV bottom up:
        private void FormatView(DataGridView dgv)
        {
            for (int i = dgv.Rows.Count - 1; i > 0; i--)
            {
                if (dgv.Rows[i].Cells[0].Value == dgv.Rows[i -1].Cells[0].Value && dgv.Rows[i].Cells[1].Value == dgv.Rows[i - 1].Cells[1].Value)
                {
                    dgv.Rows[i].Cells[0].Value = "";
                    dgv.Rows[i].Cells[1].Value = "";
                }
                if (dgv.Rows[i].Cells[2].Value == dgv.Rows[i - 1].Cells[2].Value && dgv.Rows[i].Cells[1].Value.ToString() == "")
                    dgv.Rows[i].Cells[2].Value = "";
            }
        }

Open in new window


Test Code:
        private void FormatView(DataGridView dgv)
        {
            for (int i = dgv.Rows.Count - 1; i > 0; i--)
            {
                if (dgv.Rows[i].Cells[0].Value == dgv.Rows[i -1].Cells[0].Value && dgv.Rows[i].Cells[1].Value == dgv.Rows[i - 1].Cells[1].Value)
                {
                    dgv.Rows[i].Cells[0].Value = "";
                    dgv.Rows[i].Cells[1].Value = "";
                }
                if (dgv.Rows[i].Cells[2].Value == dgv.Rows[i - 1].Cells[2].Value && dgv.Rows[i].Cells[1].Value.ToString() == "")
                    dgv.Rows[i].Cells[2].Value = "";
            }
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            DataTable example = new DataTable();

            string[] columns = { "Date", "Time", "Group", "Name", "IC", "Gender" };

            for (int i = 0; i < columns.Length; i++)
            {
                example.Columns.Add(new DataColumn { ColumnName = columns[i] });
            }

            string[,] rows = {
                {"1-Jan-18","1pm","1","abc","1","F" },
                {"1-Jan-18","1pm","1","cde","2","F" },
                {"2-Jan-18","1pm","1","efg","3","F" },
                {"2-Jan-18","1pm","1","ghi","4","M" },
                {"2-Jan-18","1pm","2","ijk","5","F" },
                {"2-Jan-18","3pm","1","klm","6","M" },
                {"2-Jan-18","3pm","2","mno","7","M" }
            };

            for (int i = 0; i < rows.GetLength(0); i++)
            {
                DataRow row = example.NewRow();
                for (int j = 0; j < rows.GetLength(1); j++)
                {
                    row[j] = rows[i, j];
                }
                example.Rows.Add(row);
            }

            dataGridView1.DataSource = example;
            FormatView(dataGridView1);
        }

Open in new window


Result:
dgv.png
Depending on your requirements, that could be made dynamic, but it shows one way to handle this.

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Anon nYmousAuthor Commented:
Thank you!
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
C#

From novice to tech pro — start learning today.