Solved

DataGridView:  Override AutoGenerateColumns logic

Posted on 2014-03-14
10
123 Views
Last Modified: 2016-05-09
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.
0
Comment
Question by:Chad Penney
  • 5
  • 4
10 Comments
 
LVL 96

Expert Comment

by:Bob Learned
ID: 39931296
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
0
 
LVL 96

Expert Comment

by:Bob Learned
ID: 39931301
And, then again, you want to change the auto-generate behavior...
0
 
LVL 96

Expert Comment

by:Bob Learned
ID: 39931374
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.
0
DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

 

Author Comment

by:Chad Penney
ID: 39931401
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.
0
 
LVL 96

Expert Comment

by:Bob Learned
ID: 39931527
Are you removing columns, and adding your own in OnDataSourceChanged?
0
 

Author Comment

by:Chad Penney
ID: 39931774
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.
0
 
LVL 96

Expert Comment

by:Bob Learned
ID: 39931959
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.
0
 

Accepted Solution

by:
Chad Penney earned 0 total points
ID: 39942308
So far this method seems to be working and I haven't run into any issues. If anyone is looking to do something similar here is my code:

   
Protected Overrides Sub OnDataSourceChanged(e As EventArgs)
            Me.Columns.Clear()

            For Each DC As DataColumn In DirectCast(DataSource, DataTable).Columns
                Dim C As DataGridViewColumn

                If 0 = String.Compare(DC.DataType.ToString().Trim(), "System.Byte[]") Then

                    If _ShowImagesAsButtons Then
                        C = New GridViewImageButtonColumn
                    Else
                        C = New DataGridViewImageColumn
                        C.DefaultCellStyle.NullValue = Nothing
                    End If

                ElseIf DC.DataType Is System.Type.GetType("System.String") Then
                    C = New DataGridViewTextBoxColumn
                ElseIf DC.DataType Is System.Type.GetType("System.Int32") Then
                    C = New GridViewNumericColumn
                ElseIf DC.DataType Is System.Type.GetType("System.Int16") Then
                    C = New GridViewNumericColumn
                ElseIf DC.DataType Is System.Type.GetType("System.Decimal") Then
                    C = New GridViewNumericColumn
                ElseIf DC.DataType Is System.Type.GetType("System.DateTime") Then
                    C = New DataGridViewTextBoxColumn
                    C.DefaultCellStyle.Format = "M/d/yy h:mm:ss tt"
                ElseIf DC.DataType Is System.Type.GetType("System.TimeSpan") Then
                    C = New DataGridViewTextBoxColumn
                    C.DefaultCellStyle.Format = "M/d/yy h:mm:ss tt"
                ElseIf DC.DataType Is System.Type.GetType("System.Boolean") Then
                    C = New DataGridViewCheckBoxColumn
                Else
                    C = New DataGridViewTextBoxColumn
                End If

                C.DataPropertyName = DC.ColumnName
                C.HeaderText = DC.Caption

                Me.Columns.Add(C)

            Next

        End Sub

Open in new window

0
 

Author Closing Comment

by:Chad Penney
ID: 41584571
It was the best option
0

Featured Post

Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

For those of you who don't follow the news, or just happen to live under rocks, Microsoft Research released a beta SDK (http://www.microsoft.com/en-us/download/details.aspx?id=27876) for the Xbox 360 Kinect. If you don't know what a Kinect is (http:…
Wouldn’t it be nice if you could test whether an element is contained in an array by using a Contains method just like the one available on List objects? Wouldn’t it be good if you could write code like this? (CODE) In .NET 3.5, this is possible…
Established in 1997, Technology Architects has become one of the most reputable technology solutions companies in the country. TA have been providing businesses with cost effective state-of-the-art solutions and unparalleled service that is designed…
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…

831 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question