Hard time trying to Add / Edit / Cancel new row

2 posts, 0 answers
  1. Marc Roussel
    Marc Roussel avatar
    429 posts
    Member since:
    Dec 2009

    Posted 21 Jan 2010 Link to this post

    Hi,

    It's almost working but my application crashes because of ONLY ONE LINE which I don't really understand.  Could you please look at the code bellow and tell me if you can why it's saying "Object reference not set to an instante of an object"
    specially when the code is not going anywhere else after that.

    Based on this code, I'm adding a new row, which works.  Now if the user doesn't enter all I want, I cancel everything and I remove
    the new added object from the collection and I'm trying to refresh the grid.  The line that crashes is the Rebind() in the RowEditended

    private void rgvTimes_AddingNewDataItem(object sender, GridViewAddingNewEventArgs e)  
    {  
        if (!_AddingNewTimeRow)  
        {  
            _AddingNewTimeRow = true;  
            _AddedTime = new Object_Time() { JobId = SelectedJob.JobId };  
            _Times.Add(_AddedTime);  
            rgvTimes.Rebind();  
     
            //This is something I'm trying to make the little black triangle marquee selector on the row
            // and trying to make my columns in editing mode.  But doesn't even seem to work and it's not my bigest problem.
            rgvTimes.SelectedItem = _AddedTime;  
            rgvTimes.CurrentItem = rgvTimes.Columns[2];  
            rgvTimes.BeginEdit();  
        }  
        else 
            _SAPMessage.Show("Vous devez saisir les information de la nouvelle ligne avant d'en ajouter une autre...""Message...");  
    }  
    private void rgvTimes_RowEditEnded(object sender, GridViewRowEditEndedEventArgs e)  
    {  
        try 
        {  
            if (_AddingNewTimeRow)  
            {  
                Object_Time Time = e.Row.DataContext as Object_Time;  
     
                if (Time.EmployeName == null || Time.PosteName == null)  
                {  
                    rgvTimes.CancelEdit();  
                    _Times.Remove(Time);  
                    rgvTimes.Rebind();           // If I remove this line everything works fine except the grid isn't refreshed...
                }  
                else 
                    _Proxy.AddNewTimeAsync(Gear.ServiceKey(), Time);  
            }  
     
            _AddingNewTimeRow = false;  
        }  
        catch (Exception ex)  
        {  
            _SAPMessage.Show(ex.Message, "Error...");  
        }  
    }  // When the application go out of this event, it crashes with "Object reference not set to an isntance of an object"
     
  2. Marc Roussel
    Marc Roussel avatar
    429 posts
    Member since:
    Dec 2009

    Posted 21 Jan 2010 Link to this post

    I finally understood.  It's very not easy to find documentation or help on specific things for the grid.
    Searhcing a lot on the forum and finding bits and pieces that I tryed to succeed.  Here's the code changed from the ugly one above

    private void rgvTimes_AddingNewDataItem(object sender, GridViewAddingNewEventArgs e)  
    {  
        _AddingNewTimeRow = true;  
        e.NewObject = new Polyrol_Time();  
    }  
    private void rgvTimes_RowEditEnded(object sender, GridViewRowEditEndedEventArgs e)  
    {  
        if (_AddingNewTimeRow)  
        {  
            _AddingNewTimeRow = false;  
            Polyrol_Time Time = e.NewData as Polyrol_Time;  
            Time.JobId = SelectedJob.JobId;  
            _Proxy.AddNewTimeAsync(Gear.ServiceKey(), Time);  
        }  
    }  
     

    It's good to know that adding the row doesn't add it to the underlaying collection.  This way when I get into the callback, I can add it and do a Rebind()

    Hope this witll help others....
  3. DevCraft banner
Back to Top