Note that there are some explanatory texts on larger screens.

plurals
  1. POC# Add UserControl to a datagridview
    text
    copied!<p>I wanna add a usercontrol in a datagridview, my usercontrol is very simple : </p> <pre><code>public partial class UpDownControl : UserControl { public event EventHandler UpClick; public event EventHandler DownClick; public UpDownControl(string id) { InitializeComponent(); this.Tag = id; } void BDownClick(object sender, EventArgs e) { if(DownClick!=null) DownClick(this,e); } void BUpClick(object sender, EventArgs e) { if(UpClick!=null) UpClick(this,e); } } </code></pre> <p>All I wanna do is add this control in the first column of a datagridview, I tried to find out a solution by myself, but in vain I didnt find one. Don't tell me to go see this link, <a href="http://msdn.microsoft.com/en-us/library/7tas5c80.aspx" rel="nofollow">http://msdn.microsoft.com/en-us/library/7tas5c80.aspx</a>, I read it all but I didnt find a way to do it with MY usercontrol.</p> <p>So, this is my current code : </p> <pre><code>for(int i=0;i&lt;lposte.Count;++i) { UpDownControl updown = new UpDownControl("test1"); updown.DownClick += new EventHandler(testvincdown); updown.UpClick += new EventHandler(testvincup); rang = 1; CustomColumn col = new CustomColumn(); CustomeCell cell = new CustomeCell(); col.CellTemplate = cell; dataGridView1.Columns.Add(col); dataGridView1.Rows.add(updown); } public class CustomColumn : DataGridViewColumn { public CustomColumn() : base(new CustomeCell()) { } public override DataGridViewCell CellTemplate { get { return base.CellTemplate; } set { // Ensure that the cell used for the template is a CalendarCell. if (value != null &amp;&amp; !value.GetType().IsAssignableFrom(typeof(CustomeCell))) { throw new InvalidCastException("It should be a custom Cell"); } base.CellTemplate = value; } } } public class CustomeCell : DataGridViewCell { public CustomeCell() : base() { } public override Type ValueType { get { return typeof(UpDownControl); } } } </code></pre> <p>Any help will be highly appreciated. PS: Sorry if my english is not perfect, I did my best.</p> <p>Thank you. </p> <p>Vinc</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload