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 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171
|
class DataGridViewPanelColumn : DataGridViewColumn
{
public DataGridViewPanelColumn()
{
this.CellTemplate = new DataGridViewPanelCell();
}
public override DataGridViewCell CellTemplate
{
get
{
return base.CellTemplate;
}
set
{
if (value != null && !value.GetType().IsAssignableFrom(typeof(DataGridViewPanelCell)))
{
throw new InvalidCastException("Must be a Panel Control.");
}
base.CellTemplate = value;
}
}
}
class DataGridViewPanelCell : DataGridViewButtonCell
{
private PanelTest pt;
public DataGridViewPanelCell()
{
pt = new PanelTest();
}
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)
{
base.Paint(graphics, clipBounds, cellBounds, rowIndex, cellState, value, formattedValue, errorText, cellStyle, advancedBorderStyle, paintParts);
pt.Size = cellBounds.Size;
pt.Location = cellBounds.Location;
if(!(this.DataGridView.Controls.Contains(pt)))
this.DataGridView.Controls.Add(pt);
foreach (Control ctr in DataGridView.Controls)
{
ctr.Refresh();
}
}
public override Type EditType
{
get
{
return typeof(PanelTest);
}
}
}
Et enfin ma classe panel Test :
public partial class PanelTest : UserControl, IDataGridViewEditingControl
{
public PanelTest()
{
InitializeComponent();
}
private int rowIndex;
private bool valueChanged = false;
private DataGridView dataGridView;
#region IDataGridViewEditingControl Members
public void ApplyCellStyleToEditingControl(DataGridViewCellStyle dataGridViewCellStyle)
{
this.label1.Font = dataGridViewCellStyle.Font;
}
public DataGridView EditingControlDataGridView
{
get
{
return dataGridView;
}
set
{
dataGridView = value;
}
}
public object EditingControlFormattedValue
{
get
{
return this.label1.Text;
}
set
{
string newValue = value as string;
if (newValue != null)
{
this.label1.Text = newValue;
}
}
}
public int EditingControlRowIndex
{
get
{
return rowIndex;
}
set
{
rowIndex = value;
}
}
public bool EditingControlValueChanged
{
get
{
return valueChanged;
}
set
{
valueChanged = value;
}
}
public bool EditingControlWantsInputKey(Keys keyData, bool dataGridViewWantsInputKey)
{
return true;
}
public Cursor EditingPanelCursor
{
get { return base.Cursor; }
}
public object GetEditingControlFormattedValue(DataGridViewDataErrorContexts context)
{
return EditingControlFormattedValue;
}
public void PrepareEditingControlForEdit(bool selectAll)
{
//NO preparation needed;
}
public bool RepositionEditingControlOnValueChange
{
get { return true; }
}
#endregion
private void NotifyDataGridViewOfValueChange()
{
if (!this.valueChanged)
{
this.valueChanged = true;
this.dataGridView.NotifyCurrentCellDirty(true);
}
}
} |
Partager