Click here to Skip to main content
15,885,546 members
Please Sign up or sign in to vote.
4.00/5 (1 vote)
See more:
Hi friend Please help
i have one problem with my datagridviewcomboboxCell .

My Grid have one datagridviewcomboboxCell that is bound to datatable on GridCell_Click event .
i add one DataGrViewEditingControlShowing event for Combo box Selected index Change event.
on index change i am adding few columns next to datagridviewcomboboxCell . every thing is working
properly except one prob that at first selection datagridviewcomboboxCell value is going to blank.
but dynamic columns add properly based on id from combo box selected index .

but why on first selection value is going to blank its not clear to me.
code is here :

C#
 private void grdBO_CellClick(object sender, DataGridViewCellEventArgs e)
  {
             DataGridViewComboBoxCell boSizeRangeCell = new DataGridViewComboBoxCell();
                boSizeRangeCell.DataSource = fillGridSizeMasterCol();// DataTable
                boSizeRangeCell.DisplayMember = "SizeType";
                boSizeRangeCell.ValueMember = "SizeKey";
                boSizeRangeCell.DisplayStyle = DataGridViewComboBoxDisplayStyle.Nothing;
                grdBO.CurrentRow.Cells[ModConstant.cBOSizeRange] = boSizeRangeCell;


  }

       private void grdBO_EditingControlShowing(object sender, DataGridViewEditingControlShowingEventArgs e)
        {
            if (e.Control is DataGridViewComboBoxEditingControl)
            {
                ComboBox combo = e.Control as ComboBox;
                if (combo != null)
                {                   
                    combo.SelectedIndexChanged -= new EventHandler(ComboBox_SelectedIndexChanged);                
                    combo.SelectedIndexChanged += new EventHandler(ComboBox_SelectedIndexChanged);
                }
            }

private void ComboBox_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (this.grdBO.CurrentCell.ColumnIndex == this.grdBO.Columns[ModConstant.cBOSizeRange].Index)
            {
                if (((ComboBox)sender).SelectedIndex > -1)//||(((ComboBox)sender).Items.Count > 0 )) 
                {
                    string sizekey = ((ComboBox)sender).SelectedValue.ToString();
                    if (lSizeRangeAdd.Count > 0)
                    {
                        lSizeRangeAdd.Clear();
                    }
                    lSizeRangeAdd = SetSizeGridHeadersDB(sizekey);  // list fill by datatable return by method SetSizeGridHeadersDB ;
                    lSizeRangeRemove = SetSizeGridHeadersDB(sizekey);  // list for removing prev //created col
                    if (lSizeRangeAdd.Count > 0)
                    {
                        foreach (string size in lSizeRangeAdd)
                        {
                            DataGridViewTextBoxColumn col = new DataGridViewTextBoxColumn();
                            col.HeaderText = size;
                            col.Name = size;
                            grdBO.Columns.Insert(grdBO.Columns[ModConstant.cBOSizeRange].Index + 1, col);
                            grdBO.Columns[grdBO.Columns[ModConstant.cBOSizeRange].Index + 1].DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleRight;
                        }
                    }
                }
            }
        }


// this method is for getting col header at run time .
     private List<string> SetSizeGridHeadersDB(String strSizeKey)
        {
            strSql = "select * from Sizemaster where sizekey = '" + strSizeKey + "'";
            DataSet ds = new DataSet();
            List<string> lSizeRange = new List<string>();
             Service1 objS = new WebService1.Service1();
             try
             {
                 ds = objS.GetDataSet(strSql, Text);
                 if (ds.Tables[0].Rows.Count > 0)
                 {
                     for (int i = 1 ; i < ds.Tables[0].Columns.Count - 4; i++)
                     {
                         string SizeRange = ds.Tables[0].Rows[0]["size" + i.ToString()].ToString();
                         if (SizeRange != "")
                         {   
                            
                             lSizeRange.Add(SizeRange);
                         }
                     }

                 }   
             }
             catch (Exception ex)
             {
             }
             finally
             {
                 objS = null;
                 ds = null;
             }
             return lSizeRange;
        }
Posted
Updated 18-Feb-13 2:50am
v2

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