Solved

DataGridView:  Override AutoGenerateColumns logic

Posted on 2014-03-14
10
106 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
 

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
Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

 
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

What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

This article describes relatively difficult and non-obvious issues that are likely to arise when creating COM class in Visual Studio and deploying it by professional MSI-authoring tools. It is assumed that the reader is already familiar with the cla…
In my previous two articles we discussed Binary Serialization (http://www.experts-exchange.com/A_4362.html) and XML Serialization (http://www.experts-exchange.com/A_4425.html). In this article we will try to know more about SOAP (Simple Object Acces…
Internet Business Fax to Email Made Easy - With eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, fr…
This video discusses moving either the default database or any database to a new volume.

708 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

Need Help in Real-Time?

Connect with top rated Experts

15 Experts available now in Live!

Get 1:1 Help Now