Solved

Initialize custom DataGridView

Posted on 2006-06-26
10
1,508 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
  • 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
Courses: Start Training Online With Pros, Today

Brush up on the basics or master the advanced techniques required to earn essential industry certifications, with Courses. Enroll in a course and start learning today. Training topics range from Android App Dev to the Xen Virtualization Platform.

 
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 500 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

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

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

Suggested Solutions

This tutorial demonstrates one way to create an application that runs without any Forms but still has a GUI presence via an Icon in the System Tray. The magic lies in Inheriting from the ApplicationContext Class and passing that to Application.Ru…
Microsoft Reports are based on a report definition, which is an XML file that describes data and layout for the report, with a different extension. You can create a client-side report definition language (*.rdlc) file with Visual Studio, and build g…
This is used to tweak the memory usage for your computer, it is used for servers more so than workstations but just be careful editing registry settings as it may cause irreversible results. I hold no responsibility for anything you do to the regist…
In this video I am going to show you how to back up and restore Office 365 mailboxes using CodeTwo Backup for Office 365. Learn more about the tool used in this video here: http://www.codetwo.com/backup-for-office-365/ (http://www.codetwo.com/ba…

815 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

8 Experts available now in Live!

Get 1:1 Help Now