Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Initialize custom DataGridView

Posted on 2006-06-26
10
Medium Priority
?
1,597 Views
Last Modified: 2008-01-09
I have an extended version of a DataGridView control which I want by default to instantiate with 7 columns and 5 rows.

Partial Public Class CustomDataGridView

    Public Sub New()
        MyBase.New()

        ' This call is required by the Windows Form Designer.
        InitializeComponent()

        ' Add any initialization after the InitializeComponent() call
        Me.RowCount = 5
        Me.ColumnCount = 7

    End Sub

End Class

When I drag and drop the control on a blank form I indeed get a 7x5 grid but if I hit run I get 14 columns with 5 rows and the 7 new columns. Obviously I am doing something wrong but I can't figure out what.

BTW - I tried Columns.Add("","") also with virtually the same result.
0
Comment
Question by:tmostad
[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
  • 6
  • 4
10 Comments
 
LVL 96

Expert Comment

by:Bob Learned
ID: 16987635
Me.AutoGenerateColumns = False

Bob
0
 
LVL 1

Author Comment

by:tmostad
ID: 16987906
I added Me.AutoGenerateColumns = False right before Me.RowCount = 5 but unfortunately there was no difference. I am still getting 14 columns.
0
 
LVL 96

Expert Comment

by:Bob Learned
ID: 16988031
If you have already defined the columns, then remove Me.ColumnCount = 7

Bob
0
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
LVL 1

Author Comment

by:tmostad
ID: 16988052
BTW - I should add that my grid is being used in unbound mode and it appears that AutoGenerateColumns affects whether columns are created in a bound DataGridView. It is actually irrelevent in any case given that this is in the initialize code for the control before it could (or at least would) be bound to data anyway. Any other ideas?
0
 
LVL 96

Expert Comment

by:Bob Learned
ID: 16988100
1) Did you add any columns through the designer?

2) You could clear columns:
   Me.Columns.Clear()
   Me.ColumnCount = 7
   Me.RowCount = 5

3) I came up with a custom class, with this code, and I only get 7 columns and 5 rows.

Bob
0
 
LVL 1

Author Comment

by:tmostad
ID: 16988105
I haven't defined any columns. They get created when the control is dropped from the toolbox onto the form then get added again at some point later. If I remove them from the constructor then when I drop the control on a form I get just one column then I run the app and get two columns. I am beginning to think that when I add rows it automatically adds the columns again because if I remove the Me.ColumnCount=7 then I automatically get 5 rows and one column when I droip the control on the form then I get 5 rows and 2 columns when I run the app.
0
 
LVL 1

Author Comment

by:tmostad
ID: 16988144
Bob,

Could you check the result again? I am getting 8 columns and 5 rows. The 8th column has a header that says "DataGridViewTextBox". Are you seeing the same thing?
0
 
LVL 96

Accepted Solution

by:
Bob Learned earned 2000 total points
ID: 16988302
Try it in the OnHandleCreated method, instead of the New, because the ColumnCount = 0 in New, so ColumnCount = 7, and then columns are added from the designer.

Protected Overrides Sub OnHandleCreated(ByVal e As System.EventArgs)
   MyBase.OnHandleCreated(e)
   Me.Columns.Clear()
   Me.Rows.Clear()
   Me.ColumnCount = 7
   Me.RowCount = 5
End Sub

Bob
0
 
LVL 1

Author Comment

by:tmostad
ID: 16988448
Thanks. That works great! It seems like it should also work the other way but apparently the way I am trying to use the control was not envisioned. In fact it is a bit annoying that DataGridView doesn't work symmetrically (i.e. the ability simply interchange row and column in any instance). I realize that the designers were thinking mainly of bound applications but the old DataGrid works better than its newer cousin for unbound applications. Since it is not recommended for new apps, which this one is, I will stick with DataGridView but it takes some thought sometimes to switch between the way you handle a row and a column. Thanks again.
0
 
LVL 1

Author Comment

by:tmostad
ID: 17014191
Actually I never did get it to work properly. It appears that the IDE executes the code in OnHandleCreated so it can paint the control. When it does this it adds some initialization code in the designer created portion that caused problems. I could manually delete it and it would work until I rebuilt the app. I finally moved the initializtion code into an initialization subroutine which I call in the app that uses the control. This means that I can't see an accurate representation of the control in the IDE but it works OK at run-time. Given that this control is for my use only this is acceptable.
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

1.0 - Introduction Converting Visual Basic 6.0 (VB6) to Visual Basic 2008+ (VB.NET). If ever there was a subject full of murkiness and bad decisions, it is this one!   The first problem seems to be that people considering this task of converting…
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…
Visualize your data even better in Access queries. Given a date and a value, this lesson shows how to compare that value with the previous value, calculate the difference, and display a circle if the value is the same, an up triangle if it increased…
Have you created a query with information for a calendar? ... and then, abra-cadabra, the calendar is done?! I am going to show you how to make that happen. Visualize your data!  ... really see it To use the code to create a calendar from a q…
Suggested Courses

722 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