Thread: DataGridViewComboBoxColumn issue/Question

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

    DataGridViewComboBoxColumn issue/Question

    Hey everyone,
    I have two ComboBoxColumns that are beside each other in my DataGridView one only holds an Integer then another holds an object that I have defined. I created separate events for when they individually get clicked/changed but for some reason the wrong event is firing when I click on one of the comboboxcolumns. Example: I click on the PatternComboBox and the IdComboBox Event fires.

    Any ideas would be great,
    Thanks. Hunter.

  2. #2
    Gawking at stupidity
    Join Date
    Jul 2004
    Location
    Oregon, USA
    Posts
    3,218
    It's going to be hard to find out why without seeing some code...
    If you understand what you're doing, you're not learning anything.

  3. #3
    Registered User
    Join Date
    Jan 2011
    Posts
    41
    Ill post some, but it is a rather large project.

    I Use this method the remove the old event and add the new event for my ComboBoxcolumns, for some reason the portion of code in the comments causes the program to crash.
    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;
                        }//new added this portion for editable ID tags
    
                        /*if (grid.CurrentCell.ColumnIndex == grid.Columns["ID"].Index)
                        {
                            cbx.SelectedValueChanged -= this.IdBoxSelectedValueChangedEvent;
                            cbx.SelectedValueChanged += this.IdBoxSelectedValueChangedEvent;
                        }*/
                    }
                }
            }
    this is the event for when the pattern gets changed, this works fine
    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;
                    //MessageBox.Show("pattern item: " + pat.ToString());
                    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);
                    }
                }
            }
    Here is the code for the changing of the Id box.
    Code:
    private void IdBoxSelectedValueChangedEvent(object sender, EventArgs e)
            {
                ComboBox cbx = sender as ComboBox;
                int ballqty;
    
                if ((cbx != null) && (cbx.SelectedIndex > -1))
                {
                   // MessageBox.Show("id item: " + cbx.SelectedValue);
    
                    //grab the id from the combobox
                    int id = (int)(cbx.SelectedItem);
                    //get the pattern object
                    PatternXML pat = GetPatternById(id);
    
                    //copy the pattern into the datagrid cell if it isnt already there
                    if (((int.TryParse(datagrid.CurrentRow.Cells["BallQty"].Value.ToString(), out ballqty)) &&
                        (pat != null) && (datagrid.CurrentRow.Cells["Pattern"].Value != pat.Value)))
                    {
                        datagrid.CurrentRow.Cells["Pattern"].Value   = pat.Value;
                        datagrid.CurrentRow.Cells["Frequency"].Value = PaytableCalculator.CalcFrequency(pat, ballqty);
                    }
                }
            }
    And attached is what the screen looks like.

    any help would be awesome.
    Hunter
    Attached Images Attached Images DataGridViewComboBoxColumn issue/Question-screen-jpg 

  4. #4
    Registered User
    Join Date
    Jan 2011
    Posts
    41
    Solved, the issue was which overlapping event handlers

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. bandwidth issue / network issue with wireless device communication
    By vlrk in forum Networking/Device Communication
    Replies: 0
    Last Post: 07-05-2010, 11:52 PM
  2. new issue
    By hiddenprophecy in forum C Programming
    Replies: 3
    Last Post: 04-15-2009, 05:59 PM
  3. DNS issue
    By George2 in forum C# Programming
    Replies: 1
    Last Post: 08-02-2008, 10:15 AM
  4. asm issue
    By DRDOS in forum Tech Board
    Replies: 8
    Last Post: 07-04-2006, 11:15 AM
  5. dll issue
    By yahn in forum C++ Programming
    Replies: 1
    Last Post: 01-20-2006, 10:40 PM