Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: .NET3.0 C#4.0
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 :
 
 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 18-Feb-13 3:23am
Edited 18-Feb-13 3:50am
(no name)498.6K
v2

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

  Print Answers RSS
0 OriginalGriff 386
1 Marcin Kozub 225
2 Sergey Alexandrovich Kryukov 215
3 /\jmot 189
4 Praneet Nadkar 173
0 OriginalGriff 8,289
1 Sergey Alexandrovich Kryukov 7,407
2 DamithSL 5,624
3 Maciej Los 4,989
4 Manas Bhardwaj 4,986


Advertise | Privacy | Mobile
Web03 | 2.8.1411023.1 | Last Updated 18 Feb 2013
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100