I have a DataGridView that I want to use to store generic data. I want to keep a typed data list in the DataGridView class so that all of the sorts, etc. can be handled internally. But I don’t want to have to set the type on the DataGridView since I won’t know the data type until the InitializeData method is called.
public class MyDataGridView : DataGridView {
private List<T> m_data;
public InitializeData<T>(List<T> data) {
m_data = data;
}
... internal events to know when the datagrid wants to sort ...
m_data.Sort<T>(...)
}
Is this possible? If so, how?
If you won’t know the type until you call
InitializeData, then the type clearly can’t be a compile-time part of the object.Do you know everything you need to know about the sorting when you call
InitializeData<T>? If so, how about you do something like:Then when you need to sort later, you can just call
m_sorter().If you might sort on different things, you could potentially change it from an
ActiontoAction<string>or whatever you’d need to be able to sort on.