我正在 C# 中创建一个扩展方法来从 datagridview 中检索一些值。在这里,如果用户给出的列名不存在,那么我希望此函数抛出一个异常,该异常可以在调用此函数的地方进行处理。我怎样才能做到这一点。
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));
}