Question

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));
    }
Was it helpful?

Solution

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/catch block around the callsite.

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));
}

// Wherever you call the method:
try
{
    dataGridView.Value(rowNumber, columnName);
}
catch (ArgumentException)
{
    // caught the exception
}

Is this what you are looking for?

OTHER TIPS

This is not the right way to do this. First off, there is nothing exceptional about a user mis-typing something. Secondly, this extension method is liable to be called deeply nested in some kind of code that works with, say, the database. You have to catch the exception, since a typing mistake is normal. But you now also have the burden of writing the exception handler and a bunch of code that properly restores the state of the program.

Validating user input should happen early, before you set off a train of code that is hard to stop. And there's no reason to use exceptions in user input validation, a simple if() statement gets the job done.

You can now leave the throw statement in place, if you wish, it does make for a better diagnostic. But you should never handle that exception because now it diagnoses a bug in your code. And you can't fix bugs with catch clauses.

Though the question is already answered, i would just suggest a second solution. The idea of throwing exception should be the last option. You can achieve the same using the below approach.

public static bool TryGetValue<T>(this DataGridView dgv, int RowNo, 
    string ColName, out T cellValue)
{
    cellValue = default(T);
    if (!dgv.Columns.Contains(ColName))
        return false;
    cellValue = (T)Convert.ChangeType(dgv.Rows[RowNo].Cells[ColName].Value, typeof(T));
    return true;
}

public static void Main(){
int desiredValue;
if(dataGridView.TryGetValue<int>(rowNumber, columnName, out desiredValue)){
    //Use the value
}
else{
    //Value can not be retrieved.
}
}

PS: i haven't typed this code on editor, so please excuse any typos.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top