I created a DataGridView and set its properties as I wanted.
Problem is that I can have four same-look DataGridView in application , depending on some conditions in my app. I want to know if I could create one instance of DataGridView programmatically and copy all properties from the original DataGridView, so that I have 2,3,4 same DataGridViews?
I know that it is possible via the designer but how to copy it programatically?
I heard about extension methods but is there some easier approach?
EDIT:
i added new method:
private void copyControl(Control sourceControl, Control targetControl)
{
// make sure these are the same
if (sourceControl.GetType() != targetControl.GetType())
{
throw new Exception("Incorrect control types");
}
foreach (PropertyInfo sourceProperty in sourceControl.GetType().GetProperties())
{
object newValue = sourceProperty.GetValue(sourceControl, null);
MethodInfo mi = sourceProperty.GetSetMethod(true);
if (mi != null)
{
sourceProperty.SetValue(targetControl, newValue, null);
}
}
}
DataGridView dgw = new DataGridView();
copyControl(PartsDgv, dgw);