Hello everyone!
How can i integrate a combobox on Datagridview cell that is combobox will appear only when i double click the specific cell on datagridview.
Any help pls..
Thnx.. godbless.. - Moved bynobugzMVP, ModeratorSaturday, August 08, 2009 3:24 PM (From:Windows Forms General)
-
| | jabdgreat Saturday, August 08, 2009 9:59 AM | Hi jabdgreat,
I found another simple solution. These are the steps:
1. Add a DataGridViewTextBoxColumn instead of a DataGridViewComboBoxColumn.
2. Add a ComboBox to the form and hide it.
3. Handle the CellDoubleClick event. In the handler, locate the ComboBox to the position of the current cell, set its value to the text of the current cell and show it.
This is the code snippet: private ComboBox combobox1 = new ComboBox();
private void Form_load(object sender, EventArgs e)
{
//TODO: Set the data source, display member, value member of the combobox.
combobox1.Hide();
this.datagridview1.Controls.Add(combobox1);
}
private void dataGridView2_CellDoubleClick(object sender, DataGridViewCellEventArgs e)
{
//Show the combo box.
this.combobox1.Location = this.datagridview1.GetCellDisplayRectangle(e.ColumnIndex, e.RowIndex, true).Location;
this.combobox1.SelectedValue = this.datagridview1.CurrentCell.Value;
this.combobox1.Show();
}
Best regards, Aland Li
Please mark the replies as answers if they help and unmark if they don't. This can be beneficial to other community members reading the thread. - Marked As Answer byAland LiMSFT, ModeratorFriday, August 14, 2009 5:21 AM
-
| | Aland Li Friday, August 14, 2009 5:00 AM | Why not use the built-in ComboBox for the DataGridView? It would be significantly easier. Hope this helps. www.insteptech.com ;
msmvps.com/blogs/deborahk
We are volunteers and ask only that if we are able to help you, that you mark our reply as your answer. THANKS! | | DeborahK Tuesday, August 11, 2009 11:44 PM | Hi jabdgreat,
At first, we need to create our own ComboBoxColumn, ComboBoxCell and ComboBoxControl to have the ComboBox not show when the cell is not in edit mode. This is the code snippet:
//Custom DataGridViewColumn
public class DataGridViewComboBoxColumn : DataGridViewColumn
{
public DataGridViewComboBoxColumn()
: base(new DataGridViewComboBoxCell())
{
}
public override DataGridViewCell CellTemplate
{
get
{
return base.CellTemplate;
}
set
{
if ((value != null) &&
(!value.GetType().IsAssignableFrom(typeof(DataGridViewComboBoxCell))))
{
throw new InvalidCastException("Must be a DataGridViewHelpCombBoxCell");
}
base.CellTemplate = value;
}
}
public override object Clone()
{
DataGridViewComboBoxColumn col = base.Clone() as DataGridViewComboBoxColumn;
//Copy the added properties, otherwise it would cause some error, such as serialization error.
col.ValueMember = this.ValueMember;
col.DisplayMember = this.ValueMember;
col.DataSource = this.DataSource;
col.ValueType = this.ValueType;
return col;
}
//Data source of the combobox.
public object DataSource { set; get; }
//Display member of the combobox.
public string DisplayMember { set; get; }
//Value member of the combobox.
public string ValueMember { set; get; }
//Type of the value in combobox.
public Type ValueType { set; get; }
}
public class DataGridViewComboBoxCell : DataGridViewTextBoxCell
{
public override void InitializeEditingControl(int rowIndex, object initialFormattedValue, DataGridViewCellStyle dataGridViewCellStyle)
{
base.InitializeEditingControl(rowIndex, initialFormattedValue, dataGridViewCellStyle);
ComboBox combBox = DataGridView.EditingControl as ComboBox;
combBox.DropDownStyle = ComboBoxStyle.DropDownList;
DataGridViewComboBoxColumn col = DataGridView.Columns[ColumnIndex] as DataGridViewComboBoxColumn;
//Transfer values from column to combobox.
combBox.DataSource = col.DataSource;
combBox.DisplayMember = col.DisplayMember;
combBox.ValueMember = col.ValueMember;
try
{
object value = this.GetValue(rowIndex);
//If value is null, set the value to the first value in data source.
if (value != null)
combBox.SelectedValue = value;
else
combBox.SelectedIndex = 0;
}
catch
{
MessageBox.Show("Set value fail.");
}
}
public override Type ValueType
{
get
{
return (DataGridView.Columns[ColumnIndex] as DataGridViewComboBoxColumn).ValueType;
}
}
public override Type FormattedValueType
{
get
{
return typeof(string);
}
}
public override object DefaultNewRowValue
{
get
{
return null;
}
}
public override Type EditType
{
get
{
return typeof(ComboBoxControl);
}
}
public override object ParseFormattedValue(object formattedValue, DataGridViewCellStyle cellStyle, TypeConverter formattedValueTypeConverter, TypeConverter valueTypeConverter)
{
//Parse null value to blank.
if (formattedValue == null)
return "";
else
return formattedValue.ToString();
}
protected override void Paint(System.Drawing.Graphics graphics, System.Drawing.Rectangle clipBounds, System.Drawing.Rectangle cellBounds, int rowIndex, DataGridViewElementStates cellState, object value, object formattedValue, string errorText, DataGridViewCellStyle cellStyle, DataGridViewAdvancedBorderStyle advancedBorderStyle, DataGridViewPaintParts paintParts)
{
//When the value is null, draw blank.
string newVal = "";
if (this.DataGridView.Rows[rowIndex].IsNewRow == false)
{
if ((value != null) && (value.ToString().Trim() != ""))
{
newVal = value.ToString().Trim();
}
}
base.Paint(graphics, clipBounds, cellBounds, rowIndex, cellState, value, value, errorText, cellStyle, advancedBorderStyle, paintParts);
}
}
public class ComboBoxControl : ComboBox, IDataGridViewEditingControl
{
private int index_ = 0;
private DataGridView dataGridView_ = null;
private bool valueChanged_ = false;
public ComboBoxControl()
: base()
{
this.SelectedIndexChanged += new EventHandler(HelpCombBoxControl_SelectedIndexChanged);
}
void HelpCombBoxControl_SelectedIndexChanged(object sender, EventArgs e)
{
NotifyDataGridViewOfValueChange();
}
protected virtual void NotifyDataGridViewOfValueChange()
{
this.valueChanged_ = true;
if (this.dataGridView_ != null)
{
this.dataGridView_.NotifyCurrentCellDirty(true);
}
}
#region IDataGridViewEditingControl members
public void ApplyCellStyleToEditingControl(DataGridViewCellStyle dataGridViewCellStyle)
{
this.Font = dataGridViewCellStyle.Font;
this.ForeColor = dataGridViewCellStyle.ForeColor;
this.BackColor = dataGridViewCellStyle.BackColor;
}
public DataGridView EditingControlDataGridView
{
get
{
return dataGridView_;
}
set
{
dataGridView_ = value;
}
}
public object EditingControlFormattedValue
{
get
{
return base.SelectedValue;
}
set
{
base.SelectedValue = value;
}
}
public int EditingControlRowIndex
{
get
{
return index_;
}
set
{
index_ = value;
}
}
public bool EditingControlValueChanged
{
get
{
return valueChanged_;
}
set
{
valueChanged_ = value;
}
}
public bool EditingControlWantsInputKey(Keys keyData, bool dataGridViewWantsInputKey)
{
if (keyData == Keys.Return)
return true;
switch (keyData & 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:
case Keys.Return:
return true;
default:
return false;
}
}
public Cursor EditingPanelCursor
{
get
{
return base.Cursor;
}
}
public object GetEditingControlFormattedValue(DataGridViewDataErrorContexts context)
{
return EditingControlFormattedValue;
}
public void PrepareEditingControlForEdit(bool selectAll)
{
}
public bool RepositionEditingControlOnValueChange
{
get { return false; }
}
#endregion
}
Second, we need to set the EditMode of the DataGridView to EditProgrammatically as follows: this.dataGridView2.EditMode = DataGridViewEditMode.EditProgrammatically;
Then handle the CellDoubleClick event to initial the edit mode: private void dataGridView2_CellDoubleClick(object sender, DataGridViewCellEventArgs e)
{
//Show the combo box.
this.dataGridView2.BeginEdit(false);
}
Let me know if this helps. Aland Li
Please mark the replies as answers if they help and unmark if they don't. This can be beneficial to other community members reading the thread. | | Aland Li Wednesday, August 12, 2009 11:45 AM | Hi jabdgreat,
I found another simple solution. These are the steps:
1. Add a DataGridViewTextBoxColumn instead of a DataGridViewComboBoxColumn.
2. Add a ComboBox to the form and hide it.
3. Handle the CellDoubleClick event. In the handler, locate the ComboBox to the position of the current cell, set its value to the text of the current cell and show it.
This is the code snippet: private ComboBox combobox1 = new ComboBox();
private void Form_load(object sender, EventArgs e)
{
//TODO: Set the data source, display member, value member of the combobox.
combobox1.Hide();
this.datagridview1.Controls.Add(combobox1);
}
private void dataGridView2_CellDoubleClick(object sender, DataGridViewCellEventArgs e)
{
//Show the combo box.
this.combobox1.Location = this.datagridview1.GetCellDisplayRectangle(e.ColumnIndex, e.RowIndex, true).Location;
this.combobox1.SelectedValue = this.datagridview1.CurrentCell.Value;
this.combobox1.Show();
}
Best regards, Aland Li
Please mark the replies as answers if they help and unmark if they don't. This can be beneficial to other community members reading the thread. - Marked As Answer byAland LiMSFT, ModeratorFriday, August 14, 2009 5:21 AM
-
| | Aland Li Friday, August 14, 2009 5:00 AM |
|