Solved

DataGridView:  Override AutoGenerateColumns logic

Posted on 2014-03-14
10
141 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

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: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering 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

More often than not, we developers are confronted with a need: a need to make some kind of magic happen via code. Whether it is for a client, for the boss, or for our own personal projects, the need must be satisfied. Most of the time, the Framework…
The ECB site provides FX rates for major currencies since its inception in 1999 in the form of an XML feed. The files have the following format (reducted for brevity) (CODE) There are three files available HERE (http://www.ecb.europa.eu/stats/exch…
This is a high-level webinar that covers the history of enterprise open source database use. It addresses both the advantages companies see in using open source database technologies, as well as the fears and reservations they might have. In this…
If you're a developer or IT admin, you’re probably tasked with managing multiple websites, servers, applications, and levels of security on a daily basis. While this can be extremely time consuming, it can also be frustrating when systems aren't wor…

691 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