Customise datagridview column does not bind into data field ?

T

Tsair

My Customise datagridview column Code like this, it does not bind the data
into data field. When i go to next row of data, the column refresh back to
empty. Looking for any solutions. Thank you.


public class DateTimeTextBoxCell : DataGridViewTextBoxCell
{

private static Type defaultEditType =
typeof(DateTimeTextBoxEditingControl);
private static Type defaultValueType = typeof(System.DateTime);

public DateTimeTextBoxCell(): base()
{ }

public override void InitializeEditingControl(int rowIndex, object
initialFormattedValue, DataGridViewCellStyle dataGridViewCellStyle)
{

base.InitializeEditingControl(rowIndex, initialFormattedValue,
dataGridViewCellStyle);
DateTimeTextBoxEditingControl datetimeTextBox =
DataGridView.EditingControl as DateTimeTextBoxEditingControl;
DataGridViewColumn dateTimeTextBoxColumn =
this.DataGridView.Columns[this.ColumnIndex];
string initialFormattedValueStr = initialFormattedValue as string;
if (dateTimeTextBoxColumn is DataGridViewDateTimeTextBoxColumn)
{
datetimeTextBox.FieldType =
(winControl.TextBox.enumDataType)this._fieldtype;
if (initialFormattedValueStr == null)
datetimeTextBox.Text = string.Empty;
else
datetimeTextBox.Text = initialFormattedValueStr;
}

}

public override Type EditType
{
get { return typeof(DateTimeTextBoxEditingControl); }
}

public override Type ValueType
{
get
{
Type valueType = base.ValueType;
if (valueType != null)
return valueType;

return defaultValueType;
}
}

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

public override object Clone()
{
DateTimeTextBoxCell dataGridViewCell = base.Clone() as
DateTimeTextBoxCell;
return dataGridViewCell;
}
}



public class DataGridViewDateTimeTextBoxColumn : DataGridViewColumn
{
public DataGridViewDateTimeTextBoxColumn(): base(new
DateTimeTextBoxCell())
{
}

public override DataGridViewCell CellTemplate
{
get{ return base.CellTemplate; }
set
{
DateTimeTextBoxCell dateTimeTextBoxCell = value as
DateTimeTextBoxCell;
if (value != null && dateTimeTextBoxCell == null)
{
throw new InvalidCastException("Value provided for
CellTemplate must be of type DataGridViewDateTimeTextBox or derive from
it");
}
base.CellTemplate = value;
}
}

public override object Clone()
{
DataGridViewDateTimeTextBoxColumn obj =
(DataGridViewDateTimeTextBoxColumn)base.Clone();
return obj;
}
}


class DateTimeTextBoxEditingControl : DateTimeTextBox,
IDataGridViewEditingControl
{
System.Windows.Forms.DataGridView dataGridView;
private bool valueChanged = false;
int rowIndex;

public DateTimeTextBoxEditingControl()
{ this.textBox.Leave += new
System.EventHandler(this.DateTimeTextBox_Leave);
}

private void DateTimeTextBox_Leave(object sender, System.EventArgs
e)
{
valueChanged = true;
this.EditingControlDataGridView.NotifyCurrentCellDirty(true);
}

public Cursor EditingPanelCursor
{
get { return Cursors.IBeam; }
}

public virtual System.Windows.Forms.DataGridView
EditingControlDataGridView
{
get { return dataGridView; }
set { dataGridView = value; }
}

public object EditingControlFormattedValue
{
get { return this.textBox.Text; }
set { this.Text = value.ToString(); }
}

public object GetEditingControlFormattedValue(
DataGridViewDataErrorContexts context)
{
return EditingControlFormattedValue;
}

public bool EditingControlWantsInputKey(Keys key, bool
dataGridViewWantsInputKey)
{
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;
}

return !dataGridViewWantsInputKey;
}


public void PrepareEditingControlForEdit(bool selectAll)
{
}


public bool RepositionEditingControlOnValueChange
{
get{return false;}
}

public int EditingControlRowIndex
{
get{return rowIndex;}
set{rowIndex = value;}
}

public void ApplyCellStyleToEditingControl( DataGridViewCellStyle
dataGridViewCellStyle)
{ }


public bool EditingControlValueChanged
{
get{return valueChanged;}
set{valueChanged = value;}
}
}


// DateTimeTextBox control contain Textbox (for display date string) +
button (for call datetimepicker)
// the DateTimeTextBox DefaultBindingProperty("DataText")
// the problem is when navigated to new row, the column value show empty /
null, it does not save input into datatable

[System.ComponentModel.DefaultBindingProperty("DataText")]
public partial class DateTimeTextBox : UserControl
{
.......

[System.ComponentModel.Bindable(true), Browsable(true),
Category("Behavior"), Description("Date and Time displayed")]
public virtual String DataText
{
get
{
try
return
DateTime.Parse(this.textBox.Text).ToString("dd-MMM-yyyy",
Application.CurrentCulture);
catch
return null;
}

set
{
try
this.textBox.Text = string.Format("{0:dd-MMM-yyyy}",
DateTime.Parse( value ));
catch
this.textBox.Text = value;
}
}
}
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top