|
-
Jul 26th, 2012, 08:14 PM
#1
Thread Starter
Lively Member
Inherited DataGridView Column Double Up
I am creating a little timesheet app, and I have created my own custom datagridview. I have got it all setup and it looks fine when I first add the control to the form.
When I debug the app for the first time after adding, I get a double up of columns. Does anyone know what causes this?
-
Jul 26th, 2012, 08:44 PM
#2
Re: Inherited DataGridView Column Double Up
Are the columns added in code? Have you actually checked whether the code is executed twice?
-
Apr 25th, 2013, 03:20 PM
#3
Hyperactive Member
Re: Inherited DataGridView Column Double Up
I'm having this same problem.
In my app, Columns are manually added in the New() sub.
In design view, it has the right number of columns, but when I run the app, it doubles up the columns.
Does vb run the New() sub twice?
I even added Columns.Clear in the New() sub, in case the New() sub was being run twice, but that didn't fix it.
The form designer code shows only the 1st set of columns.
-
Apr 25th, 2013, 03:56 PM
#4
Hyperactive Member
Re: Inherited DataGridView Column Double Up
Perhaps this is part of the problem?
The original URL is missing and I don't understand what needs to be done to fix this problem.
-
Apr 25th, 2013, 04:47 PM
#5
Re: Inherited DataGridView Column Double Up
Nope. If there was a problem with inheriting DataGridView then it's been fixed. I'm not getting any problems with it so it's gotta be in your code somewhere. I'd suggest you show us at least you New Sub!
As the 6-dimensional mathematics professor said to the brain surgeon, "It ain't Rocket Science!"
Reviews: "dunfiddlin likes his DataTables" - jmcilhinney
Please be aware that whilst I will read private messages (one day!) I am unlikely to reply to anything that does not contain offers of cash, fame or marriage!
-
Apr 25th, 2013, 05:03 PM
#6
Hyperactive Member
Re: Inherited DataGridView Column Double Up
 Originally Posted by dunfiddlin
Nope. If there was a problem with inheriting DataGridView then it's been fixed. I'm not getting any problems with it so it's gotta be in your code somewhere. I'd suggest you show us at least you New Sub!
That's good news. Probably something stupid on my end then...
VB.NET Code:
Public Sub New()
InitializeComponent()
VirtualMode = True
Dim HexColumns(17) As DataGridViewTextBoxColumn
For i As Integer = 0 To 17
HexColumns(i) = New DataGridViewTextBoxColumn
With HexColumns(i)
.HeaderText = Hex(i - 1)
.Width = 25
.SortMode = DataGridViewColumnSortMode.NotSortable
.Name = .HeaderText
End With
Next
Columns.Clear()
Columns.AddRange(HexColumns)
With Columns(0)
.HeaderText = "Addr"
.HeaderCell.Style.Alignment = DataGridViewContentAlignment.MiddleCenter
.Width = 75
End With
With Columns(17)
.HeaderText = "Text"
.HeaderCell.Style.Alignment = DataGridViewContentAlignment.MiddleCenter
.Width = 150
End With
End Sub
-
Apr 25th, 2013, 06:02 PM
#7
Hyperactive Member
Re: Inherited DataGridView Column Double Up
This sounds like the same problem, but I don't understand the solution.
Another link to another solution
If I add "If Me.DesignMode Then" to the column creation section of the New() Sub, no columns get created at designtime or runtime.
If I add "If Not Me.DesignMode Then" to the column creation section, columns get created twice.
Interestingly, the extra columns don't have the alignment set as I set them in the New() Sub.
First pic is design mode
2nd is running the program. Note the alignment of the HeaderText...
Last edited by Meestor_X; Apr 25th, 2013 at 06:32 PM.
-
Apr 25th, 2013, 06:36 PM
#8
Re: Inherited DataGridView Column Double Up
What happens if you delete InitializeComponent() from the New sub?
As the 6-dimensional mathematics professor said to the brain surgeon, "It ain't Rocket Science!"
Reviews: "dunfiddlin likes his DataTables" - jmcilhinney
Please be aware that whilst I will read private messages (one day!) I am unlikely to reply to anything that does not contain offers of cash, fame or marriage!
-
Apr 25th, 2013, 06:50 PM
#9
Hyperactive Member
Re: Inherited DataGridView Column Double Up
 Originally Posted by dunfiddlin
What happens if you delete InitializeComponent() from the New sub?
I get the warning:
"Warning 1 'Public Sub New()' in designer-generated type 'AndysControls.HexDataGridView' should call InitializeComponent method."
It also doesn't fix the problem. 
Here's another discussion of this problem.
Did you have a look at the links I posted?
You said earlier:
Nope. If there was a problem with inheriting DataGridView then it's been fixed. I'm not getting any problems with it so it's gotta be in your code somewhere.
Are you able to make an inherited DataGridView with programmatically-created columns that doesn't show the problem I and the OP are experiencing?
If so, can you show me your code so I can see where I'm going wrong?
Last edited by Meestor_X; Apr 25th, 2013 at 06:58 PM.
-
Apr 25th, 2013, 07:02 PM
#10
Re: Inherited DataGridView Column Double Up
I can but I haven't worked it up to a full custom control, just as an additional class instanced at runtime. If you can zip up your control project and attach it here, I'll be happy to see what can be done.
As the 6-dimensional mathematics professor said to the brain surgeon, "It ain't Rocket Science!"
Reviews: "dunfiddlin likes his DataTables" - jmcilhinney
Please be aware that whilst I will read private messages (one day!) I am unlikely to reply to anything that does not contain offers of cash, fame or marriage!
-
Apr 26th, 2013, 01:49 PM
#11
Hyperactive Member
Re: Inherited DataGridView Column Double Up
Ok, got it sorted out. It sure was a difficult solution to find!
Had to add:
vb.net Code:
<System.ComponentModel.DesignerSerializationVisibilityAttribute(System.ComponentModel.DesignerSerializationVisibility.Hidden)> _
Overloads ReadOnly Property Columns As DataGridViewColumnCollection
Get
Return MyBase.Columns
End Get
End Property
In my class that inherits the DataGridView.
Still the designer (re)creates all the columns, but at least it doesn't add them to the DataGridView.
I'm not sure if there would be a way to use this attribute to also supress the duplication of the Columns in the designer, just to really completely correct this problem...
WHEW!
-
Apr 26th, 2013, 01:55 PM
#12
Re: Inherited DataGridView Column Double Up
put a break point on this line:
Columns.Clear()
BEFORE it runs... check to see what the Columns.Count is...
then run that ONE line... go no further...
before this line:
Columns.AddRange(HexColumns)
again check to see what the Columns.Count is... Something tells me that it hasn't changed... clearing the columns does NOT (if I remember right) REMOVE the columns... it jsut clears them out, sets the headers to empty, removes databindings, data, etc...
It's like using Textbox.Clear... doesn't actually remove the textbox.. it just clears it out.
If you want to remove the existing columns, then that's what you need to do... REMOVE the columns... THEN add the new ones.
-tg
-
Apr 26th, 2013, 02:05 PM
#13
Hyperactive Member
Re: Inherited DataGridView Column Double Up
Please see the previous post AND all my links.
The problem is the designer is creating the columns and adding them in addition to me programmatically adding the columns.
My previous post shows the solution which works.
-
Apr 26th, 2013, 02:30 PM
#14
Re: Inherited DataGridView Column Double Up
I did read all of the posts... and I keyed in on this:
Still the designer (re)creates all the columns, but at least it doesn't add them to the DataGridView.
which tells me the problem is still there but you've managed to gloss over it... fine, what ever. your project.
-tg
-
Apr 26th, 2013, 02:35 PM
#15
Hyperactive Member
Re: Inherited DataGridView Column Double Up
 Originally Posted by techgnome
which tells me the problem is still there but you've managed to gloss over it... fine, what ever. your project.
Ok, I'll bite.
Your solution says to remove the columns and that Columns.Clear() does not do this. So what would actually REMOVE the columns then?
If you look at the form1.designer file, you can see where it recreates all the columns as I was saying. The only way I've found to stop this is my solution, but I'm open to other suggestions...
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|