how to count the same name in datagridview c#

Anonymous
2023-04-17T12:45:48.18+00:00

Good day everyone, I would like to ask some help from you. User's image

Base to this image, They have a CARS__AVAILABLE in datagridview. Now, I want to count all CARS__AVAILABLE and display in Vehicle Available panel at the top. I have a code but it's not work, Please help me. here my code. User's image

Developer technologies C#
{count} votes

Accepted answer
  1. Anonymous
    2023-04-18T13:24:13.8166667+00:00

    Hi @Anonymous , Welcome to Microsoft Q&A, you could try the following code to get what you wanted.

    int badCount = dataGridView2.
    				Rows.Cast<DataGridViewRow>()
                    .Where(row => row.Cells["Car_Status"].Value.ToString() == "CARS_AVAILABLE")
                    .Count();
    

    User's image

    Best Regards,

    Jiale


    If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment". 

    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.


1 additional answer

Sort by: Most helpful
  1. Karen Payne MVP 35,586 Reputation points Volunteer Moderator
    2023-04-19T20:01:17.41+00:00

    If using a DataTable (and perhaps a BindingSource) consider getting statistics from the DataTable rather than the DataGridView. Perks

    • React to things like adding a new row or changing a column value etc
    • Location of the data does not matter column-wise

    In the example below you can easily change the header text for columns. C1

    using System.Data;
    
    namespace WinFormsApp1;
    
    public partial class Form1 : Form
    {
        private readonly BindingSource _bindingSource = new ();
        public Form1()
        {
            InitializeComponent();
    
            var table = LoadDataTable();
            
            table.ColumnChanged += TableOnColumnChanged;
            table.RowDeleted += Table_RowDeleted;
            table.RowChanged += Table_RowChanged;
            
            _bindingSource.DataSource = table;
            dataGridView1.DataSource = _bindingSource;
    
            PerformCalculations();
        }
    
        private void Table_RowChanged(object sender, DataRowChangeEventArgs e)
        {
            if (e.Action == DataRowAction.Add)
            {
                PerformCalculations();
            }
        }
    
        private void Table_RowDeleted(object sender, DataRowChangeEventArgs e)
        {
            PerformCalculations();
        }
    
        private void TableOnColumnChanged(object sender, DataColumnChangeEventArgs e)
        {
            if (e.Column!.ColumnName == "CarStatus")
            {
                PerformCalculations();
            }
            
        }
    
        public static DataTable LoadDataTable()
        {
            DataTable dataTable = new ("Vechicle");
    
            dataTable.Columns.Add("ID", typeof(int));
            dataTable.Columns["Id"]!.ColumnMapping = MappingType.Hidden;
            dataTable.Columns.Add("CarId", typeof(string));
            dataTable.Columns.Add("CarStatus", typeof(string));
    
            dataTable.Rows.Add(1, "MN84", "CARS_AVAILABLE");
            dataTable.Rows.Add(2, "LW4C", "CARS_AVAILABLE");
            dataTable.Rows.Add(3, "1VAV", "RENT");
            dataTable.Rows.Add(4, "7F60", "CARS_AVAILABLE");
            dataTable.Rows.Add(5, "VNWH", "RENT");
            return dataTable;
        }
    
        private void PerformCalculations()
        {
            var table = (DataTable)_bindingSource.DataSource;
            var available = table.AsEnumerable()
                .Where(row => row.Field<string>("CarStatus") == "CARS_AVAILABLE");
            var rent = table.AsEnumerable()
                .Where(row => row.Field<string>("CarStatus") == "RENT");
    
            AvailableLabel.Text = $"Available {available.Count()}";
            RentLabel.Text = $"Rent {rent.Count()}";
        }
    }
    
    
    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.