Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. DataGridView Calculated Column update when move to new row C#

DataGridView Calculated Column update when move to new row C#

Scheduled Pinned Locked Moved C#
csharpdatabasesql-servervisual-studiosysadmin
5 Posts 2 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • A Offline
    A Offline
    ahmed_one
    wrote on last edited by
    #1

    I am using Visual Studio 2010 with Sql server Express Edition 2005 as backend DB. I've a form with DataGridView with following columns: OrderID ProductID UnitPrice Quantity Total DataGridView is bind to Dataset Table, "Total" is a Calculated column which I've added using following code:

    column = new DataColumn();
    column.DataType = Type.GetType("System.Double");
    column.ColumnName = "Total";
    ds.Tables[0].Columns.Add(column);
    ds.Tables[0].Columns["Total"].Expression = "UnitPrice * Quantity";

    Code works fine, it calculate the Total by multiplying UnitPrice to Quantity. The problem is this calculation is made when I move to new row. What I need is to it calculate Total when there is any change in UnitPrice or Quantity. Is it possible? Any ideas/Suggestions will be highly appreciated. Regards Ahmed

    S 1 Reply Last reply
    0
    • A ahmed_one

      I am using Visual Studio 2010 with Sql server Express Edition 2005 as backend DB. I've a form with DataGridView with following columns: OrderID ProductID UnitPrice Quantity Total DataGridView is bind to Dataset Table, "Total" is a Calculated column which I've added using following code:

      column = new DataColumn();
      column.DataType = Type.GetType("System.Double");
      column.ColumnName = "Total";
      ds.Tables[0].Columns.Add(column);
      ds.Tables[0].Columns["Total"].Expression = "UnitPrice * Quantity";

      Code works fine, it calculate the Total by multiplying UnitPrice to Quantity. The problem is this calculation is made when I move to new row. What I need is to it calculate Total when there is any change in UnitPrice or Quantity. Is it possible? Any ideas/Suggestions will be highly appreciated. Regards Ahmed

      S Offline
      S Offline
      smithjdst
      wrote on last edited by
      #2

      Have you tried to update the values in the CellLeave event for your datagrid.

      A 1 Reply Last reply
      0
      • S smithjdst

        Have you tried to update the values in the CellLeave event for your datagrid.

        A Offline
        A Offline
        ahmed_one
        wrote on last edited by
        #3

        No, can you give me some sample code please?

        S 1 Reply Last reply
        0
        • A ahmed_one

          No, can you give me some sample code please?

          S Offline
          S Offline
          smithjdst
          wrote on last edited by
          #4

          after looking a little more, it may be better to use the cellvaluechanged event... sample (note this was tested only in C# win32.forms, and the column ValueTypes are strings):

          private void dataGrid_CellValueChanged(object sender, DataGridViewCellEventArgs e) {
          /*check Changing cell is "not" in the "Total" Column,
          *would keep calling cell changed event, when the "Total" cell value changes*/
          if ((e.RowIndex >= 0) && e.ColumnIndex != DataGrid.Columns["Total"].Index) {
          //r = row of the changed Cell.
          int r = e.RowIndex;

                //get values from cells in the same row
                int Qty = 0;
                int.TryParse((string)DataGrid\["Quantity", r\].Value, out Qty);
          
                double Price = 0D;
                double.TryParse((string)DataGrid\["UnitPrice", r\].Value, out Price);
          
                //Calculate Total, Change cell value in "Total"
                DataGrid\["Total", r\].Value = Price \* Qty;
           }
          

          }

          A 1 Reply Last reply
          0
          • S smithjdst

            after looking a little more, it may be better to use the cellvaluechanged event... sample (note this was tested only in C# win32.forms, and the column ValueTypes are strings):

            private void dataGrid_CellValueChanged(object sender, DataGridViewCellEventArgs e) {
            /*check Changing cell is "not" in the "Total" Column,
            *would keep calling cell changed event, when the "Total" cell value changes*/
            if ((e.RowIndex >= 0) && e.ColumnIndex != DataGrid.Columns["Total"].Index) {
            //r = row of the changed Cell.
            int r = e.RowIndex;

                  //get values from cells in the same row
                  int Qty = 0;
                  int.TryParse((string)DataGrid\["Quantity", r\].Value, out Qty);
            
                  double Price = 0D;
                  double.TryParse((string)DataGrid\["UnitPrice", r\].Value, out Price);
            
                  //Calculate Total, Change cell value in "Total"
                  DataGrid\["Total", r\].Value = Price \* Qty;
             }
            

            }

            A Offline
            A Offline
            ahmed_one
            wrote on last edited by
            #5

            This helps a lot....big thanks...

            1 Reply Last reply
            0
            Reply
            • Reply as topic
            Log in to reply
            • Oldest to Newest
            • Newest to Oldest
            • Most Votes


            • Login

            • Don't have an account? Register

            • Login or register to search.
            • First post
              Last post
            0
            • Categories
            • Recent
            • Tags
            • Popular
            • World
            • Users
            • Groups