I have a method TextFilter(object e)
that asks me for an object of any type as a parameter. The objects I'm passing both of them DataGridViewCellParsingEventArgs
and DataGridViewCellEventArgs
they have almost the same properties. The ones that interest me are ColumnIndex
and RowIndex
.
Here is the code...
private bool TextFilter(object e)
{
DataTable dt = this.connection_Manager.DefaultProyectos;
DataView dv = new DataView(dt);
try
{
//Error elemento no contenido en e
string value = dgvCBD[e.ColumnIndex, e.RowIndex].Value.ToString(), column = dt.Columns[1].ColumnName;
dv.RowFilter = $"[{column}] = '{value}'";
}
catch (Exception ex)
{
MessageBox.Show(ex.Message + ex.StackTrace);
}
return (dv.Count == 0) ? true : false;
}
Is there any way to do this?
I assume that what you want to do is work with "duck typing": instead of taking into account the type of the object, you simply see if it has a property or method that you are interested in and use it.
C# allows that through Reflection. For example, to increment an integer property called "columnIndex" by 1:
Suggested reading:
https://msdn.microsoft.com/en-us/library/hh194385(v=vs.110).aspx
https://msdn.microsoft.com/en-us/library/hh194291(v=vs.110).aspx
You can try to cast the object to each of the types and, if successful, get the value of the properties: