You must Sign In to post a response.
  • Category: .NET

    Sum of datagridview column in a textbox based on another column distinct value in windows app

    Dear All,

    I want to get sum of datagridview column in a textbox based on another column's distinct value. For example:

    Band-01: 10
    Band-02: 20
    Band-02: 20
    Band-03: 10
    Band-04: 20
    Band-05: 10

    Here total is 90. But i want total must be 70 as Band-02 is appears twice.

    Please find my attached screenshot in next thread.

    Thanks in advance.
  • #768219
    Hi All,

    Please find attached screenshot.


    Ram Prasad

    Ram Prasad

    Delete Attachment

  • #768223

    Have you tried calling the Enumerable function which has the distinct functionality.

    int Manpowercount= table
    .Select(r => r.Field<int>("ManPower"))

    We can also use SUM() function in the Enumeration.

    You have to mention the column which you want to make it as Distinct.


  • #768227
    you can take use of LINQ for it, see below sample

    var result = dataGridView1.Rows.Cast<DataGridViewRow>()
    .Where(r => r.Cells[0].Value != null)
    .Select (r => r.Cells[0].Value)
    .GroupBy(id => id)
    .OrderByDescending(id => id.Count())
    .Select(g => new { Id = g.Key, Count = g.Count() });

    you can go for below link for details

    Editor, DotNetSpider MVM
    Microsoft MVP 2014 [ASP.NET/IIS]

  • Sign In to post your comments