Reputation: 1202
I want to create my DataGridViewCell
, I have created one subclassed from DataGridViewTextBoxCell
, called MyDGVCell
. But I want to pass some parameters to MyDGVCell
.
If no parameters, everything is simple:
myDGV.CellTemplate = new MyDGVCell();
But I want to pass parameter, so my MyDGVCell's constructor has to take a parameter. My Code is:
myDGV.CellTemplate = new MyDGVCell(aValue);
Now is OK, but when I bind my dataTable to myDGV, it reports error that: no parameterless constructor is found
. I've checked out that it's due to my MyDGVCell
, which indeed has no parameterless constructor.
So, my question is: how to pass the parameter to DataGridViewCell
? If parameterless constructor is required, how can I pass parameter in run-time?
Any advice will be helpful, thanks a lot.
---------EDIT---------
To simplify my question, I have created a new Windows Form Project, drag a dataGridView1 to my Form1 and make these code in Form_Load event:
private void Form1_Load(object sender, EventArgs e)
{
dataGridView1.Columns.Add("dodo", "dodo");
dataGridView1.Columns[0].CellTemplate = new MyCell("prop"); //line 1
//dataGridView1.Columns[0].CellTemplate = new DataGridViewTextBoxCell(); //line 2
dataGridView1.Rows.Add("try make cell");
}
And here is my customized DataGridViewCell
class MyCell : DataGridViewTextBoxCell
{
public string MyProperty { get; set; }
public MyCell(string myProp)
: base()
{
this.MyProperty = myProp;
}
}
The outcome is interesting, if I use code in line 1, which using my own Cell, the dataGridView doesn't create the row, but if I use the line 2 code, which is normal TextBoxCell, it creates the row.
Moreover, if I use my Cell, when I enter something in the added new cell, It reports the same error: no parameterless constructor found for this project.
So How can I pass parameter to cell in run-time? Thanks
Upvotes: 2
Views: 1951
Reputation: 81
I got inspired by @speednick:
class MyCell : DataGridViewTextBoxCell
{
public string MyProperty { get; set; }
public MyCell() : base()
{ // Default constructor }
public MyCell(MyCell own) : base()
{
MyProperty = own.MyProperty;
}
public override object Clone()
{
return (object)new MyCell(this);
}
}
Upvotes: 0
Reputation: 15
I've found this alternative solution.
In the class extending the DataGridViewCell class, you can add as properties the parameters you have to pass in the constructor and then override the Clone method. The override must call the base Clone method, then cast the object returned to the DataGridViewCell extending class and then copy the properties in the cloned object.
Here's a sample code:
public class DataGridViewBoolCell : DataGridViewImageCell
{
protected bool YourProperty{ get; private set; }
public DataGridViewBoolCell( bool yourParameter )
{
YourProperty = yourParameter;
}
public override object Clone()
{
var cloned = base.Clone();
((DataGridViewBoolCell)cloned).YourProperty = YourProperty;
return cloned;
}
}
I don't know if this is the best solution but it worked for me.
HTH
Upvotes: 1
Reputation: 1202
Since no one posted any answer, I would like to share my solution. I don't know if it's the best way but it works. I ended up setup a class from DataGridViewTextBoxCellColumn
, than I make a property to this column class. In MyCell
class, I override a method during initializing this cell. Then I use this.owningColumn.MyPropertyFromColumn to get what I want. Actually this cell can get this parameter from its owning column everywhere.
It seems the cell must has parameterless constructor. So I guess it's the only way of getting the parameter from its owning column in run-time.
Upvotes: 1