I am using a class that inherits from the
DataGridTextBoxColumn.
It adds a combo box into the column where it displays a
selection of choices.
The problem I have is that when this comboBox loses focus
I lose the selected text.
And this next line isn't setting the text in the cell.
// code within the custom DataGridComboBoxColumn class
this.TextBox.Text = this.ComboBox.SelectedItem.ToString();
The results do not show up in the cell on the grid it
remains empty.
I've queried the SelectedItem.ToString() and it does show
the correct string but somehow this isn't translating into
the cell.
Does anyone know what i'm missing here or can speculate in
constructive manner?
Many thanks
jax
public class DataGridComboBoxColumn : DataGridTextBoxColumn
{
// Hosted combobox control
private ComboBox comboBox;
private CurrencyManager cm;
private int iCurrentRow;
// Constructor - create combobox,
// register selection change event handler,
// register lose focus event handler
public DataGridComboBoxColumn()
{
this.cm = null;
// Create combobox and force DropDownList style
this.comboBox = new ComboBox();
this.comboBox.DropDownStyle = ComboBoxStyle.DropDownList;
// Add event handler for notification when combobox loses
focus
this.comboBox.Leave += new EventHandler(comboBox_Leave);
}
// Property to provide access to combobox
public ComboBox ComboBox
{
get { return comboBox;}
}
// On edit, add scroll event handler, and display combobox
protected override void Edit
(System.Windows.Forms.CurrencyManager source, int rowNum,
System.Drawing.Rectangle bounds, bool readOnly,
string instantText, bool cellIsVisible)
{
base.Edit(source, rowNum, bounds, readOnly, instantText,
cellIsVisible);
if (!readOnly && cellIsVisible)
{
// Save current row in the DataGrid and currency manager
// associated with the data source for the DataGrid
this.iCurrentRow = rowNum;
this.cm = source;
// Add event handler for DataGrid scroll notification
this.DataGridTableStyle.DataGrid.Scroll += new EventHandler
(DataGrid_Scroll);
// Site the combobox control within the current cell
this.comboBox.Parent = this.TextBox.Parent;
Rectangle rect =
this.DataGridTableStyle.DataGrid.GetCurrentCellBou nds();
this.comboBox.Location = rect.Location;
this.comboBox.Size = new Size(this.TextBox.Size.Width,
this.comboBox.Size.Height);
// Set combobox selection to given text
this.comboBox.SelectedIndex = this.comboBox.FindStringExact
(this.TextBox.Text);
// Make the combobox visible and place on top textbox
control
this.comboBox.Show();
this.comboBox.BringToFront();
this.comboBox.Focus();
}
}
// On DataGrid scroll, hide the combobox
private void DataGrid_Scroll(object sender, EventArgs e)
{
this.comboBox.Hide();
}
// On combobox losing focus, set the column value, hide
the combobox,
// and unregister scroll event handler
private void comboBox_Leave(object sender, EventArgs e)
{
if(this.comboBox.SelectedItem != null)
{
DataRowView rowView = (DataRowView)
this.comboBox.SelectedItem;
string s = (string) rowView.Row
[this.comboBox.DisplayMember];
Invalidate();
// The next three lines dont work
this.ReadOnly = false;
TextBox.Text = s;
TextBox.Show();
this.comboBox.Hide();
this.DataGridTableStyle.DataGrid.Scroll -= new EventHandler
(DataGrid_Scroll);
}
}
And I implement the class like so:
Tools.DataGridComboBoxColumn ComboCol = new
Tools.DataGridComboBoxColumn();
ComboCol.MappingName = "Type";
ComboCol.HeaderText = "Type of mortgage";
ComboCol.ComboBox.BackColor = Color.AliceBlue;
ComboCol.Width = 150;
ComboCol.ReadOnly = false;
ComboCol.ComboBox.DataSource = DataMortgageType;
ComboCol.ComboBox.DisplayMember = "Type";
ComboCol.ComboBox.ValueMember = "ID";
// dts is a datagridtablestyle which then is added to the
grid
dts.GridColumnStyles.Add(ComboCol);
The original class was designed to be databound so I'm
assuming i have the wrong end of the stick somewhere.