Hi all!
I went into this problem. I have a DataGridView with ComboBox, they are genereted dynamicaly.
The code looks like this:
DataGridViewComboBoxCell comboCell = new DataGridViewComboBoxCell();
DataTable dt1 = new DataTable();
dt1.Columns.Add( "number");
dt1.Columns.Add( "count");
for (int j = 0; j <= count; j++)
{
dt1.Rows.Add();
dt1.Rows[j][ "number"]=j.ToString();
dt1.Rows[j][ "count"] = j.ToString();
}
comboCell.ValueMember= "number";
comboCell.DisplayMember = "count";
comboCell.DataSource = dt1;
dataGridView1.Rows .Cells[ "count"] = comboCell;
What I need is to get event when the value in DataGridViewComboBoxCell is changed. I can get an event from datagrid CellValueChanged or CellEndEdit, but this is comming after the cell is loosing focus. The event should be handled imedietly after comboCell selected value is changed, like instandard ComboBox class.
Thanks fo any idea! | | kok_cz Monday, July 31, 2006 10:52 PM | hi,
you can use cell validating and check to see if this is the column you want to handle the event for something like this
private void dataGridView1_CellEnter(object sender, DataGridViewCellEventArgs e)
{
if (e.ColumnIndex == 3)
{
//your code goes here
}
also you can use editingcontrol property for datagridview to sign the event
dataGridView1.EditingControl.TextChanged += new EventHandler(EditingControl_TextChanged);
hope this helps
| | Egyptian Wednesday, August 02, 2006 6:34 AM | hi,
you can use cell validating and check to see if this is the column you want to handle the event for something like this
private void dataGridView1_CellEnter(object sender, DataGridViewCellEventArgs e)
{
if (e.ColumnIndex == 3)
{
//your code goes here
}
also you can use editingcontrol property for datagridview to sign the event
dataGridView1.EditingControl.TextChanged += new EventHandler(EditingControl_TextChanged);
hope this helps
| | Egyptian Wednesday, August 02, 2006 6:34 AM | Thanx Egyptian but, exactly what i want is to fire the event when i changed the comboBox value only. According to your suggestion event always raises whenever i enter into that cell. I didnt get any soulution.
Thanx-Nagu | | Nagu Friday, August 04, 2006 6:54 AM | hi,
this is an example which i allready have, try to change the event to fit your need
hope this helps | | Egyptian Friday, August 04, 2006 3:21 PM | Finaly I found this piece of code and I´m succesfully using for DataGridViewComboBoxCell aswell for CheckBoxCell
The Event is hanled right in time when checkBox clicked or comboBox value clicked (ofcourse in DataGridView)
Here is it for one editable comoboBox column in DataGridView (no other columns are editable):
void dataGridView1_CurrentCellDirtyStateChanged(object sender, EventArgs e)
{
if (dataGridView1.IsCurrentCellDirty)
{
dataGridView1.CommitEdit( DataGridViewDataErrorContexts.Commit);
//DoWhatYouWant();
}
}
Here is it for one editablecheckBox column in DataGridView (more complicated): First implement class:
public class DataGridViewDisableButtonCell : DataGridViewButtonCell
{
private bool enabledValue;
public bool Enabled
{
get
{
return enabledValue;
}
set
{
enabledValue = value;
}
}
// Override the Clone method so that the Enabled property is copied.
public override object Clone()
{
DataGridViewDisableButtonCell cell =
( DataGridViewDisableButtonCell)base.Clone();
cell.Enabled = this.Enabled;
return cell;
}
// By default, enable the button cell.
public DataGridViewDisableButtonCell()
{
this.enabledValue = true;
}
protected override void Paint(Graphics graphics,
Rectangle clipBounds, Rectangle cellBounds, int rowIndex,
DataGridViewElementStates elementState, object value,
object formattedValue, string errorText,
DataGridViewCellStyle cellStyle,
DataGridViewAdvancedBorderStyle advancedBorderStyle,
DataGridViewPaintParts paintParts)
{
// The button cell is disabled, so paint the border,
// background, and disabled button for the cell.
if (!this.enabledValue)
{
// Draw the cell background, if specified.
if ((paintParts & DataGridViewPaintParts.Background) ==
DataGridViewPaintParts.Background)
{
SolidBrush cellBackground =
new SolidBrush(cellStyle.BackColor);
graphics.FillRectangle(cellBackground, cellBounds);
cellBackground.Dispose();
}
// Draw the cell borders, if specified.
if ((paintParts & DataGridViewPaintParts.Border) ==
DataGridViewPaintParts.Border)
{
PaintBorder(graphics, clipBounds, cellBounds, cellStyle,
advancedBorderStyle);
}
// Calculate the area in which to draw the button.
Rectangle buttonArea = cellBounds;
Rectangle buttonAdjustment =
this.BorderWidths(advancedBorderStyle);
buttonArea.X += buttonAdjustment.X;
buttonArea.Y += buttonAdjustment.Y;
buttonArea.Height -= buttonAdjustment.Height;
buttonArea.Width -= buttonAdjustment.Width;
// Draw the disabled button.
ButtonRenderer.DrawButton(graphics, buttonArea,
PushButtonState.Disabled);
// Draw the disabled button text.
if (this.FormattedValue is String)
{
TextRenderer.DrawText(graphics,
( string)this.FormattedValue,
this.DataGridView.Font,
buttonArea, SystemColors.GrayText);
}
}
else
{
// The button cell is enabled, so let the base class
// handle the painting.
base.Paint(graphics, clipBounds, cellBounds, rowIndex,
elementState, value, formattedValue, errorText,
cellStyle, advancedBorderStyle, paintParts);
}
}
And use this:
public void dataGridView1_CellValueChanged(object sender,DataGridViewCellEventArgs e)
{
if (dataGridView1.Columns[e.ColumnIndex].Name == "CheckBoxes")
{
DataGridViewDisableButtonCell buttonCell = (DataGridViewDisableButtonCell)dataGridView1.Rows[e.RowIndex].Cells["Buttons"];
DataGridViewCheckBoxCell checkCell =
( DataGridViewCheckBoxCell)dataGridView1.
Rows[e.RowIndex].Cells[ "CheckBoxes"];
buttonCell.Enabled = !( Boolean)checkCell.Value;
dataGridView1.Invalidate();
}
}
void dataGridView1_CurrentCellDirtyStateChanged(object sender,EventArgs e)
{
if (dataGridView1.IsCurrentCellDirty)
{
dataGridView1.CommitEdit( DataGridViewDataErrorContexts.Commit);
//MessageBox.Show("CheckBox ValueChanged Just Now!!");
}
} - Proposed As Answer byakram mellIce Sunday, October 04, 2009 10:56 AM
-
| | kok_cz Tuesday, August 22, 2006 9:48 PM | Thanks a ton i was searching for this code
| | doonething2007 Monday, October 15, 2007 8:44 PM | Thanks alot, it was really helpful info Akram MellIce | | akram mellIce Sunday, October 04, 2009 10:56 AM |
|