CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 2 of 2
  1. #1
    Join Date
    Oct 2004
    Posts
    97

    DataGridView Change DataGridViewTextBoxCell to DataGridViewComboBoxCell

    Hi,

    I'm having a problem when trying to convert an DataGridViewCell at runtime. from what i can see this is a knowen issue but i cant get the workaround to work for me. this is what i have


    Code:
    public Form1()
        {
          InitializeComponent();
    
          Main.Columns.Add("Period", typeof(int));
          Main.Columns.Add("Type", typeof(int));
    
          Main.Rows.Add(1, 1);
          Main.Rows.Add(1, 2);
          Main.Rows.Add(1, 3);
          Main.Rows.Add(2, 1);
          Main.Rows.Add(2, 2);
          Main.Rows.Add(2, 3);
    
          Periods.Columns.Add("Code", typeof(int));
          Periods.Columns.Add("Desc", typeof(String));
    
          Periods.Rows.Add(1, "Jan");
          Periods.Rows.Add(2, "Feb");
    
          Types.Columns.Add("Code", typeof(int));
          Types.Columns.Add("Desc", typeof(String));
    
          Types.Rows.Add(1, "Int");
          Types.Rows.Add(2, "String");
          Types.Rows.Add(3, "Float");
    
          dgv.DataSource = Main;
    
          dgv.DataBindingComplete += new DataGridViewBindingCompleteEventHandler(dgv_DataBindingComplete);
    
        }
    
        void dgv_DataBindingComplete(object sender, DataGridViewBindingCompleteEventArgs e)
        {
          dgv.DataBindingComplete -= new DataGridViewBindingCompleteEventHandler(dgv_DataBindingComplete);
          dgv.RowEnter += new DataGridViewCellEventHandler(dgv_RowEnter);
          dgv.RowLeave += new DataGridViewCellEventHandler(dgv_RowLeave);
        }
    
        void dgv_RowLeave(object sender, DataGridViewCellEventArgs e)
        {
          foreach (DataGridViewColumn c in this.dgv.Columns)
          {
            this.dgv[c.Index, e.RowIndex] = new DataGridViewTextBoxCell();
          }
        }
    
    void dgv_RowEnter(object sender, DataGridViewCellEventArgs e)
        {
          foreach (DataGridViewColumn c in this.dgv.Columns)
          {
            this.dgv[c.Index, e.RowIndex] = new DataGridViewComboBoxCell();
    
    
            switch (c.Index)
            {
              case 0:
                {
                  ((DataGridViewComboBoxCell)this.dgv[c.Index, e.RowIndex]).DataSource = Periods;
                  ((DataGridViewComboBoxCell)this.dgv[c.Index, e.RowIndex]).ValueMember = "Code";
                  ((DataGridViewComboBoxCell)this.dgv[c.Index, e.RowIndex]).DisplayMember = "Desc";
                  break;
                }
              case 1:
                {
                  ((DataGridViewComboBoxCell)this.dgv[c.Index, e.RowIndex]).DataSource = Types;
                  ((DataGridViewComboBoxCell)this.dgv[c.Index, e.RowIndex]).ValueMember = "Code";
                  ((DataGridViewComboBoxCell)this.dgv[c.Index, e.RowIndex]).DisplayMember = "Desc";
                  break;
                }
            }
    
          }
        }
    Ok so on Row leave i want to set everyting back to textboxcells

    on Row Enter i want to set the current row to ComboBoxCells

    this works ok unless i select Cell (0,0) / (1,1) then select another cell i get this error on the RowLeave "Operation is not valid because it results in a reentrant call to the SetCurrentCellAddressCore function"


    Can anyone help please
    Last edited by Theone2k; March 17th, 2011 at 03:56 AM.

  2. #2
    Join Date
    Jul 2004
    Posts
    3

    Re: DataGridView Change DataGridViewTextBoxCell to DataGridViewComboBoxCell

    Hi. I used CellClick and CellValidating and I had to solve the "reentrant call" problem - finally I find the solution on the net.
    I explained it here:
    Dynamically change DataGridViewTextBoxCell to DataGridViewComboBoxCell

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured