473,385 Members | 2,013 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

problem with DataGridTextBoxColumn inherit

Jax
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.

Nov 15 '05 #1
0 2129

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Jaco Karsten | last post by:
I am writing a customized DataGrid control which will allow the user to only select an entire row (and not a cell inside the row). So I want to add readonly columns to the datagrid. To achieve...
3
by: clyyy2002 | last post by:
At added two column and more column hereafter,I immediately click del button,the program is no problem. but if i first click the DataGrid columnHeader and then click the del button, i find when...
0
by: Davewadd | last post by:
I'm having an odd problem with a datagrid in a c# program. The program maintains a schedule of jobs that need to be run. As jobs run, the status and next run date/time info are updated in a SQL...
2
by: Rob | last post by:
I currently use the datatable.ColumnChanging event to call my data validation method in a bound datagrid (Windows not Web). This is causing me problems since the user must click away from the...
6
by: Ron L | last post by:
I have a dataset whose source is a SQL 2k stored procedure that I am trying to display in a datagrid. This datasource has 4 columns that I am interested in here, a text column and 3 value columns...
4
by: pmclinn | last post by:
Below is the code I'm using to dynamically fill a datagrid from a datatable (fed by an ole connection). Everything is working great except that my users have to click 2 time on the checkbox to...
3
by: Jason Huang | last post by:
Hi, In my C# Windows form project. I am wondering can we manually define the width of a cell in a DataGrid? Thanks for help. Jason
1
by: Stephen Plotnick | last post by:
I have a checkbox in a data grid that does not change it's state until I actually leave the field. I've done this routine several times in other data grids without an issue. I'm using VB.2003. ...
4
LoanB
by: LoanB | last post by:
Hi gang, Im busy writing my firs Windows Mobile 6 Application. - I'm a beginner ok. My first problem: When I run the app through the emulator I get an error: Database file cannot be found. ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.