Home Forums WinForms controls Xceed Grid for WinForms Updating data to data source automatically.

Viewing 11 posts - 1 through 11 (of 11 total)
  • Author
    Posts
  • User (Old forums)
    Member
    Post count: 23064
    #13249 |

    Ohh all you great brains I’ll seek your guidance 🙂

    I’m having some problems in coding an automatically update to the database whenever Endedit is called regardless if it’s from the insertions row or a normal row. I’ve placed my update method in the EndingEdit event. I’m really trying to achieve two things: I want to update data to the under laying database and second I want to catch exceptions, if any (Constraint Exception), display an message to the user and then prevent the row from leaving edit mode.

    My code looks something like this:

    private void RowEndingEdit(object sender, CancelEventArgs e)
    {
    try
    {
    CommitData(false);

    xgDevianceTypes.AllowCellNavigation = false;

    for(int i=0; i < xgDevianceTypes.DataRows.Count; i++)
    { xgDevianceTypes.DataRows[i].CanBeCurrent = true;
    xgDevianceTypes.DataRows[i].CanBeSelected = true;
    }

    }
    catch (SSADataManager.DALUniqueConstraintException)
    {
    e.Cancel = true;
    MessageBox.Show(“Some error message”)
    }
    }

    That seems to work half when inserting a row: In that way that I’m getting two error messages: the one I’m showing in my catch(Some error message) and oddly also one from the RowValidationError event saying “the row’s attempt to end edit mode has been cancelled”. Can’t I prevent the last message??

    The next problem is when there could be a validation error e.g when a text exceeds its maximum length; I just get a Target invocation exception. If I comment out the CommitData then I get the normal error message throw by the data source but I’ll lost my functionality. This applies also when just editing a row.

    I hope that you can understand my problem from the above description and there perhaps is a bright brain than can crack this code nut.

    Thanks in advance
    Best regards
    Søren Skov Andersen, Denmark

    Imported from legacy forums. Posted by Forest (had 7552 views)

    User (Old forums)
    Member
    Post count: 23064

    Well Well. After a bit coding and something that looks like “try by error method” and a little bit thinking I found a solution or perhaps at better term is “some kind of workaround” 🙁

    In my row validation event I placed the following code:

    e.CancelEdit = false;

    if(!(e.Exception is Xceed.Grid.GridException || e.Exception is System.Reflection.TargetInvocationException))
    MessageBox.Show(e.Exception.Message);

    if(e.Exception is System.Reflection.TargetInvocationException)
    MessageBox.Show(e.Exception.InnerException.Message);

    But wheeeee I’m not happy about it!!!!!!!

    Is there somebody who can explain why the exception throw by the data source e.g. when a text exceeds its maximum length is placed in a Target Invocation Exception. This only happens when I’m committing my data source to the database in the RowEndingEdit event (notice previous post).

    And could anybody please explain the consequences surrounding the above work around?

    Imported from legacy forums. Posted by Forest (had 501 views)

    User (Old forums)
    Member
    Post count: 23064

    Hi

    What are your datasource?

    A data collection (IList) or a dataSet?

    Regards
    Thomas

    Imported from legacy forums. Posted by C# (had 635 views)

    User (Old forums)
    Member
    Post count: 23064

    Hello

    Well my data source is in fact a collection of business entities. The collection inherits from CollectionBase and that’s basically an implementation of the IList. Futhermore are IBindingList, ITypedList, ISupportInitialize implemented.

    Hope this is useful.

    – And thanks for taking time to reflect over this problem.

    Imported from legacy forums. Posted by Forest (had 419 views)

    User (Old forums)
    Member
    Post count: 23064

    Hi

    I have a solution that works with all the existing rows.
    I use this myself.

    I also use a IList that implements IBindingList, ITypedList, ISupportInitialize.

    You need to subscribe to 3 events:

    – cell.ValueChanging
    – cell.ValidationError
    – row.ValidationError

    In initGrid() I bind the grid to the datasource and subscribe to the events.
    cell_ValidationError(…) and row_ValidationError(…) are called by the grid, when a user enters a incorrect value.

    The most interested stuff is in cell_ValueChanging(…) event.
    This event is called before the data is commited, so it is possible to handle errors.

    ———————— code —————————————

    //Cell validation error event
    private void cell_ValidationError(object sender, Xceed.Grid.CellValidationErrorEventArgs e)
    {
    e.CancelEdit = false;
    MessageBox.Show(e.ToString());
    }

    //Row validation error event
    private void row_ValidationError(object sender, Xceed.Grid.RowValidationErrorEventArgs e)
    {
    e.CancelEdit = false;
    MessageBox.Show(e.ToString());
    }

    //Cell value changing event
    private void cell_ValueChanging(object sender, Xceed.Grid.ValueChangingEventArgs e)
    {

    //old cell value
    object oldCellValue = ((Cell)(((DataCell)(sender)))).Value;

    //field name
    string changedPropertyName = ((Cell)(((DataCell)(sender)))).FieldName.ToString();

    //index in collection
    int collectionIndex = ((DataRow)((DataCell)(sender)).ParentRow).Index;

    //changed object
    ComposerEditable changedEntity = (ComposerEditable)this.list[collectionIndex];

    //invoke the changed object with the new cell value
    Type type = typeof(Composer);
    type.InvokeMember(changedPropertyName, BindingFlags.DeclaredOnly | BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance | BindingFlags.SetProperty, null, changedEntity, new Object[] {e.NewValue});

    try
    {
    //update database…
    }
    catch(Exception ex)
    {
    //update failed.
    e.NewValue = oldCellValue;
    e.Cancel = false;
    MessageBox.Show(ex.Message);
    }
    }

    private void initGrid()
    {
    this.gridControl1.BeginInit();

    //test data
    for(int i=0; i<10; i++)
    {
    Xceed.Grid.Samples.ComposerEditable composer = new ComposerEditable();
    list.Add(composer);
    }

    //bind to datasource
    this.gridControl1.DataSource = list;

    //subscribe to events
    foreach( Xceed.Grid.Cell cell in this.gridControl1.DataRowTemplate.Cells )
    {
    cell.ValueChanging += new Xceed.Grid.ValueChangingEventHandler(cell_ValueChanging);
    cell.ValidationError += new Xceed.Grid.CellValidationErrorEventHandler(cell_ValidationError);
    }
    foreach( Xceed.Grid.DataRow row in gridControl1.DataRows)
    {
    row.ValidationError += new Xceed.Grid.RowValidationErrorEventHandler(row_ValidationError);
    }
    this.gridControl1.AddingDataRow += new AddingDataRowEventHandler(gridControl1_AddingDataRow);

    this.gridControl1.EndInit();
    }

    Regards / venlig hilsen 🙂

    /Thomas

    Imported from legacy forums. Posted by C# (had 991 views)

    User (Old forums)
    Member
    Post count: 23064

    Notice that this line:
    e.CancelEdit = false;

    in cell_cell_ValidationError(…) and row_ValidationError(…)
    prevent the row from leaving edit mode.

    /Thomas

    Imported from legacy forums. Posted by C# (had 587 views)

    User (Old forums)
    Member
    Post count: 23064

    Hi Thomas

    Hmmm I’ll try your solution.

    I’ll get back hopefully before the end of the weekend but wow my workload is heavy. 🙁

    – and again thanks for helping.

    Imported from legacy forums. Posted by Forest (had 347 views)

    User (Old forums)
    Member
    Post count: 23064

    Hmm er du fra danmark? 🙂

    Translated: Are from Denmark?

    Imported from legacy forums. Posted by Forest (had 462 views)

    User (Old forums)
    Member
    Post count: 23064

    Hej Søren.

    Ja, jeg kommer fra Danmark.

    Jeg har brugt forskellige 3.parts .NET komponenter, og må sige at Xceed laver komponenter af meget høj kvalitet.

    /Thomas

    Imported from legacy forums. Posted by C# (had 640 views)

    User (Old forums)
    Member
    Post count: 23064

    Vi var nogle af de første der brugte xceed.net grid så vi har nærmest været ude i de små kroge på godt og ondt. Det er dog først her på de seneste jeg er kommet på xceed griddet men det virker nu ok gennemtænkt.

    Det ser ud til at dit forslag holder vand – tak for det.

    Vi sidder i Århus hvor er du i landet?

    Hej fra Søren

    Translation:

    We where some of the first to use the Xceed grid and I think we have been in all the corners of the grid for “better and worse”, our generally impression of the Xceed grid is that it’s a well thought and stable product.

    Your suggestion seems to be working – thanks.

    We’re placed in Aarhus, Denmark where are you situated?

    Imported from legacy forums. Posted by Forest (had 400 views)

    User (Old forums)
    Member
    Post count: 23064

    Hej Søren

    Jeg bor i Århus, men arbejder pt. i Kolding.

    /Thomas

    Imported from legacy forums. Posted by C# (had 8015 views)

Viewing 11 posts - 1 through 11 (of 11 total)
  • You must be logged in to reply to this topic.