• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 305
  • Last Modified:

DataGrids

Hi everyone, I have a problem editing data in a datagrid.

When a user changes values in a column on the datagrid, I want to perform a calulation and put the result in another column. This works fine, except the change only occurs afterthe user leaves the current row. I want the change to occur without the user having to do this.

My code is as follows:

 ts1 = New DataGridTableStyle
        ts1.MappingName = "gw_BulkChanges"

Dim yourcolumn As New DataColumn
       yourcolumn.ColumnName = "newmargin"
        yourcolumn.DataType = GetType(Double)
        yourcolumn.Expression = "(((dec_newretail*0.8510638)-dec_newcost)/(dec_newretail*0.8510638))*100"
        dt.Columns.Add(yourcolumn)

        Dim myDataCol_10 As New DataGridTextBoxColumn
        myDataCol_10.HeaderText = "New Margin"
        myDataCol_10.MappingName = "newmargin"
        myDataCol_10.Width = 75
        myDataCol_10.NullText = ""
        myDataCol_10.ReadOnly = True
        myDataCol_10.Alignment = HorizontalAlignment.Right
        'myDataCol_10.DataAlignment = HorizontalAlignment.Right
        myDataCol_10.Format = "f"
        ts1.GridColumnStyles.Add(myDataCol_10)


Private Sub grd_BulkDetail_CurrentCellChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles grd_BulkDetail.CurrentCellChanged

        Dim gridCurrencyManager As CurrencyManager = _
        CType(Me.BindingContext(grd_BulkDetail.DataSource, _
        grd_BulkDetail.DataMember), CurrencyManager)
        gridCurrencyManager.EndCurrentEdit()


    End Sub

----------------------------------------------
Essentially, what I need is for the sub just above to fire when the text is changed, not when the user leaves the line in the grid.

I have done this before and had it working so I know it's possible.
Any help would be great
0
hendrix500
Asked:
hendrix500
  • 3
  • 2
  • 2
  • +1
1 Solution
 
RonaldBiemansCommented:
0
 
RonaldBiemansCommented:
Although your code should update the extra column as soon as you move from a cell (not just a row) so what is wrong with that  ?
0
 
iboutchkineCommented:
It is not a good idea to calculate as you typing. Just imagine tha in your expression
    yourcolumn.Expression = "(((dec_newretail*0.8510638)-dec_newcost)/(dec_newretail*0.8510638))*100"
you start typing dec_newretail value. On text change event (which means after typing every number) you will perform calculation
If for example dec_newretail  = 0.25
after you typed 0 you will perform the following operation

 "(((0*0.8510638)-dec_newcost)/(0*0.8510638))*100"

it will cause division by zero and will generate an error. That is why the approach to calculate on a celle leave event is much better
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
hendrix500Author Commented:
yeah think I agree. If I stick with that method - everytime i leave the cell the sub below is called


Private Sub grd_BulkDetail_CurrentCellChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles grd_BulkDetail.CurrentCellChanged

        Dim gridCurrencyManager As CurrencyManager = _
        CType(Me.BindingContext(grd_BulkDetail.DataSource, _
        grd_BulkDetail.DataMember), CurrencyManager)
        gridCurrencyManager.EndCurrentEdit()


    End Sub
---------------------------------------------------
Do you know how I can tell which row in my dataset has been altered? I want to do some calculations in this sub but need to know which row in my dataset do perform the calulations on
0
 
iboutchkineCommented:
you can check changes in the underlying datatable.
Unfortunately there is no method datatable.haschanges.

So you should use a workaround something as pseudo

dim myNewTable as new datatable = datasource.table.getchanges
And than test if that is filled with rows


You will get there al the changed rows from the datagrid
0
 
hendrix500Author Commented:
Ive tried this:

Dim myNewTable As New DataTable
myNewTable = Me.objDS.Tables("gw_BulkChanges").GetChanges

Dim prodcode as string
Dim nCnt as short = 0
Dim dr as DataRow

For Each dr In myNewTable.rows
            prodcode = myNewTable.Rows(nCnt).Item("prodcode")
            nCnt += 1
Next
---------------------------------
It goes into the For Loop for every row in the datagrid - not just the amended row
0
 
iboutchkineCommented:
this is a general idea that works. try it

  If ds.HasChanges Then
            'build another ds that has only modified rows
            Dim ds2 As DataSet = ds.GetChanges
            Dim dt As DataTable
            'update each datatable individually
            For Each dt In ds2.Tables
                If dt.Rows.Count > 0 Then
                    'this table contains modified rows
                    MessageBox.Show("Updating table " & dt.TableName)
                    'proceed with update
                End If
            Next
        End If
0
 
CetusMODCommented:
Closed, 125 points refunded.
CetusMOD
Community Support Moderator
0

Featured Post

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

  • 3
  • 2
  • 2
  • +1
Tackle projects and never again get stuck behind a technical roadblock.
Join Now