Wrox Programmer Forums
Go Back   Wrox Programmer Forums > Visual Basic > VB.NET 1.0 > VB.NET 2002/2003 Basics
|
VB.NET 2002/2003 Basics For coders who are new to Visual Basic, working in .NET versions 2002 or 2003 (1.0 and 1.1).
Welcome to the p2p.wrox.com Forums.

You are currently viewing the VB.NET 2002/2003 Basics section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old December 12th, 2003, 05:30 PM
Friend of Wrox
 
Join Date: Jun 2003
Posts: 428
Thanks: 57
Thanked 2 Times in 2 Posts
Default How do I get the CHANGED value in a datagrid cell?

I want to obtain the value entered into a datagrid cell in a VB.NET windows app (not ASP), so I can validate it before the change is applied to the dataset. The following code gets the ORIGINAL value in the cell, but I can't figure out how to obtain the NEW value:

grdServices.Item(grdServices.CurrentCell).ToString ()

What do I need to do to get the new value entered by the user? I've tried the _Validating function, which fires when the user leaves the cell (and, oddly, when the user enters another cell), but its arguments don't appear to contain any properties to obtain the NEW value, just the OLD one. Is there another method I should use or what?
 
Old December 15th, 2003, 04:53 PM
Friend of Wrox
 
Join Date: Jun 2003
Posts: 428
Thanks: 57
Thanked 2 Times in 2 Posts
Default

Here's the answer I've finally found, if anybody cares:

Although it seems intuitive, you CANNOT check the new value by referencing the grid. It's necessary instead to reference the underlying table in the dataset (even though the table has NOT actually been updated - it makes no sense to me, but then very little MS has done with .NET makes sense to me) using the ColumnChanging event. Here's some genericized code that works:

Private Sub ColumnChangingValidation(ByVal sender As Object, _
  ByVal e As System.Data.DataColumnChangeEventArgs)

' first check the column name because this subroutine
' is not column specific (that would be too easy)
    If (e.Column.ColumnName.Equals("Server")) Then

            ' Validate e.ProposedValue, which is
            ' what the user entered
            If YourValidationRoutine(e.ProposedValue) = False Then

                ' set the row hover message on the table that
                ' makes it magically appear on the grid
                e.Row.RowError = "The Server column contains an error"

                ' set the column hover message on the table that
                ' makes it magically appear on the grid
                e.Row.SetColumnError(e.Column, "Value cannot be " & _ CType(e.ProposedValue, String) & " - Expected blah, blah, blah...")
            End If
        End If
    End Sub

Next, the ColumnChanging event has to be related to the table in the dataset:

AddHandler DataSetName.Tables("TableName").ColumnChanging, AddressOf ColumnChangingValidation

Then when the user enters a value into the indicated column and tabs out or clicks on another cell or another control, the event fires and the error indicator appears on the grid row and column. I haven't figured out hwo to cancel the update to the field in the table yet, but it should only take me another week or so to figure out that minor detail.

If you want to know how to do this in C#, you can find MicroSoft's ever-so-helpful example code at:

http://msdn.microsoft.com/library/de...ridcontrol.asp
 
Old December 23rd, 2004, 10:51 AM
Registered User
 
Join Date: Dec 2004
Posts: 1
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Here's the answer for cancelling the update.

Example

Private Sub ColumnChangingValidation(ByVal sender As Object, _
            ByVal e As System.Data.DataColumnChangeEventArgs)
  'cancel the event
  'get the original value
    e.Row.Item(e.Column.Ordinal)
  'validate if this the right value
  'if not then set the proposed value back to original
    e.ProposedValue = e.Row.Item(e.Column.Ordinal)
End Sub

good luck





Similar Threads
Thread Thread Starter Forum Replies Last Post
Excel VBA Check When Cell Has Changed tonyrosen Excel VBA 13 March 24th, 2015 05:15 PM
How do I check if a cell content has changed SirAldemar Access VBA 2 August 13th, 2008 07:46 AM
Getting CHANGED value from Datagrid? Ron Howerton Pro VB.NET 2002/2003 5 January 5th, 2005 02:11 PM
DataGrid Cell mrideout BOOK: Beginning ASP.NET 1.0 0 August 17th, 2004 12:34 PM
selection index changed in datagrid lily611 General .NET 1 July 21st, 2004 07:56 AM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.