calculated column in datagridview

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • rony08
    New Member
    • Oct 2012
    • 2

    calculated column in datagridview

    Hi friends
    I am new to vb.net , I am having difficulty to add a calculated column to calculate
    amount = salesPrice*qty in the code below
    please help me

    Code:
      
     DataGridView1.ColumnCount = 5
            DataGridView1.Columns(0).Name = "Product ID"
            DataGridView1.Columns(1).Name = "Product Name"
            DataGridView1.Columns(2).Name = "salesPrice"
            DataGridView1.Columns(3).Name = "qty"
            DataGridView1.Columns(4).Name = "amount"
    
            Dim row As String() = New String() {"1", "Product 1"}
            DataGridView1.Rows.Add(row)
            row = New String() {"2", "Product 2"}
            DataGridView1.Rows.Add(row)
            row = New String() {"3", "Product 3"}
            DataGridView1.Rows.Add(row)
            row = New String() {"4", "Product 4"}
            DataGridView1.Rows.Add(row)
  • Frinavale
    Recognized Expert Expert
    • Oct 2006
    • 9749

    #2
    See if the DataGridView CellParsing Event can help you achieve what you are looking for.

    -Frinny

    Comment

    • rony08
      New Member
      • Oct 2012
      • 2

      #3
      Hi
      Thanks for your reply, I have figured out on adding expression column another way but now having difficulty to get the correct out put . any bit of info would be helpful
      Code:
              Dim dt As New DataTable
              dt.Columns.Add("col1")
              dt.Columns.Add("col2")
              dt.Columns.Add("col3")
              dt.Columns.Add("col4")
              dt.Columns.Add("total")
              dt.Columns("total").Expression = "[col1]*[col2]"

      Comment

      • andrewpattinson
        New Member
        • Aug 2012
        • 6

        #4
        In the cell leave event of Quantity column multiply the column of item amount and Quantity and set the cell value of total Amount column. And it is nice if you give Save button to save the records.
        Last edited by Frinavale; Nov 2 '12, 01:04 PM. Reason: Removed link. This is considered spam and is not tolerated on this forum.

        Comment

        • Frinavale
          Recognized Expert Expert
          • Oct 2006
          • 9749

          #5
          @roy08
          That is really cool!
          I have never used the DataColumn.Expr ession Property for anything except filtering. I didn't even realize you could do that.

          Anyways, according to the documentation (in the link I just posted) it seems you do not have to have the [] around the column names.

          Try removing them and see if it fixes your problem :)

          I love it when I learn something new :)

          -Frinny

          Comment

          Working...