By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
424,680 Members | 1,475 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 424,680 IT Pros & Developers. It's quick & easy.

problem with DataGridTextBoxColumn inherit

P: n/a
I am using a class that inherits from the
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


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()
{ = null;

// Create combobox and force DropDownList style
this.comboBox = new ComboBox();
this.comboBox.DropDownStyle = ComboBoxStyle.DropDownList;
// Add event handler for notification when combobox loses
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,

if (!readOnly && cellIsVisible)
// Save current row in the DataGrid and currency manager
// associated with the data source for the DataGrid
this.iCurrentRow = rowNum; = source;

// Add event handler for DataGrid scroll notification
this.DataGridTableStyle.DataGrid.Scroll += new EventHandler

// 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,

// Set combobox selection to given text
this.comboBox.SelectedIndex = this.comboBox.FindStringExact

// Make the combobox visible and place on top textbox

// On DataGrid scroll, hide the combobox
private void DataGrid_Scroll(object sender, EventArgs e)

// 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)
string s = (string) rowView.Row
// The next three lines dont work
this.ReadOnly = false;
TextBox.Text = s;
this.DataGridTableStyle.DataGrid.Scroll -= new EventHandler

And I implement the class like so:

Tools.DataGridComboBoxColumn ComboCol = new
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

The original class was designed to be databound so I'm
assuming i have the wrong end of the stick somewhere.

Nov 15 '05 #1
Share this question for a faster answer!
Share on Google+

This discussion thread is closed

Replies have been disabled for this discussion.