473,390 Members | 1,348 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,390 software developers and data experts.

ComboBox in DataGrid,

I want to implement a key hit with enter to dropdown a combobox that is in
the datagrid. in this case I need to override its original behaviours. I
found some codes from the web. Does anyone know how to use this code? please
help!

http://www.experts-exchange.com/Prog..._20862953.html


using System;
using System.Windows.Forms;
using System.Drawing;
using System.Data;
using System.Collections;
using System.Diagnostics;
using System.Reflection;

namespace AdvancedDataGrid
{
/// <summary>
/// Implementation of a ComboBox as a column in a DataGrid
/// </summary>
public class DataGridDataBoundComboBoxColumn : DataGridTextBoxColumn
{
private ComboBox internalComboBox = new ComboBox();
private bool currentlyInEdit = false;
private CurrencyManager source = null;
private int rowNum = -1;
private AdvancedDataGrid.MyDataGrid dataGrid = null;

#region Properties
private object nullValue = DBNull.Value;
/// <summary>
/// Gives a value that will be considered null and will be replaced for
showing by NullValue
/// </summary>
public object NullValue
{
set {this.nullValue = value;}
get {return this.nullValue;}
}

private object nullCorrespondingValue = null;
/// <summary>
/// Gives a value that will replace the null values (given by NullValue)
for showing
/// </summary>
public object NullCorrespondingValue
{
set {this.nullCorrespondingValue = value;}
get {return this.nullCorrespondingValue;}
}

public object DataSource
{
set
{
// If the display member or value member is wrong,
// this line will clear it
this.internalComboBox.DataSource = value;
SetComboBoxDropDownWidth();
}
get { return this.internalComboBox.DataSource; }
}
public string DisplayMember
{
set
{
this.internalComboBox.DisplayMember = value;
SetComboBoxDropDownWidth();
}
get { return this.internalComboBox.DisplayMember; }
}
public string ValueMember
{
set {this.internalComboBox.ValueMember = value; }
get { return this.internalComboBox.ValueMember; }
}
#endregion

#region Construction
public DataGridDataBoundComboBoxColumn () : base()
{
this.internalComboBox.Visible = false;
this.internalComboBox.DropDownStyle = ComboBoxStyle.DropDownList;
// Some events that guarantee correct working
this.internalComboBox.LostFocus += new EventHandler
(OnInternalComboBoxLostFocus);
}
#endregion

#region Event handlers for the combo box
private void OnInternalComboBoxLostFocus (object sender, EventArgs e)
{
if ((sender is DataGrid && !this.internalComboBox.Focused) ||
sender == this.internalComboBox)
{
if (this.currentlyInEdit)
Commit (this.source, this.rowNum);
}
}
#endregion

#region Key management
/// <summary>
/// Handles opening and closing of the ComboBox with the keyboard
/// For this function to work, the data grid should be of type
AdvancedDataGrid
/// </summary>
/// <param name="msg"></param>
/// <param name="keyData"></param>
/// <returns></returns>
public bool ProcessCmdKey(ref Message msg, Keys keyData)
{
if (!this.currentlyInEdit)
return false;

if (keyData == Keys.Down)
{
// Try to select next or the first item
if (this.internalComboBox.SelectedIndex <
this.internalComboBox.Items.Count - 1)
this.internalComboBox.SelectedIndex++;
else if (this.internalComboBox.Items.Count > 0)
this.internalComboBox.SelectedIndex = 0;
return true;
}
else if (keyData == Keys.Up)
{
// Try to select previous or the last item
if (this.internalComboBox.SelectedIndex > 0)
this.internalComboBox.SelectedIndex--;
else if (this.internalComboBox.Items.Count > 0)
this.internalComboBox.SelectedIndex =
this.internalComboBox.Items.Count - 1;
return true;
}
else if ((keyData == (Keys.Up | Keys.Alt) ||
keyData == (Keys.Down | Keys.Alt)) &&
!this.internalComboBox.DroppedDown)
{
this.internalComboBox.DroppedDown = true;
return true;
}
// Keys.Down | Keys.Alt work automatically if DropedDown
else if ((keyData == Keys.Enter || keyData == (Keys.Up | Keys.Alt)) &&
this.internalComboBox.DroppedDown)
{
HideDropDown();
return true;
}
else if (keyData == Keys.Escape)
{
Abort (this.rowNum);
return true;
}

return false;
}
#endregion

#region Overriden methods

protected override object GetColumnValueAtRow (CurrencyManager source, int
rowNum)
{
object val = base.GetColumnValueAtRow (source, rowNum);
// Check for the null value
if (Object.Equals (val, this.NullValue) && this.NullCorrespondingValue !=
null)
val = this.NullCorrespondingValue;

foreach (object o in this.internalComboBox.Items)
{
object oVal = GetValueMember (o);
if ((oVal == null && val == null) ||
(oVal != null && oVal.Equals (val)))
return GetDisplayMember (o);
}
return DBNull.Value;
}

protected override void Abort(int rowNum)
{
// If in read only mode, just follow the basic methods
if (this.DataGridTableStyle.DataGrid.ReadOnly || this.ReadOnly ||
(this.DataGridTableStyle.DataGrid.DataSource is DataView &&
!((DataView)this.DataGridTableStyle.DataGrid.DataS ource).AllowEdit))
{
base.Abort (rowNum);
return;
}

if(this.currentlyInEdit)
{
this.currentlyInEdit = false;
this.source = null;
this.rowNum = -1;
this.internalComboBox.Hide();
if (this.internalComboBox.DroppedDown)
HideDropDown();
}
}

protected override bool Commit(CurrencyManager dataSource,int rowNum)
{
// If in read only mode, just follow the basic methods
if (this.DataGridTableStyle.DataGrid.ReadOnly || this.ReadOnly ||
(this.DataGridTableStyle.DataGrid.DataSource is DataView &&
!((DataView)this.DataGridTableStyle.DataGrid.DataS ource).AllowEdit))
return base.Commit(dataSource, rowNum);

if(this.currentlyInEdit)
{
object selValue = this.internalComboBox.SelectedValue;
if (selValue == null || Object.Equals (selValue,
this.NullCorrespondingValue))
selValue = this.NullValue;
SetColumnValueAtRow(dataSource, rowNum, selValue);

this.currentlyInEdit = false;
this.source = null;
this.rowNum = -1;
this.internalComboBox.Hide();
if (this.internalComboBox.DroppedDown)
HideDropDown();
}

return true;
}

protected override void Edit(CurrencyManager source, int rowNum, Rectangle
bounds, bool readOnly, string instantText, bool cellIsVisible)
{
// If in read only mode, just follow the basic methods
if (this.DataGridTableStyle.DataGrid.ReadOnly || this.ReadOnly ||
(this.DataGridTableStyle.DataGrid.DataSource is DataView &&
!((DataView)this.DataGridTableStyle.DataGrid.DataS ource).AllowEdit))
{
base.Edit(source, rowNum, bounds, readOnly, instantText, cellIsVisible);
return;
}

if (this.currentlyInEdit)
return;

if(!cellIsVisible)
return;

// Important: show it before setting the selected value
// otherwise in some cases the value from the previous edit is returned
// (when the drop down is open with the mouse, a new value is pointed
// and Tab is pressed)
this.internalComboBox.Bounds = bounds;
this.internalComboBox.Show();

// Get the underlying value
// The current implementation of GetColumnValueAtRow will return the
DisplayMember
object val = base.GetColumnValueAtRow (source, rowNum);
// Check for the null value
if (Object.Equals (val, this.NullValue) && this.NullCorrespondingValue !=
null)
val = this.NullCorrespondingValue;

if (val == null || val == DBNull.Value)
if (this.internalComboBox.Items.Count > 0)
this.internalComboBox.SelectedIndex = 0;
else
this.internalComboBox.SelectedIndex = -1;
else
this.internalComboBox.SelectedValue = val;

this.currentlyInEdit = true;
this.source = source;
this.rowNum = rowNum;

ColumnStartedEditing (this.internalComboBox);
}

protected override int GetMinimumHeight()
{
return this.internalComboBox.PreferredHeight + 1;
}

protected override void SetDataGridInColumn(DataGrid value)
{
base.SetDataGridInColumn (value);

if (value == this.dataGrid)
return;

Debug.Assert (value != null);
Debug.Assert (value is AdvancedDataGrid.MyDataGrid,"The used data grid
should be of type AdvancedDataGrid");

if (this.dataGrid != null)
{
this.dataGrid.Controls.Remove (this.internalComboBox);
this.dataGrid.LostFocus -= new EventHandler
(OnInternalComboBoxLostFocus);
}

this.dataGrid = (AdvancedDataGrid.MyDataGrid)value;

this.dataGrid.Controls.Add(this.internalComboBox);
// Some events that guarantee correct working
this.dataGrid.LostFocus += new EventHandler
(OnInternalComboBoxLostFocus);
}
#endregion

#region Helper functions
private void HideDropDown()
{
// Unknown why, the value is lost when DroppedDown is set to false
object val = this.internalComboBox.SelectedValue;
this.internalComboBox.DroppedDown = false;
this.internalComboBox.SelectedValue = val;
}
private object GetMember (object o, string member)
{
DataRow row = null;
if (o is DataRowView)
row = ((DataRowView)o).Row;
if (o is DataRow)
row = (DataRow)o;
if (row != null)
{
object ret = row[member];
if (ret == null)
return DBNull.Value;
else
return ret;
}
else
{
PropertyInfo pi = o.GetType().GetProperty (member);
Debug.Assert (pi != null);
return pi.GetValue (o, null);
}
}

private string GetDisplayMember (object o)
{
if (o == null || o == DBNull.Value)
return this.NullText;
object val = GetMember (o, this.internalComboBox.DisplayMember);
if (val == null || val == DBNull.Value)
return this.NullText;
else
return val.ToString();
}

private object GetValueMember (object o)
{
return GetMember (o, this.internalComboBox.ValueMember);
}

/// <summary>
/// Sets the DropDownWidth property of the combo box if all needed
/// properties are already set
/// </summary>
private void SetComboBoxDropDownWidth()
{
if (this.internalComboBox.DataSource == null ||
this.internalComboBox.DisplayMember == null ||
this.internalComboBox.DisplayMember == String.Empty)
return;

Graphics g = this.internalComboBox.CreateGraphics ();
float w = 0;
foreach (object o in this.internalComboBox.Items)
w = Math.Max (w, g.MeasureString (GetDisplayMember (o),
this.internalComboBox.Font).Width);
if (w > 0)
this.internalComboBox.DropDownWidth = (int)(w + 0.5);
}
#endregion
}
}
Nov 16 '05 #1
2 4302
What exactly is your question? Do you want to know how to integrate this
code into your own application so that you can have a combo box in a grid or
does your question have to do with using the enter key to activate the
dropdown? Or is your question as you ask it: Do you want to know if we know
how to use that code? I doubt that's really your question.

If you can be more specific about your question, you're more likely to get a
response.

Pete
"pei_world" <pe*******@hotmail.com> wrote in message
news:ev**************@TK2MSFTNGP14.phx.gbl...
I want to implement a key hit with enter to dropdown a combobox that is in
the datagrid. in this case I need to override its original behaviours. I
found some codes from the web. Does anyone know how to use this code? please help!

[snip]
Nov 16 '05 #2
sorry about my question, I have integrated a combobox into the datagrid. But
when I try to use keyboard to control the datagrid, after I press the Enter
key, combobox dropped down, however, when I try to select the next item on
the combobox, it move to the next row of the datagrid. how can I use this
code to override its default action.
what I try to do are:
1.press Enter key to show Combobox Dropdown list
2.Press UP or Down key to select item
3.Press Enter key to hide Dropdown list

Also, How to use this code in my application? I am using other code to
integrate combobox into datagrid.
"Pete Davis" <pd******@NOSPAM.hotmail.com> wrote in message
news:F9********************@giganews.com...
What exactly is your question? Do you want to know how to integrate this
code into your own application so that you can have a combo box in a grid
or
does your question have to do with using the enter key to activate the
dropdown? Or is your question as you ask it: Do you want to know if we
know
how to use that code? I doubt that's really your question.

If you can be more specific about your question, you're more likely to get
a
response.

Pete
"pei_world" <pe*******@hotmail.com> wrote in message
news:ev**************@TK2MSFTNGP14.phx.gbl...
I want to implement a key hit with enter to dropdown a combobox that is
in
the datagrid. in this case I need to override its original behaviours. I
found some codes from the web. Does anyone know how to use this code?

please
help!

[snip]

Nov 16 '05 #3

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

Similar topics

0
by: Gamze | last post by:
Hi, In my vb.net windows application ,i have combobox which is populated by sqlserver database table.When i select value from combobox ,value saved in to other table of my database and i use to...
3
by: Bill C. | last post by:
Hello, I know this has been discussed a lot already because I've been searching around for information the last few weeks. I'm trying to implement a DataGridComboBoxColumn class. I've found...
2
by: john sutor | last post by:
Does anyone know how to create a combobox in a standard datagrid? I can create check boxes , but not the combobox
3
by: PeterZ | last post by:
G'day, After doing much searching and pinching bits of ideas from here there and everywhere I came up with a fairly 'clean' solution of including a comboBox into a dataGrid column. You can...
3
by: TT (Tom Tempelaere) | last post by:
Hay there, I'm writing my own DataGridComboBoxColumn because .NET 1.1 does not have one (I hope .NET 2.0 supplies one). I based it on this article:...
1
by: John Doe | last post by:
Now i know how to manually add a combobox to a datagrid, but how would i handle the recordset below? ID | FirstName | LastName | Job -------------------------------- 1 |Joe | Smith |...
0
by: dbuchanan | last post by:
ComboBox databindng Problem == How the ComboBox is setup and used: My comboBox is populated by a lookup table. The ValueMember is the lookup table's Id and the DisplayMember is the text from a...
1
by: fiaolle | last post by:
Hi The first set of source code is the class for a combobox in a grid, hopefully. In the second set of code we try to use the combobox class, but the grid is empty. I don't understand how this...
4
by: JJGarcia | last post by:
Hi Everyone, I'll try to explain the process I'm following, I'm new to this so I'm triying the easy way first, probably the lasyest too! I created a new Project, drag in to it a SQLConnection,...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
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
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
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
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...

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.