If you want to return a list of distinct values from a DataGridView in C#, you can follow this approach:

// DataGridView is named dataGridView1
List<string> distinctValues = new List<string>();

foreach (DataGridViewRow row in dataGridView1.Rows)
{
    string value = row.Cells["ColumnName"].Value.ToString();
    if (!distinctValues.Contains(value))
    {
        distinctValues.Add(value);
    }
}

Above code example iterates through each row in the DataGridView and extracts the value from a specific column (replace "ColumnName" with the actual name of the column of you table). It then checks if the value is already present in the distinctValues list. If not, it adds the value to the list.

Lets' take a real-time example. Suppose we have a list of employees that is bound to a DataGridView. Now, I want to return a list of distinct employees from the DataGridView based on their email IDs.

To return a list of distinct employee emails from a DataGridView in C#, we can achieve this with the following code:

// DataGridView is named dataGridViewEmployees
List<string> distinctEmployeeEmails = new List<string>();

foreach (DataGridViewRow row in dataGridViewEmployees.Rows)
{
    string employeeEmail = row.Cells["EmailIdColumn"].Value.ToString();
    if (!distinctEmployeeEmails.Contains(employeeEmail))
    {
        distinctEmployeeEmails.Add(employeeEmail);
    }
}

Above code iterates through each row in the DataGridView and extracts the employee email from the EmailId column (replace "EmailIdColumn" with the actual name of the column containing employee emails). It then checks if the employee email is already present in the distinctEmployeeEmails list. If not, it adds the employee email to the list.


// Extracting distinct values from a specific column in a DataGridView
// using LINQ query method syntax.

// Declare a variable to store the distinct values.
var distinctValues = dataGridView1.Rows.Cast()
                           .Select(x => x.Cells["EmployeeEmail"].Value?.ToString()) // Selecting values from the specified column.
                           .Where(x => !string.IsNullOrEmpty(x)) // Filtering out null or empty values.
                           .Distinct() // Getting distinct values.
                           .ToList(); // Converting the result to a List.

// Explanation:
// 1. dataGridView1.Rows.Cast(): Casts the DataGridView rows into a sequence of DataGridViewRow objects.
// 2. Select(x => x.Cells["EmployeeEmail"].Value?.ToString()): Selects the value of the specified column for each row. 
//    The ?. operator is used to handle null values, avoiding potential NullReferenceExceptions.
// 3. Where(x => !string.IsNullOrEmpty(x)): Filters out any null or empty values to ensure only valid values are considered.
// 4. Distinct(): Retrieves distinct values from the sequence.
// 5. ToList(): Converts the result into a List.

// The resulting List 'distinctValues' contains the distinct values from the specified column in the DataGridView.

Count distinct values of a column in dataGridView using linq

If you want to count the distinct values of a column in a DataGridView using LINQ in .NET, you can follow these steps:

  1. Access the DataGridView and its rows.
  2. Extract the values from the desired column.
  3. Filter out any null or empty values.
  4. Get the distinct values.
  5. Count the distinct values.

Here's how you can achieve this using LINQ:

//  DataGridView is named dataGridView1
var distinctValuesCount = dataGridView1.Rows.Cast<DataGridViewRow>()
                                .Select(row => row.Cells["ColumnName"].Value?.ToString())
                                .Where(value => !string.IsNullOrEmpty(value))
                                .Distinct()
                                .Count();

This code example retrieves the distinct count of values from the specified column in the DataGridView. Replace "ColumnName" with the actual name of the column containing the values.