Link to home
Start Free TrialLog in
Avatar of Chad Penney
Chad PenneyFlag for United States of America

asked on

DataGridView: Override AutoGenerateColumns logic

I'm working on a custom DataGridView control because I want to create a couple new column types, This is used as part of a debugging and diagnostic tool so I have no idea what the resulting datatables that will be bound to the DataGridView will be at run time.  

I think the best way to do this is to override the logic that creates those columns when the datasource is assigned to the control. I just haven't been able to find any examples of how to pull this off.  

I have a feeling I need to override the OnDataBindingComplete or OnDataSourceChanged methods but I'm not sure if I'm on the right track and where to start.
Avatar of Bob Learned
Bob Learned
Flag of United States of America image

I believe that this example shows what you would need to do:

Build a Custom NumericUpDown Cell and Column for the DataGridView Control
http://msdn.microsoft.com/en-us/library/aa730881(v=vs.80).aspx
And, then again, you want to change the auto-generate behavior...
With some research, I am not sure if you can override the default auto-generate process. There are a few private methods used to generate columns.
Avatar of Chad Penney

ASKER

Yeah I already figured out how to create the new column and cell types. It's how to have them applied automatically when a datasource is assigned that I'm running into difficulty. I've done some experimenting with overriding the OnDataSourceChanged method and had some luck but I'm not sure if this is the best way to go.
Are you removing columns, and adding your own in OnDataSourceChanged?
So far yes. I call a DataGridView.Clear() Then check the DataType of each datasource column and create each of the columns and not calling the base class OnDataSourceChanged if the datasource is a datatable. If not I just call the OnDataSourceChanged method of thebase class.

So far it seems to be working OK but I'm not sure what problems I might run into later or like I said if there is a better way to be doing this.
I would have to say that this is a non-standard requirement for the DataGridView, but go with it, if it gets the job done--I can't think of a better solution.
ASKER CERTIFIED SOLUTION
Avatar of Chad Penney
Chad Penney
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
It was the best option