Solved

DataGridView:  Override AutoGenerateColumns logic

Posted on 2014-03-14
10
128 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
How Do You Stack Up Against Your Peers?

With today’s modern enterprise so dependent on digital infrastructures, the impact of major incidents has increased dramatically. Grab the report now to gain insight into how your organization ranks against your peers and learn best-in-class strategies to resolve incidents.

 

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

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

A long time ago (May 2011), I have written an article showing you how to create a DLL using Visual Studio 2005 to be hosted in SQL Server 2005. That was valid at that time and it is still valid if you are still using these versions. You can still re…
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
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…
Although Jacob Bernoulli (1654-1705) has been credited as the creator of "Binomial Distribution Table", Gottfried Leibniz (1646-1716) did his dissertation on the subject in 1666; Leibniz you may recall is the co-inventor of "Calculus" and beat Isaac…

839 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