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
LVL 1
hendrix500Asked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

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
Cloud Class® Course: MCSA MCSE Windows Server 2012

This course teaches how to install and configure Windows Server 2012 R2.  It is the first step on your path to becoming a Microsoft Certified Solutions Expert (MCSE).

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

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Visual Basic.NET

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.