Wrox Programmer Forums
Go Back   Wrox Programmer Forums > Visual Basic > VB.NET 1.0 > Pro VB.NET 2002/2003
|
Pro VB.NET 2002/2003 For advanced Visual Basic coders working .NET version 2002/2003. Beginning-level questions will be redirected to other forums, including Beginning VB.NET.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the Pro VB.NET 2002/2003 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 15th, 2003, 12:17 PM
Friend of Wrox
 
Join Date: Jun 2003
Posts: 428
Thanks: 57
Thanked 2 Times in 2 Posts
Default Getting CHANGED value from Datagrid?

I posted this to the beginner board last week and had lots of reads but no responses, so I thought I'd try it here this week:

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:54 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 July 20th, 2004, 09:08 AM
Registered User
 
Join Date: Jul 2004
Posts: 2
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Thanks Ron. This solved my problem too.:)

fluffycheeseball
 
Old July 20th, 2004, 10:29 AM
Registered User
 
Join Date: Jul 2004
Posts: 2
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Has anyone else noticed a problem with ClearErrors();

I have a dateGrid with a user entry column. Different rows in this column contain strings, numbers etc. Thus I need to do some manual validation of user input.

I am using a System.Data.DataColumnChangeEventHandler (as advised by Ron) and this works fine. When the user enters an erroneous value I call e.Row.SetColumnError(e.Column, "my error string");
Result is a little exclamation mark and the error string is displayed whenever the mouse is over the cell.

But! Once the user enters a valid value I call ClearErrors(). th exclamation mark clears as expected but I cannot get rid of the error string.
Yet, if I then call e.Row.GetColumnError(e.Column) and write the result to console - it shows an empty error string!

I've tried calling e.Row.SetColumnError(e.Column, " "), before (and after ) calling Clear Errors(). No affect

Oddly enough manually resizing the window does stop the error string poopping up every time the mouse is over teh (now correct) cell. But invalidating th edatagrid, refreshing the datagrid (or its form) has no effect.

Any suggestions?


fluffycheeseball
 
Old January 5th, 2005, 11:36 AM
Registered User
 
Join Date: Jan 2005
Posts: 2
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Hi,
I am having similar issues clearing the errors in C#. Basically it appears that Microsoft made the function to create the error and to clear the whole row of errors but not a column. Does anyone else have a solution on this?

dapanther
 
Old January 5th, 2005, 02:11 PM
Registered User
 
Join Date: Jan 2005
Posts: 2
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Hi all,
I got this working this morning in C#. Basically you need to send the SetColumnError a null instead of an empty string and it removes the column error prompt. You will still need to also remove the row error prompt if the row contains no additional errors (I am checking 15 columns). In order to do this I checked the GetColumnsInError array for it's length and if it is less than 1 I kick off the ClearErrors method for the row. Below is some sample code from the hadnler I created. Note: the numTest method is just a simple method to check for numbers in strings...

Code:
        private void data_ColumnChanging(object sender, System.Data.DataColumnChangeEventArgs e) 
        {
            // validate the first column            
            if (e.Column.ColumnName.Equals("fname"))
            {
                if (numTest(e.ProposedValue.ToString()) == true)
                {
                    e.ProposedValue = "Error";
                    e.Row.RowError = "This row contains an error.";
                    e.Row.SetColumnError(e.Column, "First Name must be text.");
                }
                else 
                {
                    if (e.Row.GetColumnError(e.Column) != "")
                    {
                        e.Row.SetColumnError(e.Column, null);
                    }

                    if (e.Row.GetColumnsInError().Length < 1)
                    {
                        e.Row.ClearErrors();
                    }
                }
            }
         }





Similar Threads
Thread Thread Starter Forum Replies Last Post
How can the mousepointer be changed? apike VBScript 0 April 17th, 2007 10:38 PM
hex num changed.. life_s Ng ASP.NET 2.0 Professional 1 October 30th, 2006 12:41 PM
Same file or changed? jacob C# 2 July 8th, 2005 09:09 PM
How do I get the CHANGED value in a datagrid cell? Ron Howerton VB.NET 2002/2003 Basics 2 December 23rd, 2004 10:51 AM
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.