validate DataGrid bound to Array

Good afternoon,

I have a DataGrid bound to an ArrayList.  The ArrayList contains "segment" objects.
Among other properties each segment object has a "value" and a "length".  The following must be true:

segment.value.Length = segment.length

In other words, if segment.lenth = 3 then segment.value = 245 would be OK but segment.value = 2456 or segment.value = 24 would not.

Now for the question!  

My Datagrid columns include segment.value and segment.length.  The user can change a segment.value but I want to make sure that the new segment.value.Length is equal to the segment.length when the user trys to leave the segment.value cell. 

Any ideas on how to do this

Thanks
jmatt





Answer this question

validate DataGrid bound to Array

  • Hibri

    Check out this project

    http://www.planet-source-code.com/vb/scripts/ShowCode.asp txtCodeId=1591&lngWId=10

  • abarone

    Glad I could help you for a change.

    jmatt

  • Brian Senecal

    I apologize for the delay in responding!  Your help is greatly appreciated by me... you know how it is when things are busy.

    I like your solution; thanks!  I did go a different way however.  I am trying to code in a more truly object orientated way so I realized I could do the following in my segment class:

    using System;
    using System.Windows.Forms;

    namespace WinFundClasses
    {
    /// <summary>
    /// A class to deal with account number segment meanings.
    /// </summary>
    public class Segment
    {
    private int number;
    private string name;
    private int length;
    private bool used;
    private string val;

    //Constructor
    public Segment()
    {
    //Default
    }

    //Properties
    public int Number
    {
    get {return number;}
    set {number = value;}
    }
    public string Name
    {
    get {return name;}
    set {name = value;} 
    }
    public int Length 
    {
    get {return length;}
    set {length = value;}
    }
    public bool Used
    {
    get {return used;}
    set {used = value;}
    }
    public string Value
    {
    get {return val;}
    set 
    {
    try
    {
    if(value.Length != this.length)
    {
    throw(new ArgumentException(
    "The number of digits in a segment must equal the length defined for the segment."));
    }
    else
    {
    val = value;
    }
    }
    catch (Exception ex)
    {
    MessageBox.Show(ex.Message);
    }
    }
    }

    }
    }

    This works well.

    jmatt

  • shanecav

    Something like:

    'Declare Class Wide Variable 
    Dim LastCell as DataGridCell = Nothing

    'In DataGrid.Click
    Private Sub DataGrid1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles DataGrid1.Click
    LastCell = DataGrid1.CurrentCell
    End Sub

    'In DataGrid.CurrentCellChanged
    Private Sub DataGrid1_CurrentCellChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles DataGrid1.CurrentCellChanged
    If Not IsNothing(LastCell) Then
    If LastCell.ColumnNumber = [Index of Value Column] Then
    If CStr(DataGrid1.Item(LastCell)).Length <> CInt(DataGrid1.Item(LastCell.RowNumber, [Index of Length Column]) Then
    'Set the CurrentCell to LastCell
    DataGrid1.CurrentCell = LastCell
    End If
    End If
    End If
    End Sub

    There may be a better way, but this will work.  Be sure to replace [Index of Value Column] and [Index of Length Column] with the appropriate integer values.

  • Ajay Verma

    Your quite welcome for any help I've been able to provide.  And yes, I do know how it can get!  So, no problem as to the response time.

    This is an interesting solution you've posted!  I've done validation inside a custom class like this before, but not with databinding... I wasn't sure where to catch the exception... I never thought of catching it in the same code block you generate it in!

    The only immediate limitation I can see is that this class requires a user session (ie. you couldn't run this class within a service because of the msgbox), but in this case that's not really an issue.  But it seems that you could add an "UpdateFailed" event to the class and fire that event instead of showing the dialog box.  That would allow the calling class to capture the exception in an event handler and wouldn't require a user session.

    What a great concept to play with!!  Here's exactly what makes a user forum so powerful!!

    Cheers!

  • validate DataGrid bound to Array