DataGridView ComboBox column selection changed event - c#

I have a datagrid with 2 combobox columns. I wrote selection changed event for the combobox column as follows.
private void Grid_EditingControlShowing(object sender, DataGridViewEditingControlShowingEventArgs e)
{
ComboBox cb = e.Control as ComboBox;
if (cb!=null)
{ cb.SelectionChangeCommitted -= new EventHandler(cb_SelectedIndexChanged);
// now attach the event handler
cb.SelectionChangeCommitted += new EventHandler(cb_SelectedIndexChanged);
}
}
void cb_SelectedIndexChanged(object sender, EventArgs e)
{
var tb = datagrdADDTEMP.EditingControl as ComboBox;
if (tb != null)
str = tb.SelectedValue != null ? tb.SelectedValue.ToString() : null;
Assesment_Business_layer.Businesslayer bl = new Assesment_Business_layer.Businesslayer();
DataSet ds = new DataSet();**strong text**
ds = bl.GetSubCatNamesBA(str);
cmbDataGridSubCategory.DataSource = ds.Tables[0];
cmbDataGridSubCategory.DisplayMember = "SubCategoryName";
cmbDataGridSubCategory.ValueMember = "SubCategoryCode";
}
}
its working well with the first combobox column but the problem is the above selection changed event is also raising when i am selecting the item from the second combobox column..but i dont want to raise the selection changed event for the second combos column. It should raise only for the first combobox only.
Please help as l'm stuck up with this problem.

The problem seems that you're adding the event handler to any combo box, doesn't matter what column it is, so you must find first in what column the event was triggered, for this you must take a look at the sender object of the Grid_EditingControlShowing event handler (which is a DataGridView) and its CurrentCell, SelectedColumns or SelectedCells properties.
Example:
private void Grid_EditingControlShowing(object sender, DataGridViewEditingControlShowingEventArgs e)
{
if(((DataGridView)sender).CurrentCell.ColumnIndex == 0) //Assuming 0 is the index of the ComboBox Column you want to show
{
ComboBox cb = e.Control as ComboBox;
if (cb!=null)
{
cb.SelectionChangeCommitted -= new EventHandler(cb_SelectedIndexChanged);
// now attach the event handler
cb.SelectionChangeCommitted += new EventHandler(cb_SelectedIndexChanged);
}
}
}
An example using SelectedColumns or SelectedCells, will be pretty much like this, if you want more info about that properties you can take a look at their documentation on MSDN

Related

How to set up the event Datagridviewcombobox cell selectionchanged?

I have a DataGridViewComboBoxCell control with some items into it. I would like to get the values when user chooses a value from the dropdown. I cant use DataGridViewComboBoxColumn where EditingControlShowing can be used. I need similar event handler for DataGridViewComboBoxCell. Can anyone help pls.
Please find code sample below:
private DataGridViewComboBoxCell NameDropDown = new DataGridViewComboBoxCell();
public void SetDropDown(int index)
{
NameDropDown = new DataGridViewComboBoxCell();
DropDownValues(index);
for (int j = 0; j < DropDownOld.Items.Count; j++)
{
NameDropDown.Items.Add(DropDownOld.Items[j]);
}
dataGridView1.Rows[index].Cells[4] = NameDropDown;
}
Yes, you can use the EditingControlShowing event to get a handle to the combobox.
Then hook up an event handler for the SelectedIndexChanged or whatever event you want and code it..!
DataGridViewComboBoxEditingControl cbec = null;
private void dataGridView1_EditingControlShowing(object sender,
DataGridViewEditingControlShowingEventArgs e)
{
if (e.Control is DataGridViewComboBoxEditingControl)
{
cbec = e.Control as DataGridViewComboBoxEditingControl;
cbec.SelectedIndexChanged -=Cbec_SelectedIndexChanged;
cbec.SelectedIndexChanged +=Cbec_SelectedIndexChanged;
}
}
private void Cbec_SelectedIndexChanged(object sender, EventArgs e)
{
if (cbec != null) Console.WriteLine(cbec.SelectedItem.ToString());
}

Get text from combobox in selected tab

In all of these tabs, I have a combobox with different functions as strings. I want the text under Preview (it's a richtextbox with "Nothing is selected." as a default string) to change whenever I select an item in each of the different tabs' comboboxes. Any idea how I can do that?
You could set every TextChanged event of all of your combobox to the same event handler
comboBox1.TextChanged += CommonComboTextChanged;
comboBox2.TextChanged += CommonComboTextChanged;
comboBox3.TextChanged += CommonComboTextChanged;
comboBox4.TextChanged += CommonComboTextChanged;
private void CommonComboTextChanged(object sender, EventArgs e)
{
ComboBox cbo = sender as ComboBox;
richTextBox.Text = cbo.Text;
}
However, if you change the DropDownStyle of your combos to ComboBoxStyle.DropDownList then you could use the SelectedIndexChanged event that will be triggered only when your user changes the item selected by using the DropDown List.
comboBox1.SelectedIndexChanged += CommonComboIndexChanged;
comboBox2.SelectedIndexChanged += CommonComboIndexChanged;;
comboBox3.SelectedIndexChanged += CommonComboIndexChanged;;
comboBox4.SelectedIndexChanged += CommonComboIndexChanged;;
private void CommonComboIndexChanged;(object sender, EventArgs e)
{
ComboBox cbo = sender as ComboBox;
richTextBox.Text = cbo.Text;
}
Finally to set the content of the RTB to the one of the combo in the current tab page you need to handle the TabChanged event of your tabControl
private void tabControl1_Selected(object sender, TabControlEventArgs e)
{
switch(e.TabPageIndex)
{
case 0:
richTextBox.Text = comboBox1.Text;
break;
// so on for the other page and combos
}
}
Or if your comboboxes share a common initial part of their names
private void tabControl1_Selected(object sender, TabControlEventArgs e)
{
var result = e.TabPage.Controls.OfType<ComboBox>()
.Where(x => x.Name.StartsWith("cboFunction"));
if(result != null)
{
ComboBox b = result.ToList().First();
richTextBox.Text = comboBox1.Text;
}
}

Getting the index of a ComboBox in in a DataGridView

I have a System.Windows.Forms.DataGridView in which one column has only ComboBoxes. When something changes in a ComboBox, I need to know what the new item is, and the row index of the ComboBox in which the event took place. The latter is giving me trouble. I have the following:
class MyForm : Form
{
private System.Windows.Forms.DataGridView m_GridView;
private System.Windows.Forms.DataGridViewComboBoxColumn m_ComboBoxColumn;
public MyForm ()
{
/* ... lots of initialisation stuf...*/
this.m_GridView.Columns.AddRange(new System.Windows.Forms.DataGridViewColumn[] {
this.m_ComboBoxColumn});
}
private void m_GridView_EditingControlShowing(object sender, DataGridViewEditingControlShowingEventArgs e)
{
ComboBox comboBox = e.Control as ComboBox;
if (comboBox != null)
{
comboBox.SelectedIndexChanged -= new EventHandler(m_ComboBoxColumn_SelectedIndexChanged);
comboBox.SelectedIndexChanged += new EventHandler(m_ComboBoxColumn_SelectedIndexChanged);
}
}
private void m_ComboBoxColumn_SelectedIndexChanged(object sender, EventArgs e)
{
ComboBox comboBox = (ComboBox) sender;
string item = comboBox.Text;
if (item != null)
{
System.Diagnostics.Debug.WriteLine(item); // This is the new item text
// Now I need the row index of this ComboBox in 'm_GridView' (or 'm_ComboBoxColumn')
}
}
}
How can I find the row index of the ComboBox in the DataGridViewComboBoxColumn in this last method?
You should cast it to DataGridViewComboBoxEditingControl and access the EditingControlRowIndex to get the row index like this:
var comboBox = (DataGridViewComboBoxEditingControl)sender;
int rowIndex = comboBox.EditingControlRowIndex;
You can get it via the ComboBox's SelectedIndex property - http://msdn.microsoft.com/en-us/library/system.windows.forms.combobox.selectedindex.aspx
var index = comboBox.SelectedIndex;

Get name of control when mouse hovers over it C#

I am adding ComboBoxes dynamically at runtime as shown below.
The problem that I am having is that i do not know which of the comboboxes the user is using.
For eg. The user decides to add 5 comboBoxes to the form, and then goes to the first comboBox,and selects a value, I need to retrieve the value of that comboBox.
What the below code is doing - My approach
I am adding a comboBox to a FlowlayoutPanel and the retrieve its name based on the mouse co-ordinates.... this by the way is not working... and I have no idea what to do.
Any help is greatly appreciated.
public partial class Form1 : Form
{
int count = 0;
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
count += 1;
ComboBox cb = new ComboBox();
cb.Name = count.ToString();
cb.MouseHover += new EventHandler(doStuff);
Label lb = new Label();
lb.Text = count.ToString();
flowLayoutPanel1.Controls.Add(cb);
flowLayoutPanel1.Controls.Add(lb);
}
public void doStuff(object sender, EventArgs e)
{
label1.Text = flowLayoutPanel1.GetChildAtPoint(Cursor.Position).Name;
}
}
}
You could try:
cb.SelectionChangeCommitted += selectionChangedHandler
...
void selectionChangedHandler(object sender, EventArgs e) {
ComboBox cb = (ComboBox)sender;
label1.Text = cb.Name;
// Do whatever else is needed with the combo box
}
The SelectionChangeCommitted event is "raised only when the user changes the combo box selection", which sounds like what you're after.
The combobox that raised the event in your doStuff-eventhandler is in the sender-parameter. Try casting it to a checkbox likte this:
ComboBox boxThatRaisedTheEvent = (ComboBox)sender;
string text = ((ComboBox)this.GetChildAtPoint(pt)).Text;
public void DoStuff(object sender, EventArgs e)
{
var comboBox = sender as ComboBox;
var name = (comboBox != null ? comboBox.Name : null);
}
this code casts the 'sender' parameter to a ComboBox object and if the cast is done correctly assings the ComboBox name to the string 'name', otherwise 'name' is null.
Tip: The C# coding style suggests that method names should start with capitalized letter.
You can try something like:
flowLayoutPanel1.Controls.OfType<ComboBox>().FirstOrDefault(cb => cb.Name.Equals(NAME_OF_COMBOBOX))
Or better:
ComboBox box = (ComboBox)sender;

Datagridview ComboBoxCell set default value?

I have a datagridview with lots of data, and when I add a new line, the first column's last row creates a new ComboBoxCell which contain four items. But I can't set the default value ("DropDown") for the combobox. Every time I must manually select "DropDown". What is the solution?
DataGridViewComboBoxCell dgvCell = new DataGridViewComboBoxCell();
dgv[1, dgv.Rows.Count - 1] = dgvCell;
string[] controltype = {"DropDown", "CheckBoxList", "ListControl", "Tree" };
dgvCell.DataSource = controltype;
private void dataGridView_DefaultValuesNeeded(object sender, DataGridViewRowEventArgs e)
{
e.Row.Cells[4].Value = "DropDown";
}
it,s easy,If you have a ComboBox Column in your DataGrid View and you want to know what is the selected index of the combo box, then you need to do this:
1. Handle the EditingControlShowing event of DataGrid View. In this event handler, check if the current column is of our interest. Then we create a temporary ComboBox object and get the selected index:
Code
private void dataGridView1_EditingControlShowing(object sender,
DataGridViewEditingControlShowingEventArgs e)
{
if (dataGridView1.CurrentCell.ColumnIndex == 0)
{
// Check box column
ComboBox comboBox = e.Control as ComboBox;
comboBox.SelectedIndexChanged += new EventHandler(comboBox_SelectedIndexChanged);
}
}
void comboBox_SelectedIndexChanged(object sender, EventArgs e)
{
int selectedIndex = ((ComboBox)sender).SelectedIndex;
MessageBox.Show("Selected Index = " + selectedIndex);
}
try :
if(!isPostBack)
{
dgvCell.SelectedItem=controltype[0].toString();
}

Categories