How to create custom exception handler for custom controls or extension methods.
Posted
by Shantanu Gupta
on Stack Overflow
See other posts from Stack Overflow
or by Shantanu Gupta
Published on 2010-04-24T06:23:19Z
Indexed on
2010/04/24
6:33 UTC
Read the original article
Hit count: 287
I am creating an extension method in C# to retrieve some value from datagridview. Here if a user gives column name that doesnot exists then i want this function to throw an exception that can be handled at the place where this function will be called. How can i achieve this.
public static T Value<T>(this DataGridView dgv, int RowNo, string ColName)
{
if (!dgv.Columns.Contains(ColName))
throw new ArgumentException("Column Name " + ColName + " doesnot exists in DataGridView.");
return (T)Convert.ChangeType(dgv.Rows[RowNo].Cells[ColName].Value, typeof(T));
}
© Stack Overflow or respective owner