I'd like to disable item in ComboBox that is in a DataGridview cell.
I already know how to disable(or seems disabled) items in a ComboBox, using the DrawItem event and SelectedIndexChanged event but there is no similar event in DataGridViewComboBoxCell or DataGridViewComboBoxColumn.
So my question is, how to disable any item in ComboBox that is in a DataGridView?
In ComboBox I can modify items display that need to be disabled like this:
But can't do the same functionality in DataGridView:
I think the simplest option for you would be to handle the EditControlShowing event, and then handle the ComboBoxes SelectedIndexChanged event and do what you already know how to do.
When you setup the DataGridview in code, you can do this:
dataGridView1.EditingControlShowing += dataGridView1_EditingControlShowing;
And then implement the handler like:
void dataGridView1_EditingControlShowing(object sender, DataGridViewEditingControlShowingEventArgs e)
{
ComboBox combo = e.Control as ComboBox;
if (combo != null)
{
// Both of these lines are essential, otherwise you will be handling the same event twice in some conditions
combo.SelectedIndexChanged -= combo_SelectedIndexChanged;
combo.SelectedIndexChanged += combo_SelectedIndexChanged;
}
}
Finally, the SelectedIndexChanged event is handled exactly the way you want to:
void combo_SelectedIndexChanged(object sender, EventArgs e)
{
ComboBox thisCombo = sender as ComboBox;
if (thisCombo != null)
{
Debug.Print(thisCombo.Text);
}
}
Related
I want to create DataGridViewComboBoxColumn and attach to it's combo box Click event (i want to generate it's datasource on click only).
While I have no idea about why you need Click event of that ComboBox control,
you can access that combo box using EditingControlShowing event:
private void dataGridView1_EditingControlShowing(object sender, DataGridViewEditingControlShowingEventArgs e)
{
//Check if the event is for your column, for example column 1
if (this.dataGridView1.CurrentCell.ColumnIndex == 1)
{
var comboBox = e.Control as DataGridViewComboBoxEditingControl;
comboBox.Click -= comboBox_Click;
comboBox.Click += comboBox_Click;
}
}
private void comboBox_Click(object sender, EventArgs e)
{
var comboBox = sender as DataGridViewComboBoxEditingControl;
//use comboBox here
}
But you should know, you can set the DataSource for you column in CellClick event of your datagridview too:
private void dataGridView1_CellClick(object sender, DataGridViewCellEventArgs e)
{
if(e.ColumnIndex==1 && e.RowIndex>-1)
{
//Check if the event is for your column, for example column 1
var column = (DataGridViewComboBoxColumn)this.dataGridView1.Columns[e.ColumnIndex];
//use column.DataSource
}
}
And another important thing you should know is if you set a datasource that not contains the value of one of cells of this column, you will receive errors while rendering the column.
For example, I have 100 comboboxes and need to update a particular combobox when it's selected a new index. How can I use only one method to catch the event when there is a combobox set a new value?
Create only one event handler ComboBox_SelectedIndexChanged, and subscribe all the combo boxes to this event:
combobox1.SelectedIndexChanged += ComboBox_SelectedIndexChanged;
combobox2.SelectedIndexChanged += ComboBox_SelectedIndexChanged;
combobox3.SelectedIndexChanged += ComboBox_SelectedIndexChanged;
combobox4.SelectedIndexChanged += ComboBox_SelectedIndexChanged;
//and so on
The event handler code:
private void ComboBox_SelectedIndexChanged(object sender, EventArgs e)
{
//now "sender" is the reference to the combo box raised the event
//so just cast it
ComboBox combobox = sender as ComboBox;
//now access it as you want
}
I want to bind a ComboBox with checked items from CheckedListBox based on selection made by user.
This is how I bind ComboBox:
private void LoadFOCOutlets()
{
ArrayList outletList = new ArrayList();
Outlet objOutlet = new Outlet();
for (int i = 0; i < customCheckListBoxOutletList.CheckedItems.Count; i++)
{
objOutlet = (Outlet)customCheckListBoxOutletList.Items[i];
outletList.Add(objOutlet);
}
objOutlet.OutletID = 0;
objOutlet.OutletName = "Select Outlet";
outletList.Insert(0, objOutlet);
cmbFOCOutlets.DataSource = outletList;
cmbFOCOutlets.DisplayMember = "OutletName";
cmbFOCOutlets.ValueMember = "OutletID";
cmbFOCOutlets.DropDownStyle = ComboBoxStyle.DropDownList;
}
So, every time when a user check a new item, it should re-bind the ComboBox. The above code works fine.
But which event of CheckedListBox can I use to re-bind the ComboBox after a new item has been checked? I tried using ItemCheck Event. But It doesn't count the current selection.
Any help will be very much appreciated.
Try this event
private void CheckedListBox1_SelectedIndexChanged(object sender, EventArgs e)
{
//Your code here
}
(Or)
private void CheckedListBox1_ItemCheck(object sender, EventArgs e)
{
//Your code here
}
Refer This
The following is excerpted from
Which CheckedListBox event triggers after a item is checked?
Which CheckedListBox event triggers after a item is checked?
(answer #3)
softburger states:
I tried this and it worked:
(seems to work for me, too)
private void clbOrg_ItemCheck(object sender, ItemCheckEventArgs e)
{
CheckedListBox clb = (CheckedListBox)sender;
// Switch off event handler
clb.ItemCheck -= clbOrg_ItemCheck;
clb.SetItemCheckState(e.Index, e.NewValue);
// Switch on event handler
clb.ItemCheck += clbOrg_ItemCheck;
// Now you can go further
CallExternalRoutine();
}
The idea is, as mentioned in many posts, CheckedListBox has an ItemCheck event, but no ItemChecked event.
To get around this,
the ItemCheck handler assignment is briefly suspended(within the ItemCheck handler routine itself (!?)),
during which time the CheckedListBox's SetItemCheckState method is invoked for the newly checked item, (which should place the item in the CheckedListBox's CheckedItems collection)
and then the ItemCheck handler is then reassigned.
i.e.
// Switch off event handler
clb.ItemCheck -= clbOrg_ItemCheck;
clb.SetItemCheckState(e.Index, e.NewValue);
// Switch on event handler
clb.ItemCheck += clbOrg_ItemCheck;
and now you can (finally) get all theCheckedListBox Checked Items from its CheckedItems collection. (great hack, if you ask me)
I have a DataGridView with two columns (DataGridViewTextBoxColumn and DataGRidViewComboBoxColumn). If I click on a cell in the textbox column and scroll with the mousewheel, the grid scrolls. This is perfect.
If I click on a cell in the combobox column, the mousewheel will scroll the items in the combobox. I need to scroll the datagridview instead.
In my attempt to fix I can disable the scrolling in the combobox by handling the EditingControlShowing event :
private void SeismicDateGridView_EditingControlShowing(object sender, DataGridViewEditingControlShowingEventArgs e)
{
if (e.Control is IDataGridViewEditingControl)
{
dgvCombo = (IDataGridViewEditingControl) e.Control;
((System.Windows.Forms.ComboBox)dgvCombo).MouseWheel -= new MouseEventHandler(DGVCombo_MouseWheel);
((System.Windows.Forms.ComboBox)dgvCombo).MouseWheel += new MouseEventHandler(DGVCombo_MouseWheel);
}
}
private void DGVCombo_MouseWheel(object sender, System.Windows.Forms.MouseEventArgs e)
{
HandledMouseEventArgs mwe = (HandledMouseEventArgs)e;
mwe.Handled = true;
}
Any ideas how to scroll the DataGridView when the DataGridViewComboBox column is active ?
Have you considered handling the ComboBox's DropDownClosed event and changing the focus to the parent?
void DateGridView_CellClick(object sender, DataGridViewCellEventArgs e)
{
System.Windows.Forms.ComboBox comboBox = dataGridView.EditingControl as System.Windows.Forms.ComboBox;
if (comboBox != null)
{
comboBox.DropDownClosed += comboBox_DropDownClosed;
}
}
void comboBox_DropDownClosed(object sender, EventArgs e)
{
(sender as System.Windows.Forms.ComboBox).DropDownClosed -= comboBox_DropDownClosed;
(sender as System.Windows.Forms.ComboBox).Parent.Focus();
}
If you want to scroll the DataGridView before you select a cell but while the ComboBox is still dropped down, that would be a different situation but judging by what you said here:
If I click on a cell in the combobox column, the mousewheel will
scroll the items in the combobox.
I am assuming that you simply want to change the focus once a selection has been made.
You could redirect the input using P/Invoke like here. Or you could subclass the DataGridView to add a Scroll Method to it which calls the base class's OnMouseWheel method, which you could then call from DGVCombo_MouseWheel. Example here.
I think the second option is probably the most elegant, no reason to use PInvoke.
Here it is done with inline functions. And handled case, when combo box is dropped down:
dgv.EditingControlShowing += (s, e) =>
{
DataGridViewComboBoxEditingControl editingControl = e.Control as DataGridViewComboBoxEditingControl;
if (editingControl != null)
editingControl.MouseWheel += (s2, e2) =>
{
if (!editingControl.DroppedDown)
{
((HandledMouseEventArgs)e2).Handled = true;
dgv.Focus();
}
};
};
I want to display a message when items in DataGridViewComboBox has been changed. I am able to perform it partially by taking help of datagridview CellbeginEdit event and CellEndEdit event but that is not up to mark. I want it as it happen in combobox selection change event.
I had google it for solving but not get appropriate help.
Any help will be appriciated.
use EditingControlShowing event for it
private void grvList_EditingControlShowing(object sender, DataGridViewEditingControlShowingEventArgs e)
{
if (grvList.Columns[grvList.CurrentCell.ColumnIndex].Name.Equals("routing_ID"))
{
ComboBox cmbprocess = e.Control as ComboBox;
cmbprocess.SelectedIndexChanged += new EventHandler(grvcmbProcess_SelectedIndexChanged);
}
}
private void grvcmbProcess_SelectedIndexChanged(object sender, EventArgs e)
{
ComboBox cmbprocess = (ComboBox)sender;
if (cmbprocess.SelectedValue != null)
{
/// Your Code goes here
}
}
this is only an example program to show how to do it