Thread: SelectedValueChangedEvents firing at the wrong time.

  1. #1
    Registered User
    Join Date
    Jan 2011
    Posts
    41

    SelectedValueChangedEvents firing at the wrong time.

    Hi ,

    I am working on a program that uses a DataGridView with 12 columns. 2 of these columns are DataGridViewComboBoxColumns and the other 10 DataGridViewComboBoxColumns. When you change the value in either of the ComboBoxColumns the values in the rest of the columns for that row are supposed to update themselves. I have created two separate events to handle the SelectedValueChanged event for the two ComboBoxColumns and somehow they are overlapping/firing at the wrong time.


    Here is the event that adds the event depending on which column gets clicked:
    Code:
    private void DataEditingControlShowingEvent(object sender, DataGridViewEditingControlShowingEventArgs e)
            {
                DataGridView grid = sender as DataGridView;
                if (grid != null)
                {
                    DataGridViewComboBoxEditingControl cbx = e.Control as DataGridViewComboBoxEditingControl;
                    if (cbx != null)
                    {
                        if (grid.CurrentCell.ColumnIndex == grid.Columns["Pattern"].Index)
                        {
                            cbx.SelectedValueChanged -= this.PatternBoxSelectedValueChangedEvent;
                            cbx.SelectedValueChanged += this.PatternBoxSelectedValueChangedEvent;
                        }
                        if (grid.CurrentCell.ColumnIndex == grid.Columns["ID"].Index)
                        {
                            cbx.SelectedValueChanged -= this.IdBoxSelectedValueChangedEvent;
                            cbx.SelectedValueChanged += this.IdBoxSelectedValueChangedEvent;
                        }
                    }
                }
            }
    Here is the event for if someone clicks the ID column:
    Code:
    private void IdBoxSelectedValueChangedEvent(object sender, EventArgs e)
            {
                // this event causes dynamic change for the calculated values when the ID
                // drop down changes selection.
                ComboBox cbx = sender as ComboBox;
                if (cbx != null && cbx.SelectedIndex > -1)
                {
                    int ballqty;
                    int id = (int)cbx.SelectedValue;
                    PatternXML pat = GetPatternById(id);
                    if ((int.TryParse(datagrid.CurrentRow.Cells["BallQty"].Value.ToString(), out ballqty)) &&
                        pat != null && datagrid.CurrentRow.Cells["Pattern"].Value != pat.Value)
                    {
                        //if the pattern doesnt match already force it to
                        datagrid.CurrentRow.Cells["Pattern"].Value = pat.Value;
                        datagrid.CurrentRow.Cells["Frequency"].Value = PaytableCalculator.CalcFrequency(pat, ballqty);
                    }
                }
               // control.SelectedValueChanged -= IdBoxSelectedValueChangedEvent;
            }
    Here is the event that is supposed to fire if the Pattern column is selected:
    Code:
    private void PatternBoxSelectedValueChangedEvent(object sender, EventArgs e)
            {
                // this event causes dynamic change for the calculated values when the Pattern
                // drop down changes selection.
                ComboBox cbx = sender as ComboBox;
                if ((cbx != null) && (cbx.SelectedIndex > -1))
                {
                    int ballqty;
                    PatternXML pat = cbx.SelectedItem as PatternXML;
                    if ((int.TryParse(datagrid.CurrentRow.Cells["BallQty"].Value.ToString(), out ballqty)) &&
                        pat != null && datagrid.CurrentRow.Cells["ID"].Value.ToString() != pat.ID.ToString())
                    {
                        datagrid.CurrentRow.Cells["ID"].Value = pat.ID;
                        datagrid.CurrentRow.Cells["Frequency"].Value = PaytableCalculator.CalcFrequency(pat, ballqty);
                    }
                }
               // control.SelectedValueChanged -= PatternBoxSelectedValueChangedEvent;
            }
    Any ideas you guys could give would be awesome.

    PS - also posted at http://stackoverflow.com/questions/6...ew-event-issue

    Thanks.
    Hunter
    Last edited by mcmillhj; 05-27-2011 at 07:54 AM. Reason: adding crosspost information

  2. #2
    Registered User
    Join Date
    Jan 2011
    Posts
    41
    I resolved this issue by implementing the CellClickEvent which fires everytime a cell is clicked versus the ControlShowingEvent which fires when a NEW cell is clicked.

    Hunter

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. [Help]always wrong at first time
    By kingofdcp in forum C Programming
    Replies: 10
    Last Post: 06-06-2009, 07:57 PM
  2. Firing bullets from rotating ship???
    By blackCat in forum Game Programming
    Replies: 9
    Last Post: 04-12-2009, 12:11 PM
  3. Firing problem in space sim
    By VirtualAce in forum Game Programming
    Replies: 7
    Last Post: 03-29-2005, 12:14 PM
  4. Constructor with Parameter not Firing
    By BillBoeBaggins in forum Windows Programming
    Replies: 4
    Last Post: 08-26-2004, 02:17 PM
  5. Stop Bullet Firing...
    By BillBoeBaggins in forum Game Programming
    Replies: 14
    Last Post: 11-25-2003, 10:48 AM