?
Solved

DataGridView:  Override AutoGenerateColumns logic

Posted on 2014-03-14
10
Medium Priority
?
147 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

Technology Partners: 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!

Question has a verified solution.

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

Today I had a very interesting conundrum that had to get solved quickly. Needless to say, it wasn't resolved quickly because when we needed it we were very rushed, but as soon as the conference call was over and I took a step back I saw the correct …
Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
Do you want to know how to make a graph with Microsoft Access? First, create a query with the data for the chart. Then make a blank form and add a chart control. This video also shows how to change what data is displayed on the graph as well as form…
In this video, Percona Solutions Engineer Barrett Chambers discusses some of the basic syntax differences between MySQL and MongoDB. To learn more check out our webinar on MongoDB administration for MySQL DBA: https://www.percona.com/resources/we…
Suggested Courses

762 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