473,387 Members | 1,282 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

DataGridView as Custom Control with a default column: Code executes twice...

I'm doing a Component class that inherits from DataGridView.
One thing that it does is to add a default column in the grid.
I put the code for this in the class and when I drag the control to my form
i VS2005 it appears just as expected in the designer.
The problem is that when I run the application it adds the column twice so I
get 2 columns in runtime.
The reason is that when I drag my custom control to the form it generates
code i the designer file to add a column.
Then in runtime it runs the code in the forms designer file AND the code in
my Custom class so I get 2 columns...

The problem as I see it is that the drag/drop in VS2005 creates the stuff I
want, but I do not want the code in my class to be run in runtime.

What do I do wrong?

TIA / Claes
Aug 7 '07 #1
3 3270
Her is my code in my Custom Control:

public partial class MyCC : DataGridView
{
public MyCC()
{
InitializeComponent();
System.Windows.Forms.DataGridViewTextBoxColumn m_MyCol = new
System.Windows.Forms.DataGridViewTextBoxColumn();
m_MyCol.HeaderText = "My Col ";
m_MyCol.Name = "MyCol";
m_MyCol.Width = 100;
Columns.Insert(0, m_MyCol);
}
}


Aug 7 '07 #2
Claes Wedin wrote:
[...]
The problem as I see it is that the drag/drop in VS2005 creates the stuff I
want, but I do not want the code in my class to be run in runtime.
Are you sure that's the problem? I mean, it sounds logical, but in fact
the Designer doesn't normally take code-generated stuff and throw it
into the InitializeComponent() method. At least, that hasn't been my
experience.

I don't know for sure what the problem is, but you should definitely
look at the generated InitializeComponent() method for the form
containing your control to see if it really has the same code to add the
column that you've added explicitly in the constructor.

If the Designer is in fact getting confused at copying code-generated
data into the InitializeComponent() method for the control (or for the
containing control, for that matter), then you might be able to work
around the issue by using one of the code attributes defined for working
with the Designer (to disable your column-adding code when your control
is instantiated by the Designer).

Pete
Aug 7 '07 #3
Hi Pete,
Definitly code is created in the designer file when draging the component
from my toolbox to the vindow:
I get all the properties from my components constructor put into the forms
designer file.

I thought I found one workaround is:
1. Drag a standard DataGridView to the form (puts a few standard properties
in the designer file)
2. Change the taype of the control in the forms designer file:
from: this.dataGridView1 = new System.Windows.Forms.DataGridView();
to: this.dataGridView1 = new MyDataGridView();
(and private DataGridView dataGridView1 ;
to private MyDataGridView dataGridView1;)

This seemed to work the way one wants it to:
It looks OK both i n the Form Designer window AND in runtime...

But if you do anything with the control in the form (change the size) it
copies all properties from from the component into the forms designer file
and you are back where you started...

//Claes

Aug 8 '07 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

6
by: dbuchanan | last post by:
Hello, Is this a bug? Is there some kind of work around? I want to add default values for a few columns in my datagridview I found the "DefaultValuesNeeded" event for the datagridview I...
3
by: Bob | last post by:
Thinking two things, 1- Creating a userControl -yeah you guessed it, a multi column drop down combobox - I've looked at several articles and did not find what I need, one that's bindable and that...
2
by: Bob | last post by:
How do I show a calendar control or another appropriate control to pick a date and time for a cell in a column in a datagridview? Thanks for any help. Bob
7
by: Mitchell S. Honnert | last post by:
Is there an equivalent of the DataGrid's DataGridTableStyle for the DataGridView? If not, is there an easy way to duplicate the DataGridTableStyle's functionality for the DataGridView? Here's...
0
by: Mike | last post by:
Hey everyone... I've got three problems with a custom DataGridView column I've built following the "How To: Host Controls in Windows Forms DataGridView Cells" article. The base editing control...
7
by: Ryan | last post by:
I have a DataGridView which displays numeric (Int32) data from an underlying database. I want the numbers to be displayed in numeric format "#,###" (with commas). I want to also limit the user so...
0
by: RickH | last post by:
Has anyone tried wrapping the DataGridView control itself as a custom Cell/Column control. So that I can have a child DataGridView appear in a column of a parent DataGridView so that the...
6
by: Miro | last post by:
Sorry for the cross post. I am stuck. I have a datagridview for poker rounds. Basically there are 3 columns in this datagridview. "Round" "SmallBlind" "BigBlind" I have an issue when I tab...
0
by: priyamtheone | last post by:
I'm trying to make a datagridview column to act like a datetimepicker column (C#.Net 2005). These are the behaviours that the dgv should have: 1) Initially all the cells of the dtp column should be...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.