Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations biv343 on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Error Handling Help 1

Status
Not open for further replies.

jtrembla

ISP
Jul 6, 2006
104
US
I have about 10 grids that use the same error handling code below. Currenly this code is behind all of the grids. Is there a better place to have this code, maybe in a class, and then reference it that way? How would I do this? Kinda new to C#. P.S. I am using winforms if that makes a difference.


Code:
private void ultraGridPop_Error(object sender, Infragistics.Win.UltraWinGrid.ErrorEventArgs e)
        {
            // If there is an error while cutting, copying, or pasting, this 
            // event will fire and e.ErrorType will be MultiCellOperation.
            if (e.ErrorType == ErrorType.MultiCellOperation)
            {
                // Cancel the default error message
                e.Cancel = true;
                
                // e.MultiCellOperationErrorInfo returns the error information.
                // Build a custom error message for the user.
                System.Text.StringBuilder SB = new System.Text.StringBuilder();

                SB.AppendFormat("An error has occurred during the {0} operation", e.MultiCellOperationErrorInfo.Operation.ToString());
                SB.Append(Environment.NewLine);
                SB.Append("The error was as follows:");
                SB.Append(Environment.NewLine);
                SB.Append(Environment.NewLine);
                SB.AppendFormat("\"{0}\"", e.ErrorText);

                // Get the cell on which the error occurred
                UltraGridCell errorCell = e.MultiCellOperationErrorInfo.ErrorCell;

                // The ErrorCell will be null if the error was an error in the whole
                // operation as oppossed to an error no a single cell.
                if (errorCell != null)
                {
                    int rowIndex = errorCell.Row.Index;
                    string columnCaption = errorCell.Column.Header.Caption;

                    SB.Append(Environment.NewLine);
                    SB.AppendFormat("The error occurred on Row {0} in Column {1}", rowIndex, columnCaption);
                }

                MessageBox.Show(this, SB.ToString());
                
                
            }
            
        }
        #endregion
 
Code:
public class UltraWinGridErrorHandler
{
        public static void Error(object sender, ErrorEventArgs e)
        {
            // If there is an error while cutting, copying, or pasting, this 
            // event will fire and e.ErrorType will be MultiCellOperation.
            if (e.ErrorType == ErrorType.MultiCellOperation)
            {
                // Cancel the default error message
                e.Cancel = true;
                
                // e.MultiCellOperationErrorInfo returns the error information.
                // Build a custom error message for the user.
                System.Text.StringBuilder SB = new System.Text.StringBuilder();

                SB.AppendFormat("An error has occurred during the {0} operation", e.MultiCellOperationErrorInfo.Operation.ToString());
                SB.Append(Environment.NewLine);
                SB.Append("The error was as follows:");
                SB.Append(Environment.NewLine);
                SB.Append(Environment.NewLine);
                SB.AppendFormat("\"{0}\"", e.ErrorText);

                // Get the cell on which the error occurred
                UltraGridCell errorCell = e.MultiCellOperationErrorInfo.ErrorCell;

                // The ErrorCell will be null if the error was an error in the whole
                // operation as oppossed to an error no a single cell.
                if (errorCell != null)
                {
                    int rowIndex = errorCell.Row.Index;
                    string columnCaption = errorCell.Column.Header.Caption;

                    SB.Append(Environment.NewLine);
                    SB.AppendFormat("The error occurred on Row {0} in Column {1}", rowIndex, columnCaption);
                }

                MessageBox.Show(this, SB.ToString());
                
                
            }
            
        }
}
now for each grid register this static function to grid
Code:
MyGrid1.Error += UltraWinGridErrorHandler.Error;
MyGrid2.Error += UltraWinGridErrorHandler.Error;
...

Jason Meckley
Programmer
Specialty Bakers, Inc.
 
thanks. quick question, where do I register the static function, in the grid error event?

I will give u a star.
 
put the class above in it's own file. then when the grid is initialized assign the error event referencing the class's static function.

Jason Meckley
Programmer
Specialty Bakers, Inc.
 
I am also getting this error.

Error 1 The type or namespace name 'ErrorEventArgs' could not be found (are you missing a using directive or an assembly reference?)
 
I put the grid in its class, but too many errors are happening..

Error 1 No overload for method 'Error' takes '0' arguments C:\Documents and Settings\pryan\My Documents\Visual Studio 2005\Projects\Market Forecaster\Market Forecaster\frm_MF_Population.cs 163 40 MarketForecaster
Error 2 Keyword 'this' is not valid in a static property, static method, or static field initializer C:\Documents and Settings\pryan\My Documents\Visual Studio 2005\Projects\Market Forecaster\Market Forecaster\BOL\UltraWinGridErrorHandler.cs 45 33 MarketForecaster
Error 3 The best overloaded method match for 'System.Windows.Forms.MessageBox.Show(string, string)' has some invalid arguments C:\Documents and Settings\pryan\My Documents\Visual Studio 2005\Projects\Market Forecaster\Market Forecaster\BOL\UltraWinGridErrorHandler.cs 45 17 MarketForecaster
Error 4 Argument '1': cannot convert from 'MarketForecaster.BOL.UltraWinGridErrorHandler' to 'string' C:\Documents and Settings\pryan\My Documents\Visual Studio 2005\Projects\Market Forecaster\Market Forecaster\BOL\UltraWinGridErrorHandler.cs 45 33 MarketForecaster
 
I just had to remove this from the messagebox.show

All set, thanks.
 
replace [tt]MessageBox.Show(this, SB.ToString());[/tt]
with[tt]MessageBox.Show(sender, SB.ToString());[/tt]


Jason Meckley
Programmer
Specialty Bakers, Inc.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top