NEWS

[C#] Tìm giá trị trùng lắp và tô màu trên datagridview winform

[C#] Tìm giá trị trùng lắp và tô màu trên datagridview winform
Đăng bởi: Thảo Meo - Lượt xem: 7648 12:45:38, 09/12/2021PHẦN MỀM

Xin chào các bạn, bài viết hôm nay mình sẻ tiếp tục hướng dẫn các bạn cách tìm giá trị trùng lắp trên DataGridview C# Winform.

[C#] Find Duplicate and High Light Color in DataGridview Winform

Mình có một đoạn dữ liệu như hình bên dưới:

duplicate_value_csharp

Ở hình trên, các bạn thấy có dòng dữ liệu mình trùng giá trị, bây giờ mình muốn tìm giá trị trùng và tô màu.

Bài này mình sẻ kiểm tra trùng 2 giá trị: tên nhân viêntuổi mới tô màu.

Kết quả:

find_duplicate

Như kết quả hình trên các bạn thấy, có một bạn "Nguyễn Đình Tuyên" nhưng tuổi = 30, nên nó không tô màu.

Source code Find Duplicate Value Datagridview c#:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace HighlightDuplicateValueGridview
{
    public partial class Form1 : Form
    {
        public readonly Color color_duplicate = Color.Yellow;
        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            var table = new DataTable();
            table.Columns.Add("id", typeof(int));
            table.Columns.Add("name", typeof(string));
            table.Columns.Add("age", typeof(string));
            table.Rows.Add(1, "Nguyễn Thảo", 18);
            table.Rows.Add(2, "Nguyễn Đình Tuyên", 30);
            table.Rows.Add(3, "Nguyễn Phương Nhi", 18);
            table.Rows.Add(4, "Hoàng Thị Thảo", 18);
            table.Rows.Add(5, "Trịnh Quốc Khang", 18);
            table.Rows.Add(6, "Cái Trí Minh", 18);
            table.Rows.Add(7, "Võ Sơn Băng", 18);
            table.Rows.Add(8, "Cao Hoàng Thiên Bảo", 18);
            table.Rows.Add(9, "Nguyễn Thảo", 18);
            table.Rows.Add(10, "Nguyễn Đình Tuyên", 20);
            table.Rows.Add(11, "Nguyễn Đình Tuyên", 20);
            table.Rows.Add(12, "Nguyễn Thị Cẩm Tú", 30);
            dataGridView1.DataSource = table;
        }

        private void FindDuplicates(DataGridView dataGridView)
        {
            for (int currentRow = 0; currentRow < dataGridView.Rows.Count - 1; currentRow++)
            {

                string name = dataGridView.Rows[currentRow].Cells[1].Value.ToString();
                int age = Convert.ToInt32(dataGridView.Rows[currentRow].Cells[2].Value);
                for (int row = 0; row < dataGridView.Rows.Count - 1; row++)
                {
                    string nameCompare = dataGridView.Rows[row].Cells[1].Value.ToString();
                    int ageCompare = Convert.ToInt32(dataGridView.Rows[row].Cells[2].Value);
                    if (currentRow != row)
                    {
                        if (name == nameCompare && age == ageCompare)
                        {
                            dataGridView.Rows[currentRow].DefaultCellStyle.BackColor = color_duplicate;
                            break;
                        }
                        else
                        {
                            dataGridView.Rows[currentRow].DefaultCellStyle.BackColor = Color.White;
                        }
                    }
                }
            }
        }

        private void btn_find_Click(object sender, EventArgs e)
        {
            FindDuplicates(dataGridView1);
        }
    }
}

Thanks for watching!

DOWNLOAD SOURCE

Tags: duplicate datagridviewfind duplicate datagridview c#

THÔNG TIN TÁC GIẢ

BÀI VIẾT LIÊN QUAN

[C#] Tìm giá trị trùng lắp và tô màu trên datagridview winform
Đăng bởi: Thảo Meo - Lượt xem: 7648 12:45:38, 09/12/2021PHẦN MỀM