Solved

Initialize custom DataGridView

Posted on 2006-06-26
10
1,484 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
 
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
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 
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

Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

Join & Write a Comment

Well, all of us have seen the multiple EXCEL.EXE's in task manager that won't die even if you call the .close, .dispose methods. Try this method to kill any excels in memory. You can copy the kill function to create a check function and replace the …
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…
This tutorial demonstrates a quick way of adding group price to multiple Magento products.
This video demonstrates how to create an example email signature rule for a department in a company using CodeTwo Exchange Rules. The signature will be inserted beneath users' latest emails in conversations and will be displayed in users' Sent Items…

706 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

17 Experts available now in Live!

Get 1:1 Help Now