Reputation: 351
I created one usercontrol and added more functions for TextBox, I bind this usercontrol a DataGridViewColumn. But I don't know how I can access custom properties the user control.
How base I followed the tutorial https://msdn.microsoft.com/en-us/library/7tas5c80.aspx?PHPSESSID=o1fb21liejulfgrptbmi9dec92
public class IntegerCell : DataGridViewTextBoxCell
{
//Implementations
}
public class IntegerColumn : DataGridViewColumn
{
public IntegerColumn()
:base(new IntegerCell())
{
}
public override DataGridViewCell CellTemplate
{
get
{
return base.CellTemplate;
}
set
{
// Ensure that the cell used for the template is a CalendarCell.
if (value != null &&
!value.GetType().IsAssignableFrom(typeof(IntegerCell)))
{
throw new InvalidCastException("Must be a IntegerCell");
}
base.CellTemplate = value;
}
}
[Browsable(true)]
[Category("Custom")]
[Description("")]
[DisplayName("Max. Value")]
public int MaxValue { get; set; }
[Browsable(true)]
[Category("Custom")]
[Description("")]
[DisplayName("Min. Value")]
public int MinValue { get; set; }
}
public partial class IntegerEditingControl : IntegerTextBox, IDataGridViewEditingControl
{
//Implementations
}
public class IntegerTextBox : TextBox
{
[Browsable(true)]
[Category("Custom")]
[Description("")]
[DisplayName("Max. Value")]
public int MaxValue { get; set; }
[Browsable(true)]
[Category("Custom")]
[Description("")]
[DisplayName("Min. Value")]
public int MinValue { get; set; }
}
public partial class Form1 : Form
{
private DataGridView dataGridView1 = new DataGridView();
public Form1()
{
InitializeComponent();
this.dataGridView1.Dock = DockStyle.Fill;
this.Controls.Add(this.dataGridView1);
this.Load += new EventHandler(Form1_Load);
this.Text = "DataGridView column demo";
}
private void Form1_Load(object sender, EventArgs e)
{
IntegerColumn col = new IntegerColumn();
this.dataGridView1.Columns.Add(col);
this.dataGridView1.RowCount = 5;
int i = 0;
foreach (DataGridViewRow row in this.dataGridView1.Rows)
{
row.Cells[0].Value = i++;
}
}
}
I want pass set values the DataGridView custom column to usercontrol properties.
Upvotes: 1
Views: 2046
Reputation: 125342
To fix the problem you need apply multiple fixes to the code, but as a general answer, InitializeEditingControl
method of your custom cell is where you should configure editing control. You should override InitializeEditingControl
to get access to editing control and be able to set it's properties.
public override void InitializeEditingControl(int rowIndex, object
initialFormattedValue, DataGridViewCellStyle dataGridViewCellStyle)
{
base.InitializeEditingControl(rowIndex, initialFormattedValue, dataGridViewCellStyle);
YourEditingControl c = DataGridView.EditingControl as YourEditingControl;
//Set c.Properties here
}
This is not the only fix which your code needs. The linked article is just a simple example and you should consider some other points too::
CellTemplate
. CellTemplate
, also set property value for all cells of the column using a for
loop, because the user expect column settings apply to all cells.Clone
and make sure you perform a deep copy of properties of custom cell. But since you store property values in cell template, you don't need to override Clone
for custom column. Cloning is important for designer.InitializeEditingControl
you should setup editing control using properties of the custom cell. Also keep a reference to the editing control, and update properties of the editing control in setter of properties of the cell, because the user expect to see changes on the editing control if a property of column changed.OnTextChanged
) and then notify the DataGridView
that the cell has been dirty using this.EditingControlDataGridView.NotifyCurrentCellDirty(true);
You can see the rules which I mentioned in source codes related to ComboBox
column:
Upvotes: 2