Override DataGridViewColumn and datagridviewcolumn
You need to use the DataGridView to create a project. This control is quite useful, but today we find that you cannot implement the functions you want. The primary reason is that although the DataGridViewCheckBoxColumn column provides a check box, it does not provide text display next to the check box. After searching on the Internet, many of the methods provided are to get two columns, then merge cells, and combine the two columns into one column. However, I do not like this method, so I just rewritten the DataGridViewColumn, which is a little simple and only implements the most basic functions. Now I hope you can provide some good comments and opinions.
First, define a class that implements the IDataGridViewEditingControl interface.
Class DataGridViewCheckBoxTextControl: CheckBox, IDataGridViewEditingControl {// <summary> // The current table // </summary> private datagrimymydatagri{ set; get ;} /// <summary> /// whether the value has changed /// </summary> private bool ValueChanged {set; get ;} /// <summary> /// current row // </summary> private int RowIndex {set; get;} protected override void OnCheckedChanged (EventArgs e) {ValueChanged = true; This. editingControlDataGridView. yycurrentcelldirty (true); base. onCheckedChanged (e);} public void ApplyCellStyleToEditingControl (maid) {Font = maid. font; ForeColor = maid. foreColor; BackColor = maid. backColor;} public DataGridView editingcontroldatagridatagri{ get {return MyDataGridView;} set {MyData GridView = value ;}} public object EditingControlFormattedValue {get {return GetEditingControlFormattedValue (DataGridViewDataErrorContexts. Formatting);} set {Checked = value = null? False: (bool) value ;}} 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) {switch (keyData & Keys. keyCode) {case Keys. LButton: return! DataGridViewWantsInputKey;} return! DataGridViewWantsInputKey;} public Cursor EditingPanelCursor {get {return Cursors. default ;}} public object GetEditingControlFormattedValue (DataGridViewDataErrorContexts context) {return this. checked;} public void PrepareEditingControlForEdit (bool selectAll) {} public bool RepositionEditingControlOnValueChange {get {return false ;}}}
Second, define the cells required to override this column.
Public class maid: Maid (): base () {} private static Type defaultEditType = typeof (DataGridViewCheckBoxTextControl); private static Type defaultValueType = typeof (System. boolean); public override Type EditType {get {return defaultEditType ;}/// <summary> // cell border Color /// </summary> private Color CellBorderColor {get {Return Color. fromArgb (172,168,153) ;}} protected override void Paint, dataGridViewAdvancedBorderStyle advancedBorderStyle, DataGridViewPaintParts paintParts) {var check = (Boolean) value; if (pain TParts = maid. background | paintParts = maid. all) {graphics. fillRectangle (new SolidBrush (cellStyle. backColor), cellBounds);} if (paintParts = maid. border | paintParts = maid. all) {graphics. drawRectangle (new Pen (CellBorderColor), cellBounds);} if (paintParts = DataGridViewPaintParts. selectionBackground | Selected ){ Graphics. FillRectangle (new SolidBrush (cellStyle. SelectionBackColor), cellBounds);} var col = OwningColumn as maid checkboxtextcolumn; if (col! = Null &&! String. IsNullOrEmpty (col. Text) {graphics. DrawString (col. Text, cellStyle. Font, new SolidBrush (Selected? CellStyle. selectionForeColor: cellStyle. foreColor), new Point (cellBounds. X + 25, cellBounds. Y + cellBounds. height/4);} CheckBoxRenderer. drawCheckBox (graphics, new Point (cellBounds. X + 4, cellBounds. Y + cellBounds. height/4), CheckState); base. paint (graphics, clipBounds, cellBounds, rowIndex, cellState, value, formattedValue, errorText, cellStyle, advancedBorderStyle, paintParts);} // <summ Ary >//< summary> /// status of the current check box /// </summary> private CheckBoxState CheckState {set; get;} protected override void OnMouseDown (DataGridViewCellMouseEventArgs e) {var check = (bool) Value; CheckState = check? CheckBoxState. CheckedPressed: CheckBoxState. UncheckedPressed; base. OnMouseDown (e);} protected override void OnMouseUp (DataGridViewCellMouseEventArgs e) {var check = (bool) Value; Value =! Check; SetValue (RowIndex, Value); CheckState = check? CheckBoxState. CheckedNormal: CheckBoxState. UncheckedNormal; base. OnMouseUp (e);} public override Type ValueType {get {Type valueType = base. ValueType; if (valueType! = Null) {return valueType;} return defavaluvaluetype;} public override object DefaultNewRowValue {get {return true ;}}}
Finally, you need to define the new column.
Public class DataGridViewCheckBoxTextColumn: DataGridViewColumn {public DataGridViewCheckBoxTextColumn (): base () {CellTemplate = new DataGridViewCheckBoxTextCell ();} public override DataGridViewCell CellTemplate {get {return base. cellTemplate;} set {if (value! = Null &&! Value. getType (). isAssignableFrom (typeof (DataGridViewCheckBoxTextCell) {throw new Exception ("This column must be bound to MyDataGridViewCheckBoxCell");} base. cellTemplate = value ;}public override object Clone () {maid col = (maid) base. clone (); col. text = Text; return col;} public string Text {set; get ;}}
If this column is defined in the control class library, you can directly add this column in the editor when referencing it to the project.
Finally:
How to override CellTemplate in the datagridview
Public override DataGridViewCell CellTemplate {get; set ;}// override meaning
Privatevoid AddOutOfOfficeColumn ()
{
DataGridViewCheckBoxColumn column = new DataGridViewCheckBoxColumn ();
{
Column. HeaderText = ColumnName. OutOfOffice. ToString ();
Column. Name = ColumnName. OutOfOffice. ToString ();
Column. AutoSizeMode =
DataGridViewAutoSizeColumnMode. DisplayedCells;
Column. FlatStyle = FlatStyle. Standard;
Column. ThreeState = true;
Column. CellTemplate = new maid ();
Column. CellTemplate. Style. BackColor = Color. Beige;
}
DataGridView1.Columns. Insert (0, column );
}
EXAMPLE FROM MSDN
How to add other controls to the dview
They are talking about the DataGridView of ASP Web pages.
No way in WinForm