Windows Develop Bookmark and Share   
 index > Windows Forms General > Custom DataGridView Cell not setting the value
 

Custom DataGridView Cell not setting the value

Hello I have created a custom DataGridView Cell taht houses a Custom Control I've created called a KeyMapBox. Essentially the keymapbox is just a modified comboBox.

Everything works fine except when I change the text within the KeyMapBox the values do not change on the cell. What I mean is, if the text in the KeyMapBox reads 'Hello' and I go to that cell, go to edit mode and change that text to 'GoodBye' - when I leave the cell the text will change back to hello as though I have not altered anything.

Somehow I suspect the text in the keyMapBox is not making it to the Value of the Cell.... Anyone know how to remedy this?
CommanderKeen  Friday, September 04, 2009 4:57 PM

Do you have a dependencyproperty in your control... I think it is needed.... at least a INotyfiedChange... I think


Kenneth
Kenneth Haugland  Friday, September 04, 2009 8:16 PM
Where do I add this interface? In my custom control or within the editingControl class?

Like I have:

public class DataGridViewKeyMapBoxEditingControl : KeyMapBox, IDataGridViewEditingControl
public class DataGridViewKeyMapBoxColumn : DataGridViewColumn

public class DataGridViewKeyMapBoxCell : DataGridViewComboBoxCell


And the KeyMapBox control of course which is inherited by the editing control.
CommanderKeen  Friday, September 04, 2009 8:57 PM
Oops.... I was thinking WPF.... Disregard my reply
Kenneth
Kenneth Haugland  Sunday, September 06, 2009 4:11 PM
Hi CommanderKeen,

What I don't understand is "KeyMapBox"? What's the function of it? I guess it will map a word into another word according to your description. And also could you please your code? It is hard for us to know your project without code.

Sincerely,
Kira Qian
Please mark the replies as answers if they help and unmark if they don't.
Kira Qian  Monday, September 07, 2009 7:13 AM
The KeyMapBox is just a CustomControl that I've created... it's function is not much different than a comboBox. The only difference is that the KeyMapBox shows both the text and the value in a 2 column drop down whereas the comboBox shows only the text and the value is hidden.

This point though is irrelavent,important part is that I required this keyMapBox to be placed inside a DataGridView. I did so with the followign code which is actually quite long:
CommanderKeen  Tuesday, September 08, 2009 8:48 PM

public

class DataGridViewKeyMapBoxCell : DataGridViewComboBoxCell

{

//private static readonly KeyMapBox editingControl = editingControl = new KeyMapBox();

public DataGridViewKeyMapBoxCell()

{

}

/// <summary>

/// Gets the type of the cell's hosted editing control.

/// </summary>

/// <value></value>

/// <returns>The <see cref="T:System.Type"/> of the underlying editing control. As implemented in this class, this property is always null.</returns>

public override Type EditType

{

get

{

// Return the type of the editing contol that CalendarCell uses.

return typeof(DataGridViewKeyMapBoxEditingControl);

}

}

/// <summary>

/// Gets or sets the data type of the values in the cell.

/// </summary>

/// <value></value>

/// <returns>The <see cref="T:System.Type"/> of the cell's value.</returns>

public override Type ValueType

{

get

{

// Return the type of the value that CalendarCell contains.

return typeof(string);

}

set

{

base.ValueType = value;

}

}

/// <summary>

/// Gets the type of the formatted value associated with the cell.

/// </summary>

/// <value></value>

/// <returns>A <see cref="T:System.Type"/> object representing display value type of the cell, which is the <see cref="T:System.Drawing.Image"/> type if the <see cref="P:System.Windows.Forms.DataGridViewImageCell.ValueIsIcon"/> property is set to false or the <see cref="T:System.Drawing.Icon"/> type otherwise.</returns>

public override Type FormattedValueType

{

get

{

return typeof(KeyMapBox);

}

}

//private static void SetKeyMapBoxText(KeyMapBox c, string text)

//{

// c.Text = text;

//}

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

{

base.InitializeEditingControl(rowIndex, initialFormattedValue, dataGridViewCellStyle);

KeyMapBox ctl = DataGridView.EditingControl as KeyMapBox;

if (ctl != null)

{

ctl.Text =

Convert.ToString(initialFormattedValue);

DataGridViewKeyMapBoxColumn owningColumn = OwningColumn as DataGridViewKeyMapBoxColumn;

if (owningColumn != null)

{

ctl.DataSource = owningColumn.CustomDataSource;

ctl.KeyValueColumn.KeyColumn = owningColumn.KeyColumn;

ctl.KeyValueColumn.ValueColumn = owningColumn.ValueColumn;

ctl.OwningCell =

this;

}

//editingControl.Text = value as string;

}

}

/// <summary>

/// Returns a graphic as it would be displayed in the cell.

/// </summary>

/// <param name="value">The value to be formatted.</param>

/// <param name="rowIndex">The index of the cell's parent row.</param>

/// <param name="cellStyle">The <see cref="T:System.Windows.Forms.DataGridViewCellStyle"/> in effect for the cell.</param>

/// <param name="valueTypeConverter">A <see cref="T:System.ComponentModel.TypeConverter"/> associated with the value type that provides custom conversion to the formatted value type, or null if no such custom conversion is needed.</param>

/// <param name="formattedValueTypeConverter">A <see cref="T:System.ComponentModel.TypeConverter"/> associated with the formatted value type that provides custom conversion from the value type, or null if no such custom conversion is needed.</param>

/// <param name="context">A bitwise combination of <see cref="T:System.Windows.Forms.DataGridViewDataErrorContexts"/> values describing the context in which the formatted value is needed.</param>

/// <returns>

/// An object that represents the formatted image.

/// </returns>

protected override object GetFormattedValue(object value, int rowIndex, ref DataGridViewCellStyle cellStyle, TypeConverter valueTypeConverter, TypeConverter formattedValueTypeConverter, DataGridViewDataErrorContexts context)

{

// need to do this to display value in non edited cell

return value;

}

#region

Handlers of edit events, copied from DataGridViewTextBoxCell

/// <summary>

/// Called when the focus moves to a cell.

/// </summary>

/// <param name="rowIndex">The index of the cell's parent row.</param>

/// <param name="throughMouseClick">true if a user action moved focus to the cell; false if a programmatic operation moved focus to the cell.</param>

protected override void OnEnter(int rowIndex, bool throughMouseClick)

{

base.OnEnter(rowIndex, throughMouseClick);

}

protected override void OnLeave(int rowIndex, bool throughMouseClick)

{

base.OnLeave(rowIndex, throughMouseClick);

}

/// <summary>

/// </summary>

/// <param name="e">A <see cref="T:System.Windows.Forms.DataGridViewCellMouseEventArgs"/> that contains the event data.</param>

protected override void OnMouseClick(DataGridViewCellMouseEventArgs e)

{

base.OnMouseClick(e);

base.DataGridView.BeginEdit(false);

}

/// <summary>

/// Determines if edit mode should be started based on the given key.

/// </summary>

/// <param name="e">A <see cref="T:System.Windows.Forms.KeyEventArgs"/> that represents the key that was pressed.</param>

/// <returns>

/// true if edit mode should be started; otherwise, false. The default is false.

/// </returns>

public override bool KeyEntersEditMode(KeyEventArgs e)

{

return base.KeyEntersEditMode(e);

//return (((((char.IsLetterOrDigit((char)((ushort)e.KeyCode)) && ((e.KeyCode < Keys.F1) || (e.KeyCode > Keys.F24))) || ((e.KeyCode >= Keys.NumPad0) && (e.KeyCode <= Keys.Divide))) || (((e.KeyCode >= Keys.OemSemicolon) && (e.KeyCode <= Keys.OemBackslash)) || ((e.KeyCode == Keys.Space) && !e.Shift))) && (!e.Alt && !e.Control)) || base.KeyEntersEditMode(e));

}

#endregion

}

CommanderKeen  Tuesday, September 08, 2009 8:48 PM

public

class DataGridViewKeyMapBoxColumn : DataGridViewColumn

{

private BindingSource customDataSource;

private string keyColumn;

private string valueColumn;

public DataGridViewKeyMapBoxColumn()

:

base(new DataGridViewKeyMapBoxCell())

{

}

/// <summary>

/// </summary>

/// <returns>

/// An <see cref="T:System.Object"/> that represents the cloned <see cref="T:System.Windows.Forms.DataGridViewBand"/>.

/// </returns>

/// <PermissionSet>

/// <IPermission class="System.Security.Permissions.EnvironmentPermission, mscorlib, Version=2.0.3600.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" version="1" Unrestricted="true"/>

/// <IPermission class="System.Security.Permissions.FileIOPermission, mscorlib, Version=2.0.3600.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" version="1" Unrestricted="true"/>

/// <IPermission class="System.Security.Permissions.SecurityPermission, mscorlib, Version=2.0.3600.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" version="1" Flags="UnmanagedCode, ControlEvidence"/>

/// <IPermission class="System.Diagnostics.PerformanceCounterPermission, System, Version=2.0.3600.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" version="1" Unrestricted="true"/>

/// </PermissionSet>

public override object Clone()

{

DataGridViewKeyMapBoxColumn col = base.Clone() as DataGridViewKeyMapBoxColumn;

// This is required to show properties in the Visual Studio designer column dialogue, for the

// KeyMapBox Column.

col.CustomDataSource = customDataSource;

col.KeyColumn = keyColumn;

col.ValueColumn = valueColumn;

return col;

}

/// <summary>

/// Gets or sets the template used to create new cells.

/// </summary>

/// <value></value>

/// <returns>A <see cref="T:System.Windows.Forms.DataGridViewCell"/> that all other cells in the column are modeled after. The default is null.</returns>

public override DataGridViewCell CellTemplate

{

get

{

return base.CellTemplate;

}

set

{

if (!(value is DataGridViewKeyMapBoxCell) || (value == null))

throw new InvalidCastException("CellTemplate must be a DataGridViewKeyMapBoxCell");

base.CellTemplate = value;

}

}

/// <summary>

/// Gets or sets the custom data source.

/// </summary>

/// <value>The custom data source.</value>

public BindingSource CustomDataSource

{

get

{

return customDataSource;

}

set

{

customDataSource =

value;

}

}

/// <summary>

/// Gets or sets the key column.

/// </summary>

/// <value>The key column.</value>

public string KeyColumn

{

get

{

return keyColumn;

}

set

{

keyColumn =

value;

}

}

/// <summary>

/// Gets or sets the value column.

/// </summary>

/// <value>The value column.</value>

public string ValueColumn

{

get

{

return valueColumn;

}

set

{

valueColumn =

value;

}

}

}

CommanderKeen  Tuesday, September 08, 2009 8:48 PM

public class DataGridViewKeyMapBoxEditingControl : KeyMapBox, IDataGridViewEditingControl

{

private DataGridView dataGridView;

private int rowIndex;

private bool valueChanged;

public DataGridViewKeyMapBoxEditingControl()

{

this.BorderStyle = BorderStyle.None;

}

/// <summary>

/// Raises the <see cref="E:System.Windows.Forms.Control.TextChanged"/> event.

/// </summary>

/// <param name="e">An <see cref="T:System.EventArgs"/> that contains the event data.</param>

protected override void OnTextChanged(EventArgs e)

{

base.OnTextChanged(e);

EditingControlValueChanged =

true;

this.EditingControlDataGridView.NotifyCurrentCellDirty(true);

}

#region

IDataGridViewEditingControl Members

/// <summary>

/// Changes the control's user interface (UI) to be consistent with the specified cell style.

/// </summary>

/// <param name="dataGridViewCellStyle">The <see cref="T:System.Windows.Forms.DataGridViewCellStyle"/> to use as the model for the UI.</param>

public void ApplyCellStyleToEditingControl(DataGridViewCellStyle dataGridViewCellStyle)

{

this.Font = dataGridViewCellStyle.Font;

}

/// <summary>

/// Gets or sets the <see cref="T:System.Windows.Forms.DataGridView"/> that contains the cell.

/// </summary>

/// <value></value>

/// <returns>The <see cref="T:System.Windows.Forms.DataGridView"/> that contains the <see cref="T:System.Windows.Forms.DataGridViewCell"/> that is being edited; null if there is no associated <see cref="T:System.Windows.Forms.DataGridView"/>.</returns>

public DataGridView EditingControlDataGridView

{

get

{

return dataGridView;

}

set

{

dataGridView =

value;

}

}

/// <summary>

/// Gets or sets the formatted value of the cell being modified by the

/// editor.

/// </summary>

/// <value></value>

/// <returns>An <see cref="T:System.Object"/> that represents the formatted value of the cell.</returns>

public object EditingControlFormattedValue

{

get

{

return this.Text;

}

set

{

if (value is string)

this.Text = value as string;

}

}

/// <summary>

/// Gets or sets the index of the hosting cell's parent row.

/// </summary>

/// <value></value>

/// <returns>The index of the row that contains the cell, or � if there is no parent row.</returns>

public int EditingControlRowIndex

{

get

{

return rowIndex;

}

set

{

rowIndex =

value;

}

}

/// <summary>

/// Gets or sets a value indicating whether the value of the editing control differs from the value of the hosting cell.

/// </summary>

/// <value></value>

/// <returns>true if the value of the control differs from the cell value; otherwise, false.</returns>

public bool EditingControlValueChanged

{

get

{

return valueChanged;

}

set

{

valueChanged =

value;

}

}

/// <summary>

/// Determines whether the specified key is a regular input key that the

/// editing control should process or a special key that the

/// <see cref="T:System.Windows.Forms.DataGridView"/> should process.

/// </summary>

/// <param name="keyData">A <see cref="T:System.Windows.Forms.Keys"/> that represents the key that was pressed.</param>

/// <param name="dataGridViewWantsInputKey">true when the <see cref="T:System.Windows.Forms.DataGridView"/> wants to process the <see cref="T:System.Windows.Forms.Keys"/> in <paramref name="keyData"/>; otherwise, false.</param>

/// <returns>

/// true if the specified key is a regular input key that should be handled by the editing control; otherwise, false.

/// </returns>

public bool EditingControlWantsInputKey(Keys keyData, bool dataGridViewWantsInputKey)

{

if (keyData == Keys.Down || keyData == Keys.Up)

{

return true;

}

if (keyData == Keys.Enter)

{

return true;

}

//List keys you want and return true if you want the editing control to handle keys.

return !dataGridViewWantsInputKey;

}

public Cursor EditingPanelCursor

{

get { return this.Cursor; }

}

/// <summary>

/// Retrieves the formatted value of the cell.

/// </summary>

/// <param name="context">A bitwise combination of <see cref="T:System.Windows.Forms.DataGridViewDataErrorContexts"/> values that specifies the context in which the data is needed.</param>

/// <returns>

/// An <see cref="T:System.Object"/> that represents the formatted version of the cell contents.

/// </returns>

public object GetEditingControlFormattedValue(DataGridViewDataErrorContexts context)

{

return this.Text;

}

/// <summary>

/// Prepares the currently selected cell for editing.

/// </summary>

/// <param name="selectAll">true to select all of the cell's content; otherwise, false.</param>

public void PrepareEditingControlForEdit(bool selectAll)

{

}

/// <summary>

/// Gets or sets a value indicating whether the cell contents need to be repositioned whenever the value changes.

/// </summary>

/// <value></value>

/// <returns>true if the contents need to be repositioned; otherwise, false.</returns>

public bool RepositionEditingControlOnValueChange

{

get { return false; }

}

#endregion

}

CommanderKeen  Tuesday, September 08, 2009 8:49 PM

CommanderKeen,

Please use the Code Formatting Tool to post your code samples.
There are many who ignore such posts, many who could provide vital assistance to you.

You should try to copy and paste that stuff into your own test project, and check out the result.
Discover for yourself what posts such as these are ignored.
The result does not compile, and requires extensive modifications.


Mark the best replies as answers. "Fooling computers since 1971."
Rudedog2  Tuesday, September 08, 2009 10:19 PM
Thank you. I think that the problem is too complicated for anyone to help me with. I cannot post a code sample that will be runnable for someone else to debug. I think I will close this thread until I can form a sample that works.
CommanderKeen  Tuesday, September 08, 2009 10:26 PM
Very well. But, ....
There are some very clever folks around herewho can connect the dots when provided with enough of them.

Remember this, the best answerers have been around for a while and have observed the pattern of your questions.
Being a succesful programmer, means paying attention to details all the way down to the last minutae.
Posting code like that demonstrates an unflattering laziness and is demonstrative of a total lack of effort on your part.

Happy coding.

Rudedog =8^D
Mark the best replies as answers. "Fooling computers since 1971."
Rudedog2  Tuesday, September 08, 2009 10:44 PM
Thanks for the tip and I agree with you. So I could post code that would work it someone copy pasted it into their visual studio and run it and have a working example right in front of them; however, this amount of code I would have to paste would be extremely large (maybe 2000-4000 lines I estimate).
CommanderKeen  Tuesday, September 08, 2009 10:46 PM
Not true.
When I said some of the answerers are pretty good at connecting the dots I meant it.

I have provided you with this link a few times in the past.

DataBinding FAQ
Mark the best replies as answers. "Fooling computers since 1971."
Rudedog2  Tuesday, September 08, 2009 10:59 PM
Bad eyes used the wrong link.

DataGridView Control
Mark the best replies as answers. "Fooling computers since 1971."
Rudedog2  Tuesday, September 08, 2009 11:00 PM

You can use google to search for other answers

Custom Search

More Threads

• Registering File Types
• Form doesnt return after ShowDialog when runs standalone. C# 2.0
• Black label and persistent wait cursor
• What should localized RESX files contain
• User/Computer Policies interfering with Framework setting - program won't run
• ListBox items should be gray when not in focus - why arent they?
• loop and logfile
• Focus in application started remotely
• How do I display specific columns from a BindingSource in a DataGridView?
• Type Initializer threw an exception