Adding a row to a DataGridView

  • Thread starter Thread starter Dom
  • Start date Start date
D

Dom

I'm just playing around, to confirm my mental picture of what is
happening in CSharp. Looks like I got the wrong mental picture,
because I thought this would work.

I created a class, MyDGVR, which extends the DataGridViewRow, and just
added a little functionality by adding a property called NewProperty.
Then I did the following:

MyDGVR r = new MyDGVW (1, 2, 3);
DataGridView.Rows.Add (r);
r.NewProperty = 2;

It seems the Add method called the MyDGVW parameterless constructor.
I really wasn't expecting that. Am I going about this the right way?

Thanks, Dom
 
[...]
MyDGVR r = new MyDGVW (1, 2, 3);
DataGridView.Rows.Add (r);
r.NewProperty = 2;

It seems the Add method called the MyDGVW parameterless constructor.

Why do you say that? Since the instance is already constructed, it
shouldn't be calling any constructor at all. What makes you think that
calling Add() executed a constructor?
I really wasn't expecting that. Am I going about this the right way?

I don't know. But a more complete code sample would probably help. Also,
elaborating on what exactly you're seeing and why you are interpreting it
the way you are, that would help as well (maybe even more than a more
complete code sample).

Pete
 
When I step through the code, using "Step into", I can see that the
line "DataGridView.Rows.Add (r) is calling the constructor. Also,
before I had a parameterless constructor, I got the error "No
paramterless constructor for this control", or something like that.

Dom




[...]
MyDGVR r = new MyDGVW (1, 2, 3);
DataGridView.Rows.Add (r);
r.NewProperty = 2;
It seems the Add method called the MyDGVW parameterless constructor.

Why do you say that? Since the instance is already constructed, it
shouldn't be calling any constructor at all. What makes you think that
calling Add() executed a constructor?
I really wasn't expecting that. Am I going about this the right way?

I don't know. But a more complete code sample would probably help. Also,
elaborating on what exactly you're seeing and why you are interpreting it
the way you are, that would help as well (maybe even more than a more
complete code sample).

Pete
 
Maybe the constructor itself is wrong. Here is the code:

public ipIQDataGridViewRow() { }

public ipIQDataGridViewRow(int State_ID, int Status, int New)
: base()
{
m_State_ID = State_ID;
m_Status = Status;
m_New = New;

}


Dom



[...]
MyDGVR r = new MyDGVW (1, 2, 3);
DataGridView.Rows.Add (r);
r.NewProperty = 2;
It seems the Add method called the MyDGVW parameterless constructor.

Why do you say that? Since the instance is already constructed, it
shouldn't be calling any constructor at all. What makes you think that
calling Add() executed a constructor?
I really wasn't expecting that. Am I going about this the right way?

I don't know. But a more complete code sample would probably help. Also,
elaborating on what exactly you're seeing and why you are interpreting it
the way you are, that would help as well (maybe even more than a more
complete code sample).

Pete
 
When I step through the code, using "Step into", I can see that the
line "DataGridView.Rows.Add (r) is calling the constructor. Also,
before I had a parameterless constructor, I got the error "No
paramterless constructor for this control", or something like that.

Well, without a complete code example, I'm afraid I don't really
understand. I'm not an expert in the use of the DataGridView control
myself and I've never tried to subclass the DataGridViewRow type.

That said, until you can come up with a complete code example to post, you
might want to look at this page:
http://msdn2.microsoft.com/en-us/library/ha5xt0d9.aspx

Particular interesting is probably the discussion of shared rows. You may
be running into some situation where the DataGridView is trying to create
a new row for use as a shared row. However, the row instance you've added
should _also_ still be in the DataGridView.

So, it might be expected (I'm not sure) that the parameterless constructor
for your subclass is required and used, but I would hope that the instance
you already created would still be properly added to the DataGridView
control.

Other than the unexpected instantiation of a new instance of your class,
does the control work correctly? If so, I'd guess this is working as
designed.

Pete
 
Back
Top