Solved

Initialize custom DataGridView

Posted on 2006-06-26
10
1,519 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
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.

 
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

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone 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

Suggested Solutions

If you're writing a .NET application to connect to an Access .mdb database and use pre-existing queries that require parameters, you've come to the right place! Let's say the pre-existing query(qryCust) in Access takes a Date as a parameter and l…
A while ago, I was working on a Windows Forms application and I needed a special label control with reflection (glass) effect to show some titles in a stylish way. I've always enjoyed working with graphics, but it's never too clever to re-invent …
Two types of users will appreciate AOMEI Backupper Pro: 1 - Those with PCIe drives (and haven't found cloning software that works on them). 2 - Those who want a fast clone of their boot drive (no re-boots needed) and it can clone your drive wh…
Established in 1997, Technology Architects has become one of the most reputable technology solutions companies in the country. TA have been providing businesses with cost effective state-of-the-art solutions and unparalleled service that is designed…

809 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