Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations wOOdy-Soft on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Derived comboBox in DataGrid. KeyDown doesnt work.

Status
Not open for further replies.

lak201

Programmer
Oct 6, 2004
10
US
hi everybody.
i wanted to have comboboxes in my datagrid. so what i did was to
derive a custom column style from the DataGridTextBoxColumn class, which i got from syncfusion.

private void makeDataGridWithComboBox()
{

System.Data.DataTable dt = new System.Data.DataTable("trade grid");
DataGrid dataGridBulkTrades=new DataGrid();

System.Windows.Forms.DataGridTableStyle displayStyle = new System.Windows.Forms.DataGridTableStyle();
System.Windows.Forms.DataGridColumnStyle columnStyles = new System.Windows.Forms.DataGridColumnStyle();

//Derive the columnStyle -SUBACCOUNT
DataGridComboBoxColumn subAccountColumn = new DataGridComboBoxColumn();

subAccountColumn.ColumnComboBox.DataSource = someRandomTable;

subAccountColumn.MappingName = "SubAccount";
dt.Columns.Add("SubAccount");
columnStyles = subAccountColumn;

displayStyle.GridColumnStyles.Add(columnStyles);
displayStyle.MappingName = "trade grid";

dataGridBulkTrades.TableStyles.Add(displayStyle);

dataGridBulkTrades.SetDataBinding(dt, null);

}

To make this work, set someRandomTable as something that implements the IListInterface and it should work. You would also need the source code for the DataGridComboBoxColumn class. I have attached the source code below:


public class DataGridComboBoxColumn : System.Windows.Forms.DataGridTextBoxColumn
{
public NoKeyUpCombo ColumnComboBox = null;
private System.Windows.Forms.CurrencyManager _source = null;
private int _rowNum;
private bool _isEditing = false;

public DataGridComboBoxColumn() : base()
{
ColumnComboBox = new NoKeyUpCombo();

ColumnComboBox.KeyDown+=new System.Windows.Forms.KeyEventHandler(keydown);
ColumnComboBox.Leave += new EventHandler(LeaveComboBox);
ColumnComboBox.SelectionChangeCommitted += new System.EventHandler(ComboStartEditing);
}

private void ComboStartEditing(object sender, EventArgs e)
{
_isEditing = true;
base.ColumnStartedEditing((System.Windows.Forms.Control) sender);
}

private void keydown(object sender, System.Windows.Forms.KeyEventArgs e)
{
int a =10;
}

private void LeaveComboBox(object sender, EventArgs e)
{
if(_isEditing)
{
SetColumnValueAtRow(_source, _rowNum, ColumnComboBox.Text);
_isEditing = false;
Invalidate();
}
ColumnComboBox.Hide();


}

protected override void Edit(System.Windows.Forms.CurrencyManager source, int rowNum, System.Drawing.Rectangle bounds, bool readOnly, string instantText, bool cellIsVisible)
{
if (ColumnComboBox.Visible==true) return;


base.Edit(source,rowNum, bounds, readOnly, instantText , cellIsVisible);

_rowNum = rowNum;
_source = source;

ColumnComboBox.Parent = this.TextBox.Parent;
ColumnComboBox.Location = this.TextBox.Location;//.Offset(-1,-1);
ColumnComboBox.Size = new System.Drawing.Size(this.TextBox.Size.Width, ColumnComboBox.Size.Height);
ColumnComboBox.Text = this.TextBox.Text;


this.TextBox.Visible = false;
ColumnComboBox.Visible = true;
ColumnComboBox.BringToFront();
ColumnComboBox.Focus();
}

protected override bool Commit(System.Windows.Forms.CurrencyManager dataSource, int rowNum)
{
if(_isEditing)
{
_isEditing = false;
SetColumnValueAtRow(dataSource, rowNum, ColumnComboBox.Text);
}
return true;
}

}

public class NoKeyUpCombo : System.Windows.Forms.ComboBox
{
const int WM_KEYUP = 0x101;
protected override void WndProc(ref System.Windows.Forms.Message m)
{
if(m.Msg == WM_KEYUP)
{
//ignore keyup to avoid problem with tabbing & dropdownlist;
return;
}
base.WndProc(ref m);
}
}

Please note that this code is courtesy of syncfusion.

this thing works pretty good except for a small problem.
the problem lies with the KeyDown event in the comboBox. Instead of going to the next item in the list attached to the combobox, it goes to the next row in the datagrid. does anyone know how to change the code for the DataGridTextBoxColumn class so that it goes to the next item in the list attached to the combobox rather than going to the next row of the datagrid when i press the down arrow on the keyboard.

thanks and i hope this is clear enough.
 
Would this work:

1. Trap the keypress event for the datagrid
2. Test whether the current cell is in the combo box column
3. If it is, 'handle' the keypress (thus cancelling it) and
4. Find the selectedindex of the combo in the current cell and add 1 to it.

Regards

Nelviticus
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top