locked
Sort and sum up gridview(devexpress) column in vb.net RRS feed

  • Question

  • Hello guys! I just wanna ask some help maybe someone here can help.

    I'll gonna go straight to my query.

    So I have a gridview that displays column 1, column 2, column 3

    In column 1 contains random numbers like 1-100 every row

    Also in column 2 and 3 that only contains 1 and 0(like true or false).

    It looks like this in gridview,

    column 1 | column 2 | column 3

    15           |       1      |       0

    19           |       1      |       0

    26           |       0      |       1

    75           |       1      |       0

    so on and so forth. What I wanna do is to get the total value of column 2 that has a value of (15 to 19 in column 1, bracket) when I click on the button1.

    In this example, (15-19,bracket) of column 1 has a total value of 2. And to display the total value in textbox separated every bracket. Sample of bracket I'm using is like this (15-19, 20-24, 25-29, so on and so forth.).

    Hope someone can understand my explanation and help me.

    Thanks in advance!


    • Edited by OneInch Saturday, April 25, 2020 2:08 AM forgot something
    Friday, April 24, 2020 9:05 AM

All replies

  • Hello,

    You should use their support forum

    https://supportcenter.devexpress.com/ticket/list


    Please remember to mark the replies as answers if they help and unmarked them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.

    NuGet BaseConnectionLibrary for database connections.

    StackOverFlow
    profile for Karen Payne on Stack Exchange

    Friday, April 24, 2020 9:41 AM
  • Hi Karen. I appreciated your immediate response. Actually I just put the devexpress to be more specific but default vb datagridview will do. My bad.
    Friday, April 24, 2020 11:44 PM
  • Hi

    Try this code - see if it suits. It is a stand alone example.

    ' Form1 with DataGridView1 andf
    ' Labels (1-5)
    Option Strict On
    Option Explicit On
    Public Class Form1
      Dim dt As New DataTable("Freddy")
      Dim BS As New BindingSource
      Dim r As New Random
      Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        With dt
          .Columns.Add("Column1", GetType(Integer))
          .Columns.Add("Column2", GetType(Boolean))
          .Columns.Add("Column3", GetType(Boolean))
          For i As Integer = 0 To 19
            .Rows.Add(r.Next(15, 40), IIf(r.Next(2) = 2, True, False), IIf(r.Next(2) = 2, True, False))
          Next
        End With
        BS.DataSource = dt
        DataGridView1.DataSource = BS
        CountEm()
      End Sub
      Private Sub DataGridView1_CellValidated(sender As Object, e As DataGridViewCellEventArgs) Handles DataGridView1.CellValidated
        CountEm()
      End Sub
      Sub CountEm()
        BS.EndEdit()
        Label1.Text = "(15-19 inc)    " & dt.Select("Column1 > 14 and Column1 < 20").Count.ToString
        Label2.Text = "(20-24 inc)    " & dt.Select("Column1 > 19 and Column1 < 25").Count.ToString
        Label3.Text = "(25-29 inc)    " & dt.Select("Column1 > 24 and Column1 < 30").Count.ToString
        Label4.Text = "(30-34 inc)    " & dt.Select("Column1 > 29 and Column1 < 35").Count.ToString
        Label5.Text = "(35-39 inc)    " & dt.Select("Column1 > 34 and Column1 < 40").Count.ToString
      End Sub
    End Class


    Regards Les, Livingston, Scotland

    Saturday, April 25, 2020 1:00 AM
  • Hi leshay! I really appreciated your help and your code, but I don't have to generate values in column1 since it has random values in it already. I only need to count how many "1" in column2 that has a value of 15 to 19 in column1 for example. =)
    Saturday, April 25, 2020 2:11 AM
  • Hi

    OK,try this one  .............................

    ' Form1 with DataGridView1 andf
    ' Labels (1-5)
    Option Strict On
    Option Explicit On
    Public Class Form1
      Dim dt As New DataTable("Freddy")
      Dim BS As New BindingSource
      Dim r As New Random
      Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        With dt
          .Columns.Add("Column1", GetType(Integer))
          .Columns.Add("Column2", GetType(Boolean))
          .Columns.Add("Column3", GetType(Boolean))
          For i As Integer = 0 To 19
            .Rows.Add(r.Next(15, 40), IIf(r.Next(2) = 0, True, False), IIf(r.Next(2) = 0, True, False))
          Next
        End With
        BS.DataSource = dt
        DataGridView1.DataSource = BS
        CountEm()
      End Sub
      Private Sub DataGridView1_CellValidated(sender As Object, e As DataGridViewCellEventArgs) Handles DataGridView1.CellValidated
        CountEm()
      End Sub
      Sub CountEm()
        BS.EndEdit()
        Label1.Text = "(15-19 inc)    " & dt.Select("Column2 = True and Column1 > 14 and Column1 < 20").Count.ToString
        Label2.Text = "(20-24 inc)    " & dt.Select("Column2 = True and Column1 > 19 and Column1 < 25").Count.ToString
        Label3.Text = "(25-29 inc)    " & dt.Select("Column2 = True and Column1 > 24 and Column1 < 30").Count.ToString
        Label4.Text = "(30-34 inc)    " & dt.Select("Column2 = True and Column1 > 29 and Column1 < 35").Count.ToString
        Label5.Text = "(35-39 inc)    " & dt.Select("Column2 = True and Column1 > 34 and Column1 < 40").Count.ToString
      End Sub
    End Class
    


    Regards Les, Livingston, Scotland

    Saturday, April 25, 2020 3:54 AM
  • This has been answered on stackoverflow

    https://stackoverflow.com/questions/61419891/sort-and-sum-up-gridview-column-in-vb-net


    Please remember to mark the replies as answers if they help and unmarked them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.

    NuGet BaseConnectionLibrary for database connections.

    StackOverFlow
    profile for Karen Payne on Stack Exchange

    Saturday, April 25, 2020 10:18 AM
  • When the view footer is visible, end-users can right-click it and use a context menu to add and remove summary items. It is also possible to right-click existing summaries to change their aggregate functions.



    GridOptionsMenu.EnableFooterMenu - specifies whether or not end-users can invoke the view footer context menu.

    GridColumn.AllowSummaryMenu - if disabled, end-users are able to create only "Count" summaries for this column. Other aggregate functions are disabled.

    At design time, modify the column GridColumn.SummaryItem property to add a single summary item for this column.



    GridSummaryItem.SummaryType

    Specifies the aggregate function for this summary item. If you choose "Custom", you need to handle the GridView.CustomSummaryCalculate event to manually calculate a summary item value. Note that this event is not raised in Server Mode. See the Working with Summaries in Code. Custom Summaries article to learn more about custom summaries.

    GridSummaryItem.DisplayFormat

    Applies value formatting according to the "<custom static text>{0<:format specifier<precision specifier>>}<custom static text>" formula.

    GridSummaryItem.Tag

    Stores any object that you want to associate with this item.

    GridSummaryItem.FieldName

    Specifies a data source field whose values this summary item processes. This allows you to display summaries that process values of one column below another column.

    If you need more than one summary, click the ellipsis button against the GridColumn.Summary property in Visual Studio's Property Grid and use the Collection Editor dialog to create summary items.
    Saturday, April 25, 2020 11:50 AM
  • Charles,

    This is a Microsoft forum. 

    Devenx tools where you are talking about has few to do with it. 

    Use the first link Karen gave you.


    Success
    Cor

    Saturday, April 25, 2020 7:53 PM