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));
}
Is hard to understand your question, but it sounds like you are wanting to throw an exception and handle it where you call your extension method. If so, you are almost there. You are already throwing the exception, just put a
try/catchblock around the callsite.Is this what you are looking for?