Members

Technology Zones

IBM Learning Center

Articles

Hosted By

MaximumASP

Info

Rated
Read 64,743 times

Contents

Related Categories

Winforms Data Binding Lessons Learned - Reacting to Illegal Row Editing in a DataGrid

RoyOsherove

Reacting to Illegal Row Editing in a DataGrid

Let's say that we have a grid bound to the Courses table, but we don't want the user to edit a course that has already started. We have a StartDate column in the Courses table, so all we need to do now is know when a user has changed a value and cancel that edit if needed.

The way to do this is by reacting to the DataTable's RowChanging event. This event is thrown when one of the Selected DataRow's columns is edited. Here's the code:

AddHandler ds.Tables("Courses").RowChanging, New DataRowChangeEventHandler(AddressOf OnRowChange)

Private Sub OnRowChange(ByVal sender As Object, ByVal e As DataRowChangeEventArgs)
    If IsInProgress(e.Row) Then
        Throw New Exception("Course Is Already In Progress")
    End If
End Sub

Private Function IsInProgress(ByVal row As DataRow) As Boolean
    'code to determine if the current Course is in progress
End Function

The next part is interesting: to cancel the editing, I need to throw an exception. This exception is caught by the DataGrid and shown to the user. Whatever text you write in the exception message is displayed to the user in a message box followed by a question whether they would like to cancel or edit the new value. By the way, this event can also be used to discover new rows. You can also register to the RowDeleting event of the data table to receive notification of a row's deletion, but you cannot cancel that action from within the event. This is a bit more complicated and is explained in the next lesson. You can use the DataRowChangeEventArgs.Action property to determine what action was taken for this even to occur: Deleted, Changed, Added, and so on.

Roy Osherove has spent the past 6+ years developing data driven applications for various companies in Israel. He's acquired several MCP titles, written a number of articles on various .NET topics, most of which can be found on his weblog, and loves discovering new things everyday. Roy is also the author of the Feedable service and of the free regular expression tool, The Regulator.

Comments

  • Re: [4491] A MUCH easier way

    Posted by davidhere40 on 22 May 2007

    I didn't like any of the solutions to this problem until I came up with this one...

    The solution is to bind the entire Child table to a bindingSource and that to a dataGridView.  In this c...

  • Re: your dataview index does not sync with cm index

    Posted by nhrones on 04 Oct 2006

    Use a BindingSource control then use its Find method to get the index of the row.

  • Re: [4491] Winforms Data Binding Lessons Learned

    Posted by dloiac1 on 31 Mar 2006

    I'm getting this error message: "Object reference not set to an instance of an object." when I try to run the code below. I've spent a couple days trying to figure out what it is, but I'm kind of&n...

  • You save me from getting crazy

    Posted by jmeile on 24 Sep 2005

    Well, not really. I'm exagerating a litte bit ;)

    Anyway, I just want to thank you for this nice and realy usefull article.
    I not only got the relationships shown in a DataGrid, but also I could
    ...

  • Posted by Jo_dc on 27 Jul 2005

    try
    Private view As DataView = m_ds.Tables("Stuff").DefaultView

    But anyway I think that the trick is that the field ID is PK