I am using a class that inherits from the
DataGridTextBox Column.
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 DataGridComboBo xColumn class
this.TextBox.Te xt = this.ComboBox.S electedItem.ToS tring();
The results do not show up in the cell on the grid it
remains empty.
I've queried the SelectedItem.To String() 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 DataGridComboBo xColumn : DataGridTextBox Column
{
// 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 DataGridComboBo xColumn()
{
this.cm = null;
// Create combobox and force DropDownList style
this.comboBox = new ComboBox();
this.comboBox.D ropDownStyle = ComboBoxStyle.D ropDownList;
// Add event handler for notification when combobox loses
focus
this.comboBox.L eave += new EventHandler(co mboBox_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.Currency Manager source, int rowNum,
System.Drawing. Rectangle bounds, bool readOnly,
string instantText, bool cellIsVisible)
{
base.Edit(sourc e, 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.iCurrentRo w = rowNum;
this.cm = source;
// Add event handler for DataGrid scroll notification
this.DataGridTa bleStyle.DataGr id.Scroll += new EventHandler
(DataGrid_Scrol l);
// Site the combobox control within the current cell
this.comboBox.P arent = this.TextBox.Pa rent;
Rectangle rect =
this.DataGridTa bleStyle.DataGr id.GetCurrentCe llBounds();
this.comboBox.L ocation = rect.Location;
this.comboBox.S ize = new Size(this.TextB ox.Size.Width,
this.comboBox.S ize.Height);
// Set combobox selection to given text
this.comboBox.S electedIndex = this.comboBox.F indStringExact
(this.TextBox.T ext);
// Make the combobox visible and place on top textbox
control
this.comboBox.S how();
this.comboBox.B ringToFront();
this.comboBox.F ocus();
}
}
// On DataGrid scroll, hide the combobox
private void DataGrid_Scroll (object sender, EventArgs e)
{
this.comboBox.H ide();
}
// 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.comboBo x.SelectedItem != null)
{
DataRowView rowView = (DataRowView)
this.comboBox.S electedItem;
string s = (string) rowView.Row
[this.comboBox.D isplayMember];
Invalidate();
// The next three lines dont work
this.ReadOnly = false;
TextBox.Text = s;
TextBox.Show();
this.comboBox.H ide();
this.DataGridTa bleStyle.DataGr id.Scroll -= new EventHandler
(DataGrid_Scrol l);
}
}
And I implement the class like so:
Tools.DataGridC omboBoxColumn ComboCol = new
Tools.DataGridC omboBoxColumn() ;
ComboCol.Mappin gName = "Type";
ComboCol.Header Text = "Type of mortgage";
ComboCol.ComboB ox.BackColor = Color.AliceBlue ;
ComboCol.Width = 150;
ComboCol.ReadOn ly = false;
ComboCol.ComboB ox.DataSource = DataMortgageTyp e;
ComboCol.ComboB ox.DisplayMembe r = "Type";
ComboCol.ComboB ox.ValueMember = "ID";
// dts is a datagridtablest yle which then is added to the
grid
dts.GridColumnS tyles.Add(Combo Col);
The original class was designed to be databound so I'm
assuming i have the wrong end of the stick somewhere.