Bonjour
Je développe une application dans laquelle j'ai un DataGridView qui est remplis automatiquement à partir d'un DataSet.
Dans ce DataGridView j'ai une colonne "Commentaire" dans laquelle je souhaiterai y mettre un User Control custom. En gros, dans chacune des cellules de la colonne j'aimerai y mettre ce control.
Le User Control que je souhaite faire est un simple label suivi d'un bouton (tout deux dans un TableLayoutPanel pour la disposition).
J'ai suivi ce tuto d'exemple pour mettre en place mon ColumnType par rapport à mon User Control. Ainsi que ce post.
Cependant, lorsque j'applique mon ColumnType à ma colonne "Commentaire" via la fenêtre d'édition de la form et que je lance l'application, la cellule est noire.
J'ai pu comprendre qu'il fallait surcharger la fonction Paint pour dessiner correctement le control mais je produit des résultat pas très concluant ^^'
Merci d'avance pour vos retours
Voici le code de mon control :
LabelButton.cs :
LabelButton.Designer.cs :
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66 using System; using System.Windows.Forms; namespace ControlLibrary { public class LabelButtonColumn : DataGridViewColumn { public LabelButtonColumn() : base(new LabelButtonCell()) { } public override DataGridViewCell CellTemplate { get { return base.CellTemplate; } set { if (value != null && !value.GetType() .IsAssignableFrom(typeof(LabelButtonCell))) throw new InvalidCastException("It should be a custom Cell"); base.CellTemplate = value; } } } public class LabelButtonCell : DataGridViewCell { public LabelButtonCell() : base() { } public override Type ValueType { get { return typeof(LabelButton); } } public override Type FormattedValueType { get { return typeof(string); } } //protected override void Paint(Graphics graphics, Rectangle clipBounds, Rectangle cellBounds, int rowIndex, DataGridViewElementStates cellState, object value, object formattedValue, string errorText, DataGridViewCellStyle cellStyle, DataGridViewAdvancedBorderStyle advancedBorderStyle, DataGridViewPaintParts paintParts) //{ // var ctrl = (LabelButton)value; // var img = new Bitmap(cellBounds.Width, cellBounds.Height); // ctrl.DrawToBitmap(img, new Rectangle(0, 0, ctrl.Width, ctrl.Height)); // graphics.DrawImage(img, cellBounds.Location); //} } public partial class LabelButton : UserControl { public LabelButton() { InitializeComponent(); } } }
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95 namespace ControlLibrary { partial class LabelButton { /// <summary> /// Required designer variable. /// </summary> private System.ComponentModel.IContainer components = null; /// <summary> /// Clean up any resources being used. /// </summary> /// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param> protected override void Dispose(bool disposing) { if (disposing && (components != null)) { components.Dispose(); } base.Dispose(disposing); } #region Component Designer generated code /// <summary> /// Required method for Designer support - do not modify /// the contents of this method with the code editor. /// </summary> private void InitializeComponent() { System.ComponentModel.ComponentResourceManager resources = new System.ComponentModel.ComponentResourceManager(typeof(LabelButton)); this.control_tlp = new System.Windows.Forms.TableLayoutPanel(); this.control_btn = new System.Windows.Forms.Button(); this.control_lb = new System.Windows.Forms.Label(); this.control_tlp.SuspendLayout(); this.SuspendLayout(); // // control_tlp // this.control_tlp.ColumnCount = 2; this.control_tlp.ColumnStyles.Add(new System.Windows.Forms.ColumnStyle(System.Windows.Forms.SizeType.Percent, 75.22124F)); this.control_tlp.ColumnStyles.Add(new System.Windows.Forms.ColumnStyle(System.Windows.Forms.SizeType.Percent, 24.77876F)); this.control_tlp.Controls.Add(this.control_btn, 1, 0); this.control_tlp.Controls.Add(this.control_lb, 0, 0); this.control_tlp.Dock = System.Windows.Forms.DockStyle.Fill; this.control_tlp.Location = new System.Drawing.Point(0, 0); this.control_tlp.Name = "control_tlp"; this.control_tlp.RowCount = 1; this.control_tlp.RowStyles.Add(new System.Windows.Forms.RowStyle(System.Windows.Forms.SizeType.Percent, 50F)); this.control_tlp.Size = new System.Drawing.Size(113, 24); this.control_tlp.TabIndex = 0; // // control_btn // this.control_btn.Dock = System.Windows.Forms.DockStyle.Fill; this.control_btn.Image = ((System.Drawing.Image)(resources.GetObject("control_btn.Image"))); this.control_btn.Location = new System.Drawing.Point(84, 0); this.control_btn.Margin = new System.Windows.Forms.Padding(0); this.control_btn.Name = "control_btn"; this.control_btn.Size = new System.Drawing.Size(29, 24); this.control_btn.TabIndex = 0; this.control_btn.UseVisualStyleBackColor = true; // // control_lb // this.control_lb.AutoSize = true; this.control_lb.Dock = System.Windows.Forms.DockStyle.Fill; this.control_lb.Location = new System.Drawing.Point(3, 0); this.control_lb.Name = "control_lb"; this.control_lb.Size = new System.Drawing.Size(78, 24); this.control_lb.TabIndex = 1; this.control_lb.Text = ". . ."; this.control_lb.TextAlign = System.Drawing.ContentAlignment.MiddleCenter; // // labelButton // this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F); this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font; this.Controls.Add(this.control_tlp); this.Name = "labelButton"; this.Size = new System.Drawing.Size(113, 24); this.control_tlp.ResumeLayout(false); this.control_tlp.PerformLayout(); this.ResumeLayout(false); } #endregion private System.Windows.Forms.TableLayoutPanel control_tlp; public System.Windows.Forms.Label control_lb; public System.Windows.Forms.Button control_btn; } }
Partager