Click here to Skip to main content
15,884,099 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
I Have a datagridview with two datagridviewcomboboxcell Both of them are databound using linq to sql.. Both have a valuefield which is integer and displaymember as string

I had created some events for the comboboxes in the datagridview cell click event for the first combo box and every thing is fine and both comboboxs are loading and I can select the items in the first combobox also but when I select a value from combobox2 I am getting a error.means error occur at the value change or selected index change of that combobox

I captured the exception and find that the exception is "Datagridviewcomboboxcell value is not valid" and the context is "Formating and display" and after the error dialog the first combo is also getting reset to the selected value of first combo

Can anyone suggest whats happening The column index of first combobox is 10 and the second is 16
My code like like below
C#
public void loadcategory()
     {
         CourierDataDataContext courdatacontext = new CourierDataDataContext(Program.ConnStr);
         var q = from category in courdatacontext.Category_Masters
                 select category;
         cmbcomplex.DataSource = q;
         cmbcomplex.DisplayMember = "CategoryName";
         cmbcomplex.ValueMember = "CategoryIID";

     }

     public void LoadAtcnum()
     {
         CourierDataDataContext couriercontext = new CourierDataDataContext(Program.ConnStr);

         var q = from cust in couriercontext.Atc_masters
                 select cust;

         AtcNO.DataSource = q;
         AtcNO.DisplayMember = "AtcNum";
         AtcNO.ValueMember = "Atc_id";



     }
     private void tbl_bookdata_EditingControlShowing(object sender, DataGridViewEditingControlShowingEventArgs e)
     {
         try
         {
             if (tbl_bookdata.CurrentCell.ColumnIndex == 10 && e.Control is ComboBox)
             {
                 ComboBox comboBox = e.Control as ComboBox;

                 comboBox.SelectionChangeCommitted -= new EventHandler(cmb_SelectionChangeCommitted);
                 comboBox.SelectionChangeCommitted += new EventHandler(cmb_SelectionChangeCommitted);
                 comboBox.SelectedIndexChanged += LastColumnComboSelectionChanged;

             }
         }
         catch (Exception)
         {

             MessageBox.Show("bl_bookdata_EditingControlShowingd");
         }
     }
     void cmb_SelectionChangeCommitted(object sender, EventArgs e)
     {
         try
         {
             tbl_bookdata.Rows[tbl_bookdata.CurrentRow.Index].Cells[10].Value = ((DataGridViewComboBoxEditingControl)sender).EditingControlFormattedValue;
         }
         catch (Exception)
         {

             MessageBox.Show("Error in cmb_SelectionChangeCommitted");
         }
     }
     private void LastColumnComboSelectionChanged(object sender, EventArgs e)
     {
         //  ((ComboBox)sender)

         if (tbl_bookdata.CurrentCell.ColumnIndex != 16)
         {
             var currentcell = tbl_bookdata.CurrentCellAddress;
             var sendingCB = sender as DataGridViewComboBoxEditingControl;
             DataGridViewTextBoxCell cel = (DataGridViewTextBoxCell)tbl_bookdata.Rows[currentcell.Y].Cells[11];
             DataGridViewTextBoxCell cel1 = (DataGridViewTextBoxCell)tbl_bookdata.Rows[currentcell.Y].Cells[18];
             int rownumber = tbl_bookdata.CurrentRow.Index;
             try
             {

                 int factid = int.Parse(tbl_bookdata.Rows[rownumber].Cells[1].Value.ToString());
                 int catgid = 0;
                 try
                 {
                     catgid = int.Parse(tbl_bookdata.Rows[rownumber].Cells[10].Value.ToString());
                 }
                 catch (Exception)
                 {

                     string catgid12 = ((ComboBox)sender).SelectedValue.ToString();

                     catgid = int.Parse(catgid12);
                 }
                 int orderqty = int.Parse(tbl_bookdata.Rows[rownumber].Cells[7].Value.ToString());
                 int consumptionvalue = qtycal.calculatedata(catgid, factid, orderqty);
                 cel.Value = consumptionvalue.ToString();
                 cel1.Value = consumptionvalue.ToString();
             }
             catch (Exception)
             {
                 cel.Value = 0;
                 MessageBox.Show("LastColumnComboSelectionChanged");
             }
         }
     }
Posted
Updated 5-May-15 6:38am
v2
Comments
Sergey Alexandrovich Kryukov 5-May-15 12:55pm    
In what line?
—SA
SREENATH GANGA 5-May-15 12:58pm    
its a format and display exception..its not getting stuck at any custom code
Sergey Alexandrovich Kryukov 5-May-15 13:01pm    
In what line?
—SA
Maciej Los 7-May-15 6:37am    
5^2!
Maciej Los 7-May-15 6:37am    
5!

1 solution

 
Share this answer
 
Comments
Maciej Los 7-May-15 6:41am    
+5
Sergey Alexandrovich Kryukov 7-May-15 9:01am    
Thank you, Maciej.
—SA
SREENATH GANGA 10-May-15 4:16am    
sample code gives the solution thanks
Sergey Alexandrovich Kryukov 10-May-15 10:42am    
Sure. Good luck, call again.
—SA

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900