Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. how to reject the invalid row adding to datatable?

how to reject the invalid row adding to datatable?

Scheduled Pinned Locked Moved C#
helptutorialquestion
7 Posts 2 Posters 2 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • W Offline
    W Offline
    wangier
    wrote on last edited by
    #1

    dataTable1.RowChaging += new DataRowChangeEventHandler(dataTable1_rowChaing) private void dataTable1_RowChanging(object sender, DataRowChangeEventArgs e) { if ( e.Action == DataRowAction.Add ) { if (...) throw new ApplicationException("不合法的检查项目!"); } } ------------ the problem is : this dataTable is associated to a datagrid, when add a new row through the datagrid, the rowchanging event can be trigered. If the row is invalid, the exception will be throwed, and the new row cannot be added to the dataTable1. But when should I handle this Exception? I can't catch this exception.

    H 1 Reply Last reply
    0
    • W wangier

      dataTable1.RowChaging += new DataRowChangeEventHandler(dataTable1_rowChaing) private void dataTable1_RowChanging(object sender, DataRowChangeEventArgs e) { if ( e.Action == DataRowAction.Add ) { if (...) throw new ApplicationException("不合法的检查项目!"); } } ------------ the problem is : this dataTable is associated to a datagrid, when add a new row through the datagrid, the rowchanging event can be trigered. If the row is invalid, the exception will be throwed, and the new row cannot be added to the dataTable1. But when should I handle this Exception? I can't catch this exception.

      H Offline
      H Offline
      Heath Stewart
      wrote on last edited by
      #2

      You could create a handler for AppDomain.UnhandledException, but it might be simpler to just display an error or warning message box to the user with MessageBox.Show. With Windows XP and higher, they are now using balloons when invalid characters are typed, such as a backslash in a filename or another invalid character somewhere. You could always do something like that, too. Basically, just don't throw the exception and instead provide feedback to the user.

      -----BEGIN GEEK CODE BLOCK----- Version: 3.21 GCS/G/MU d- s: a- C++++ UL@ P++(+++) L+(--) E--- W+++ N++ o+ K? w++++ O- M(+) V? PS-- PE Y++ PGP++ t++@ 5 X+++ R+@ tv+ b(-)>b++ DI++++ D+ G e++>+++ h---* r+++ y+++ -----END GEEK CODE BLOCK-----

      W 1 Reply Last reply
      0
      • H Heath Stewart

        You could create a handler for AppDomain.UnhandledException, but it might be simpler to just display an error or warning message box to the user with MessageBox.Show. With Windows XP and higher, they are now using balloons when invalid characters are typed, such as a backslash in a filename or another invalid character somewhere. You could always do something like that, too. Basically, just don't throw the exception and instead provide feedback to the user.

        -----BEGIN GEEK CODE BLOCK----- Version: 3.21 GCS/G/MU d- s: a- C++++ UL@ P++(+++) L+(--) E--- W+++ N++ o+ K? w++++ O- M(+) V? PS-- PE Y++ PGP++ t++@ 5 X+++ R+@ tv+ b(-)>b++ DI++++ D+ G e++>+++ h---* r+++ y+++ -----END GEEK CODE BLOCK-----

        W Offline
        W Offline
        wangier
        wrote on last edited by
        #3

        But if I don't throw the exception in RowChaging event, i can't prevent the new row to be added to the data table. Or some way to do that?

        H 1 Reply Last reply
        0
        • W wangier

          But if I don't throw the exception in RowChaging event, i can't prevent the new row to be added to the data table. Or some way to do that?

          H Offline
          H Offline
          Heath Stewart
          wrote on last edited by
          #4

          Sorry about that. I've been doing so much control development lately I'm too used to such events being CancelEventHandler events (or similar)! Yes, you'll have to throw the exception so add a handler to AppDomain.UnhandledException like I mentioned before. I recommend you also create a custom exception class that derives from ApplicationException (typically used for custom exceptions to give them a common, application-based base class) that you throw from the DataTable.RowChanging event handler. This way, you can easily just catch this exception in the handler for the AppDomain.UnhandledException event and handle appropriately, which is better than throwing such a oft-used exception and trying to determine where it came from and why it was thrown.

          -----BEGIN GEEK CODE BLOCK----- Version: 3.21 GCS/G/MU d- s: a- C++++ UL@ P++(+++) L+(--) E--- W+++ N++ o+ K? w++++ O- M(+) V? PS-- PE Y++ PGP++ t++@ 5 X+++ R+@ tv+ b(-)>b++ DI++++ D+ G e++>+++ h---* r+++ y+++ -----END GEEK CODE BLOCK-----

          W 1 Reply Last reply
          0
          • H Heath Stewart

            Sorry about that. I've been doing so much control development lately I'm too used to such events being CancelEventHandler events (or similar)! Yes, you'll have to throw the exception so add a handler to AppDomain.UnhandledException like I mentioned before. I recommend you also create a custom exception class that derives from ApplicationException (typically used for custom exceptions to give them a common, application-based base class) that you throw from the DataTable.RowChanging event handler. This way, you can easily just catch this exception in the handler for the AppDomain.UnhandledException event and handle appropriately, which is better than throwing such a oft-used exception and trying to determine where it came from and why it was thrown.

            -----BEGIN GEEK CODE BLOCK----- Version: 3.21 GCS/G/MU d- s: a- C++++ UL@ P++(+++) L+(--) E--- W+++ N++ o+ K? w++++ O- M(+) V? PS-- PE Y++ PGP++ t++@ 5 X+++ R+@ tv+ b(-)>b++ DI++++ D+ G e++>+++ h---* r+++ y+++ -----END GEEK CODE BLOCK-----

            W Offline
            W Offline
            wangier
            wrote on last edited by
            #5

            I did that. And I throw a ErrorDataRowException which inherits from Application in the data table's RowChanging event. But when the ErrorDataRowException is thrown, the application didnot enter the method UnHandledExceptionHandler. Instead, the application still popup an window in which told me there is an unhandled exception. why? pls help me. [STAThread] static void Main() { AppDomain currentAppDomain = AppDomain.CurrentDomain; currentAppDomain.UnhandledException += new UnhandledExceptionEventHandler(UnHandledExceptionHandler); FormRegisterConsole fmMain = new FormRegisterConsole(); Application.Run(fmMain); } private static void UnHandledExceptionHandler(object sender, UnhandledExceptionEventArgs args) { Exception e = (Exception) args.ExceptionObject; if (e.GetType() == typeof(Health.ApplicationLibrary.RIS.Register.ErrorDataRowException)) { MessageBox.Show(e.Message, "data updating", MessageBoxButtons.OK, MessageBoxIcon.Error); } else { MessageBox.Show("出现未处理错误: " + e.Message, "未处理错误", MessageBoxButtons.OK, MessageBoxIcon.Error); } }

            H 1 Reply Last reply
            0
            • W wangier

              I did that. And I throw a ErrorDataRowException which inherits from Application in the data table's RowChanging event. But when the ErrorDataRowException is thrown, the application didnot enter the method UnHandledExceptionHandler. Instead, the application still popup an window in which told me there is an unhandled exception. why? pls help me. [STAThread] static void Main() { AppDomain currentAppDomain = AppDomain.CurrentDomain; currentAppDomain.UnhandledException += new UnhandledExceptionEventHandler(UnHandledExceptionHandler); FormRegisterConsole fmMain = new FormRegisterConsole(); Application.Run(fmMain); } private static void UnHandledExceptionHandler(object sender, UnhandledExceptionEventArgs args) { Exception e = (Exception) args.ExceptionObject; if (e.GetType() == typeof(Health.ApplicationLibrary.RIS.Register.ErrorDataRowException)) { MessageBox.Show(e.Message, "data updating", MessageBoxButtons.OK, MessageBoxIcon.Error); } else { MessageBox.Show("出现未处理错误: " + e.Message, "未处理错误", MessageBoxButtons.OK, MessageBoxIcon.Error); } }

              H Offline
              H Offline
              Heath Stewart
              wrote on last edited by
              #6

              Try Application.ThreadException then. I use the same thing in our program I designed at work but couldn't remember which event I was using. It's been a long time since I wrote that section of the container application. Also, just a helpful tip: try the is keyword instead of obj.GetType() == typeof(SomeClass). It results in fewer IL instructions (better performance) and, IMO, is a little easier to read:

              public static void Main(string[] args)
              {
              Application.ThreadException +=
              new ThreadExceptionEventHandler(UnhandledException);
              Application.Run(new FormRegisterConsole());
              }
               
              private static void UnhandledException(object sender,
              ThreadExceptionEventArgs e)
              {
              if (e.Exception is ErrorDataRowException)
              {
              //...
              }
              else
              {
              //...
              }
              }

              -----BEGIN GEEK CODE BLOCK----- Version: 3.21 GCS/G/MU d- s: a- C++++ UL@ P++(+++) L+(--) E--- W+++ N++ o+ K? w++++ O- M(+) V? PS-- PE Y++ PGP++ t++@ 5 X+++ R+@ tv+ b(-)>b++ DI++++ D+ G e++>+++ h---* r+++ y+++ -----END GEEK CODE BLOCK-----

              W 1 Reply Last reply
              0
              • H Heath Stewart

                Try Application.ThreadException then. I use the same thing in our program I designed at work but couldn't remember which event I was using. It's been a long time since I wrote that section of the container application. Also, just a helpful tip: try the is keyword instead of obj.GetType() == typeof(SomeClass). It results in fewer IL instructions (better performance) and, IMO, is a little easier to read:

                public static void Main(string[] args)
                {
                Application.ThreadException +=
                new ThreadExceptionEventHandler(UnhandledException);
                Application.Run(new FormRegisterConsole());
                }
                 
                private static void UnhandledException(object sender,
                ThreadExceptionEventArgs e)
                {
                if (e.Exception is ErrorDataRowException)
                {
                //...
                }
                else
                {
                //...
                }
                }

                -----BEGIN GEEK CODE BLOCK----- Version: 3.21 GCS/G/MU d- s: a- C++++ UL@ P++(+++) L+(--) E--- W+++ N++ o+ K? w++++ O- M(+) V? PS-- PE Y++ PGP++ t++@ 5 X+++ R+@ tv+ b(-)>b++ DI++++ D+ G e++>+++ h---* r+++ y+++ -----END GEEK CODE BLOCK-----

                W Offline
                W Offline
                wangier
                wrote on last edited by
                #7

                It's OK, Thanks lot! ^|^

                1 Reply Last reply
                0
                Reply
                • Reply as topic
                Log in to reply
                • Oldest to Newest
                • Newest to Oldest
                • Most Votes


                • Login

                • Don't have an account? Register

                • Login or register to search.
                • First post
                  Last post
                0
                • Categories
                • Recent
                • Tags
                • Popular
                • World
                • Users
                • Groups