Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Initialize custom DataGridView

Posted on 2006-06-26
10
Medium Priority
?
1,631 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
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
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

Vote for the Most Valuable Expert

It’s time to recognize experts that go above and beyond with helpful solutions and engagement on site. Choose from the top experts in the Hall of Fame or on the right rail of your favorite topic page. Look for the blue “Nominate” button on their profile to vote.

Question has a verified solution.

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

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 …
Parsing a CSV file is a task that we are confronted with regularly, and although there are a vast number of means to do this, as a newbie, the field can be confusing and the tools can seem complex. A simple solution to parsing a customized CSV fi…
Want to learn how to record your desktop screen without having to use an outside camera. Click on this video and learn how to use the cool google extension called "Screencastify"! Step 1: Open a new google tab Step 2: Go to the left hand upper corn…
This lesson discusses how to use a Mainform + Subforms in Microsoft Access to find and enter data for payments on orders. The sample data comes from a custom shop that builds and sells movable storage structures that are delivered to your property. …
Suggested Courses

877 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