how to host custom checkbox in datagridview

  • Thread starter Thread starter jay721
  • Start date Start date
J

jay721

Guest
I have a datagridview with checkbox and I downloaded Material Skin from the NuGet package that provides material checkbox I want to use this material check inside my datagridview.



I tried but it's giving an exception :

System.FormatException: Formatted value of the cell has <g class="gr_ gr_143 gr-alert gr_gramm gr_inline_cards gr_run_anim Grammar multiReplace" data-gr-id="143" id="143">a wrong</g> type. To replace this default dialog please handle the data error event








class ChackBoxColuman : DataGridViewColumn
{

public ChackBoxColuman() : base(new ChackBoxCell())
{
}



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(ChackBoxCell)))
{
throw new InvalidCastException("Must be a ChackBoxCedll");
}
base.CellTemplate = value;
}
}

}

class ChackBoxCell : DataGridViewCheckBoxCell {

public ChackBoxCell() : base() {
this.Style.Format = "";
}



public override object DefaultNewRowValue
{
get
{
return false;
}
}

public override void InitializeEditingControl(int rowIndex, object initialFormattedValue, DataGridViewCellStyle dataGridViewCellStyle)
{
base.InitializeEditingControl(rowIndex, initialFormattedValue,dataGridViewCellStyle);
ChackBoxEdit ctr = DataGridView.EditingControl as ChackBoxEdit;
if (this.Value == null)
{
ctr.Checked = false;
}
else {

ctr.Checked = System.Convert.ToBoolean(this.Value);
}
}

public override Type EditType
{
get
{
// Return the type of the editing control that CalendarCell uses.
return typeof(ChackBoxEdit);
}
}


public override Type ValueType
{
get
{
return typeof(bool);
}

}


}



class ChackBoxEdit : MaterialChackbox,IDataGridViewEditingControl
{
DataGridView dataGridView;
private bool valueChanged = false;
int rowIndex;

public ChackBoxEdit():base() {

}

public DataGridView EditingControlDataGridView
{
get
{
return dataGridView;
}
set
{
dataGridView = value;
}
}
//EditControl Formeted Value
//public object EditingControlFormattedValue { get { return this.Checked; } set
// {
// if (value is bool)
// {
// try
// {
// this.Checked = Convert.ToBoolean(value);
// }
// catch
// {
// this.Checked = false;
// }

// }
// }
//}
public int EditingControlRowIndex {

get
{
return rowIndex;
}
set
{
rowIndex = value;
}

}
public bool EditingControlValueChanged {

get
{
return valueChanged;
}
set
{
valueChanged = value;
}
}

public Cursor EditingPanelCursor {

get
{
return base.Cursor;
}
}




public bool RepositionEditingControlOnValueChange {

get
{
return false;
}
}

public object EditingControlFormattedValue
{
get
{
return this.Checked;
}
set
{
this.Checked =Convert.ToBoolean(value);
}
}


public void ApplyCellStyleToEditingControl(DataGridViewCellStyle dataGridViewCellStyle)
{
this.Font = dataGridViewCellStyle.Font;
}

public bool EditingControlWantsInputKey(Keys key, bool dataGridViewWantsInputKey)
{
// Let the DateTimePicker handle the keys listed.
switch (key & Keys.KeyCode)
{
case Keys.Left:
case Keys.Up:
case Keys.Down:
case Keys.Right:
case Keys.Home:
case Keys.End:
case Keys.PageDown:
case Keys.PageUp:
return true;
default:
return !dataGridViewWantsInputKey;
}
}
//Get
public object GetEditingControlFormattedValue(DataGridViewDataErrorContexts context)
{
return this.Checked;
}

public void PrepareEditingControlForEdit(bool selectAll)
{
// throw new NotImplementedException();
}


//

protected override void OnCheckedChanged(EventArgs eventargs)
{
// Notify the DataGridView that the contents of the cell
// have changed.
valueChanged = true;
this.EditingControlDataGridView.NotifyCurrentCellDirty(true);
base.OnCheckedChanged(eventargs);
}

// Implements the IDataGridViewEditingControl.EditingControlFormattedValue
// property.



}

Continue reading...
 
Back
Top