none
Operation is not valid because it results in a reentrant call to the SetCurrentCellAddressCore function.

    Question

  • I have a datagridview where i donot have any datasource linked. The fields are dependent on each other (that means values populated on one field depends on what user has selected on previous field). On one field the type of cell also changes on what user has selected previously. There are 4 fields in the grid view

    The code runs fine till the 4th row 3rd column. Once i sleect the cell (row 4, column 4) it throws me error :

    Operation is not valid because it results in a reentrant call to the SetCurrentCellAddressCore function.
    
    Stack Trace:
    
      at System.Windows.Forms.DataGridView.SetCurrentCellAddressCore(Int32 columnIndex, Int32 rowIndex, Boolean setAnchorCellAddress, Boolean validateCurrentCell, Boolean throughMouseClick)
      at System.Windows.Forms.DataGridView.ResetCurrentCell()
      at System.Windows.Forms.DataGridView.OnReplacingCell(DataGridViewRow dataGridViewRow, Int32 columnIndex)
      at 
    I tried the solution given at teh below link but somehow i could not work that out.

    http://social.msdn.microsoft.com/Forums/en-US/winforms/thread/6f6ff25d-bdef-496e-9931-2abd03252ae8

    I am giving the code below for the application.

    Form Code :

    namespace DataGridIssue
    {
      internal enum ValueCellType
      {
        Text,
        Combo
      }
      public partial class Form1 : Form
      {
        private bool isClauseValueSet = false;
        private bool isOperatorValueSet = false;
        private bool isFieldValueSet = false;
        private bool flag = false;
    
        public Form1()
        {
          InitializeComponent();
        }
    
        private void dataGridViewQuery_UserAddedRow(object sender, DataGridViewRowEventArgs e)
        {
          this.isClauseValueSet = false;
          this.isOperatorValueSet = false;
          this.isFieldValueSet = false;
        }
    
        private void dataGridViewQuery_CellEnter(object sender, DataGridViewCellEventArgs e)
        {
          if (e.RowIndex != 3)
          {
            flag = false;
          }
    
          DataGridViewComboBoxCell cell = null;
          ValueCellType cellType;
          string fieldColumnValue = string.Empty;
    
          if (e.ColumnIndex == 0 && e.RowIndex != 0 && !isClauseValueSet)
          {
            cell = (DataGridViewComboBoxCell)dataGridViewQuery.Rows[e.RowIndex].Cells[e.ColumnIndex];
            cell.Items.Add("And");
            cell.Items.Add("Or");
            this.isClauseValueSet = true;
          }
    
          if (e.ColumnIndex == 1 && e.RowIndex != 0)
          {
            cell = (DataGridViewComboBoxCell)dataGridViewQuery.Rows[e.RowIndex].Cells[0];
            if (string.IsNullOrEmpty(Convert.ToString(cell.Value)))
            {
              MessageBox.Show("Please select a value in the clause column");
            }
          }
    
          if (e.ColumnIndex == 2 && !isOperatorValueSet)
          {
            cell = (DataGridViewComboBoxCell)dataGridViewQuery.Rows[e.RowIndex].Cells[1];
            if (string.IsNullOrEmpty(Convert.ToString(cell.Value)))
            {
              MessageBox.Show("Please select a Value in the field column");
            }
            else
            {
              fieldColumnValue = Convert.ToString(cell.Value);
              cell = (DataGridViewComboBoxCell)dataGridViewQuery.Rows[e.RowIndex].Cells[e.ColumnIndex];
              this.setValuesForOperator(fieldColumnValue, cell);
            }
          }
    
          if (e.ColumnIndex == 3 && !isFieldValueSet)
          {
    
            cell = (DataGridViewComboBoxCell)dataGridViewQuery.Rows[e.RowIndex].Cells[1];
            if (string.IsNullOrEmpty(Convert.ToString(cell.Value)))
            {
              MessageBox.Show("Please select a Value in the field column");
            }
            cell = (DataGridViewComboBoxCell)dataGridViewQuery.Rows[e.RowIndex].Cells[2];
            if (string.IsNullOrEmpty(Convert.ToString(cell.Value)))
            {
              MessageBox.Show("Please select a Value in the operator column");
            }
            else if (!flag)
            {
              cell = (DataGridViewComboBoxCell)dataGridViewQuery.Rows[e.RowIndex].Cells[1];
              fieldColumnValue = Convert.ToString(cell.Value);
              cellType = this.GetCellType(fieldColumnValue);
    
              if (cellType == ValueCellType.Text)
              {
                dataGridViewQuery.Rows[e.RowIndex].Cells[e.ColumnIndex] = new DataGridViewTextBoxCell();
              }
              else if (cellType == ValueCellType.Combo)
              {
                cell = new DataGridViewComboBoxCell();
                cell.DisplayStyle = DataGridViewComboBoxDisplayStyle.ComboBox;
                this.setValuesForValues(fieldColumnValue, cell);
                dataGridViewQuery.Rows[e.RowIndex].Cells[e.ColumnIndex] = cell;
              }
    
              if (e.RowIndex == 3)
              {
                flag = true;
              }
            }
          }
        }
    
        private void setValuesForValues(string fieldColumnValue, DataGridViewComboBoxCell cell)
        {
          if (cell.Items.Count > 0)
          {
            while (cell.Items.Count != 0)
            {
              cell.Items.RemoveAt(0);
            }
          }
          switch (fieldColumnValue)
          {
            case "Priority":
              {
                cell.Items.Add("1");
                cell.Items.Add("2");
                cell.Items.Add("3");
                break;
              }
    
            case "Severity":
              {
                cell.Items.Add("1");
                cell.Items.Add("2");
                cell.Items.Add("3");
                break;
              }
    
            case "Category" :
              {
                cell.Items.Add("Cat 1");
                cell.Items.Add("Cat 2");
                cell.Items.Add("Cat 3");
                break;
              }
    
            default:
              break;
          }
        }
    
        private ValueCellType GetCellType(string fieldColumnValue)
        {
          ValueCellType cellType = ValueCellType.Text;
          switch (fieldColumnValue)
          {
            case "Priority":
            case "Severity":
            case "Category":
              {
                cellType = ValueCellType.Combo;
                break;
              }
    
            case "ID":
            case "Description":
              {
                cellType = ValueCellType.Text;
                break;
              }
            default:
              break;
          }
    
          return cellType;
        }
    
        private void setValuesForOperator(string fieldColumnValue, DataGridViewComboBoxCell cell)
        {
          if (cell.Items.Count > 0)
          {
            while (cell.Items.Count != 0)
            {
              cell.Items.RemoveAt(0);
            }
          }
    
          switch (fieldColumnValue)
          {
            case "Priority":
            case "Severity":
            case "Category":
              {
                cell.Items.Add("Equals");
                break;
              }
    
            case "ID":
            case "Description":
              {
                cell.Items.Add("Equals");
                cell.Items.Add("Contains");
                break;
              }
    
            default:
              break;
          }
        }
      }
    }
    
    

    Form Designer Code :

     partial class Form1
      {
        /// <summary>
        /// Required designer variable.
        /// </summary>
        private System.ComponentModel.IContainer components = null;
    
        /// <summary>
        /// Clean up any resources being used.
        /// </summary>
        /// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>
        protected override void Dispose(bool disposing)
        {
          if (disposing && (components != null))
          {
            components.Dispose();
          }
          base.Dispose(disposing);
        }
    
        #region Windows Form Designer generated code
    
        /// <summary>
        /// Required method for Designer support - do not modify
        /// the contents of this method with the code editor.
        /// </summary>
        private void InitializeComponent()
        {
          this.dataGridViewQuery = new System.Windows.Forms.DataGridView();
          this.Clause = new System.Windows.Forms.DataGridViewComboBoxColumn();
          this.Field = new System.Windows.Forms.DataGridViewComboBoxColumn();
          this.Operator = new System.Windows.Forms.DataGridViewComboBoxColumn();
          this.Value = new System.Windows.Forms.DataGridViewComboBoxColumn();
          ((System.ComponentModel.ISupportInitialize)(this.dataGridViewQuery)).BeginInit();
          this.SuspendLayout();
          // 
          // dataGridViewQuery
          // 
          this.dataGridViewQuery.ColumnHeadersHeightSizeMode = System.Windows.Forms.DataGridViewColumnHeadersHeightSizeMode.AutoSize;
          this.dataGridViewQuery.Columns.AddRange(new System.Windows.Forms.DataGridViewColumn[] {
          this.Clause,
          this.Field,
          this.Operator,
          this.Value});
          this.dataGridViewQuery.Dock = System.Windows.Forms.DockStyle.Fill;
          this.dataGridViewQuery.EditMode = System.Windows.Forms.DataGridViewEditMode.EditOnEnter;
          this.dataGridViewQuery.Location = new System.Drawing.Point(0, 0);
          this.dataGridViewQuery.Name = "dataGridViewQuery";
          this.dataGridViewQuery.SelectionMode = System.Windows.Forms.DataGridViewSelectionMode.FullRowSelect;
          this.dataGridViewQuery.Size = new System.Drawing.Size(604, 429);
          this.dataGridViewQuery.TabIndex = 1;
          this.dataGridViewQuery.CellEnter += new System.Windows.Forms.DataGridViewCellEventHandler(this.dataGridViewQuery_CellEnter);
          this.dataGridViewQuery.UserAddedRow += new System.Windows.Forms.DataGridViewRowEventHandler(this.dataGridViewQuery_UserAddedRow);
          // 
          // Clause
          // 
          this.Clause.DisplayStyle = System.Windows.Forms.DataGridViewComboBoxDisplayStyle.ComboBox;
          this.Clause.HeaderText = "Clause";
          this.Clause.Name = "Clause";
          this.Clause.Width = 50;
          // 
          // Field
          // 
          this.Field.DisplayStyle = System.Windows.Forms.DataGridViewComboBoxDisplayStyle.ComboBox;
          this.Field.HeaderText = "Field";
          this.Field.Items.AddRange(new object[] {
          "ID",
          "Priority",
          "Severity",
          "Description",
          "Category"});
          this.Field.Name = "Field";
          this.Field.Width = 175;
          // 
          // Operator
          // 
          this.Operator.DisplayStyle = System.Windows.Forms.DataGridViewComboBoxDisplayStyle.ComboBox;
          this.Operator.HeaderText = "Operator";
          this.Operator.Name = "Operator";
          this.Operator.Width = 125;
          // 
          // Value
          // 
          this.Value.AutoSizeMode = System.Windows.Forms.DataGridViewAutoSizeColumnMode.Fill;
          this.Value.DisplayStyle = System.Windows.Forms.DataGridViewComboBoxDisplayStyle.ComboBox;
          this.Value.HeaderText = "Value";
          this.Value.Name = "Value";
          this.Value.Resizable = System.Windows.Forms.DataGridViewTriState.True;
          // 
          // Form1
          // 
          this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
          this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
          this.ClientSize = new System.Drawing.Size(604, 429);
          this.Controls.Add(this.dataGridViewQuery);
          this.Name = "Form1";
          this.Text = "Form1";
          ((System.ComponentModel.ISupportInitialize)(this.dataGridViewQuery)).EndInit();
          this.ResumeLayout(false);
    
        }
    
        #endregion
    
        private System.Windows.Forms.DataGridView dataGridViewQuery;
        private System.Windows.Forms.DataGridViewComboBoxColumn Clause;
        private System.Windows.Forms.DataGridViewComboBoxColumn Field;
        private System.Windows.Forms.DataGridViewComboBoxColumn Operator;
        private System.Windows.Forms.DataGridViewComboBoxColumn Value;
      }
    

     Any help will be helpful. I am stuck with this.

     

    - Girija Shankar


    Girija Shankar Beuria
    Tuesday, January 11, 2011 3:31 PM

Answers

  • Your  implementation should not be setting the Cell style during the Cell's Enter event. This has the chance of causing recursion when the DataGridView calls into SetCurrentCellAddressCore to set the currently selected cell. Since SetCurrentCellAddressCore may be on the stack already when the user enters the cell, trying to reset the cell from this event could lead to an exception.

     

    You should change the design such that you remove the code which sets the Cell from the Enter event. A better solution is to add that code on the Cell's CellValueChanged event. This event fires after the cell's value is modified. What you can do is check to see what the value is, and then set the last column's cell as needed from here before focus ever reaches the new cell. I tried this code and it seemed to work well. I have sent you the code for the CellValueChanged, as well as the new code for CellEnter.

     

     

    private void dataGridViewQuery_CellValueChanged(object sender, DataGridViewCellEventArgs e)

            {

                DataGridViewComboBoxCell cell = null;

                ValueCellType cellType;

                string fieldColumnValue = string.Empty;

     

     

                if ((e.ColumnIndex == 1 || e.ColumnIndex == 2) && e.RowIndex >= 0)

                {

                   cell = (DataGridViewComboBoxCell)dataGridViewQuery.Rows[e.RowIndex].Cells[1];

                    fieldColumnValue = Convert.ToString(cell.Value);

                    cellType = this.GetCellType(fieldColumnValue);

     

                    if (cellType == ValueCellType.Text)

                    {

                        dataGridViewQuery.Rows[e.RowIndex].Cells[3] = new DataGridViewTextBoxCell();

                    }

                    else if (cellType == ValueCellType.Combo)

                    {

                        cell = new DataGridViewComboBoxCell();

                        cell.DisplayStyle = DataGridViewComboBoxDisplayStyle.ComboBox;

                        this.setValuesForValues(fieldColumnValue, cell);

     

                        int rowIndex = e.RowIndex;

                        int columnIndex = 3;

     

                        dataGridViewQuery.Rows[rowIndex].Cells[columnIndex] = cell;

                    }

     

                }

            }

     

     

     

    private void dataGridViewQuery_CellEnter(object sender, DataGridViewCellEventArgs e)

            {

                   

                DataGridViewComboBoxCell cell = null;

            

                string fieldColumnValue = string.Empty;

     

                if (e.ColumnIndex == 0 && e.RowIndex != 0 && !isClauseValueSet)

                {

                    cell = (DataGridViewComboBoxCell)dataGridViewQuery.Rows[e.RowIndex].Cells[e.ColumnIndex];

                    cell.Items.Add("And");

                    cell.Items.Add("Or");

                    this.isClauseValueSet = true;

                }

     

                if (e.ColumnIndex == 1 && e.RowIndex != 0)

                {

                    cell = (DataGridViewComboBoxCell)dataGridViewQuery.Rows[e.RowIndex].Cells[0];

                    if (string.IsNullOrEmpty(Convert.ToString(cell.Value)))

                    {

                        MessageBox.Show("Please select a value in the clause column");

                    }

                }

     

                if (e.ColumnIndex == 2 && !isOperatorValueSet)

                {

                    cell = (DataGridViewComboBoxCell)dataGridViewQuery.Rows[e.RowIndex].Cells[1];

                    if (string.IsNullOrEmpty(Convert.ToString(cell.Value)))

                    {

                        MessageBox.Show("Please select a Value in the field column");

                    }

                    else

                    {

                        fieldColumnValue = Convert.ToString(cell.Value);

                        cell = (DataGridViewComboBoxCell)dataGridViewQuery.Rows[e.RowIndex].Cells[e.ColumnIndex];

                        this.setValuesForOperator(fieldColumnValue, cell);

                    }

                }

     

                if (e.ColumnIndex == 3 && !isFieldValueSet)

                {

     

                    cell = (DataGridViewComboBoxCell)dataGridViewQuery.Rows[e.RowIndex].Cells[1];

                    if (string.IsNullOrEmpty(Convert.ToString(cell.Value)))

                    {

                        MessageBox.Show("Please select a Value in the field column");

                    }

                    cell = (DataGridViewComboBoxCell)dataGridViewQuery.Rows[e.RowIndex].Cells[2];

                    if (string.IsNullOrEmpty(Convert.ToString(cell.Value)))

                    {

                        MessageBox.Show("Please select a Value in the operator column");

                    }

     

                }

                    

            }

     

    If it continues to be an issue you should contact Developer Support to see what various support options there are.

    There are various support options such as advisory and per issue. Please visit the below link to see the various paid

     

    support options that are available to better meet your needs.

     

    http://support.microsoft.com/default.aspx?id=fh;en-us;offerprophone

     

     

    Thanks


    bill boyce
    Friday, February 04, 2011 3:03 PM

All replies

  • No responses :-(


    Girija Shankar Beuria
    Thursday, January 13, 2011 5:36 AM
  • Hi Girija,

    Thank you for your question. 

    I am trying to involve someone familiar with this topic to further look at this issue. There might be some time delay. Appreciate your patience.

    Thank you for your understanding and support.

    Sincerely,


    Bob Bao [MSFT]
    MSDN Community Support | Feedback to us
    Get or Request Code Sample from Microsoft
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    Thursday, January 13, 2011 12:03 PM
  • The sample code was reviewed and was reproduced. Here is the callstack of the failure.

     

    System.InvalidOperationException was unhandled

      Message="Operation is not valid because it results in a reentrant call to the SetCurrentCellAddressCore function."

      Source="System.Windows.Forms"

      StackTrace:

           at System.Windows.Forms.DataGridView.SetCurrentCellAddressCore(Int32 columnIndex, Int32 rowIndex, Boolean setAnchorCellAddress, Boolean validateCurrentCell, Boolean throughMouseClick)

           at System.Windows.Forms.DataGridView.ResetCurrentCell()

           at System.Windows.Forms.DataGridView.OnReplacingCell(DataGridViewRow dataGridViewRow, Int32 columnIndex)

           at System.Windows.Forms.DataGridViewCellCollection.set_Item(Int32 index, DataGridViewCell value)

           at WindowsFormsApplication7.Form1.dataGridViewQuery_CellEnter(Object sender, DataGridViewCellEventArgs e) in D:\data\projects\vs2008\WindowsFormsApplication7\WindowsFormsApplication7\Form1.cs:line 118

           at System.Windows.Forms.DataGridView.OnCellEnter(DataGridViewCellEventArgs e)

           at System.Windows.Forms.DataGridView.OnCellEnter(DataGridViewCell& dataGridViewCell, Int32 columnIndex, Int32 rowIndex)

           at System.Windows.Forms.DataGridView.SetCurrentCellAddressCore(Int32 columnIndex, Int32 rowIndex, Boolean setAnchorCellAddress, Boolean validateCurrentCell, Boolean throughMouseClick)

           at System.Windows.Forms.DataGridView.OnCellMouseDown(HitTestInfo hti, Boolean isShiftDown, Boolean isControlDown)

           at System.Windows.Forms.DataGridView.OnCellMouseDown(DataGridViewCellMouseEventArgs e)

           at System.Windows.Forms.DataGridView.OnMouseDown(MouseEventArgs e)

           at System.Windows.Forms.Control.WmMouseDown(Message& m, MouseButtons button, Int32 clicks)

           at System.Windows.Forms.Control.WndProc(Message& m)

           at System.Windows.Forms.DataGridView.WndProc(Message& m)

           at System.Windows.Forms.Control.ControlNativeWindow.WndProc(Message& m)

           at System.Windows.Forms.NativeWindow.DebuggableCallback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam)

           at System.Windows.Forms.UnsafeNativeMethods.DispatchMessageW(MSG& msg)

           at System.Windows.Forms.Application.ComponentManager.System.Windows.Forms.UnsafeNativeMethods.IMsoComponentManager.FPushMessageLoop(Int32 dwComponentID, Int32 reason, Int32 pvLoopData)

           at System.Windows.Forms.Application.ThreadContext.RunMessageLoopInner(Int32 reason, ApplicationContext context)

           at System.Windows.Forms.Application.ThreadContext.RunMessageLoop(Int32 reason, ApplicationContext context)

           at WindowsFormsApplication7.Program.Main() in D:\data\projects\vs2008\WindowsFormsApplication7\WindowsFormsApplication7\Program.cs:line 18

           at System.AppDomain._nExecuteAssembly(Assembly assembly, String[] args)

           at Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly()

           at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)

           at System.Threading.ThreadHelper.ThreadStart()

      InnerException:

     

     

     

    And here is where is actually stops in the sample app.

     

    > WindowsFormsApplication7.exe!WindowsFormsApplication7.Form1.dataGridViewQuery_CellEnter(object, System.Windows.Forms.DataGridViewCellEventArgs) C#

      System.Windows.Forms.dll!System.Windows.Forms.DataGridView.OnCellEnter(System.Windows.Forms.DataGridViewCellEventArgs)

      System.Windows.Forms.dll!System.Windows.Forms.DataGridView.OnCellEnter(ref System.Windows.Forms.DataGridViewCell, int, int)

      System.Windows.Forms.dll!System.Windows.Forms.DataGridView.SetCurrentCellAddressCore(int, int, bool, bool, bool)

      System.Windows.Forms.dll!System.Windows.Forms.DataGridView.OnCellMouseDown(System.Windows.Forms.DataGridView.HitTestInfo, bool, bool)

      System.Windows.Forms.dll!System.Windows.Forms.DataGridView.OnCellMouseDown(System.Windows.Forms.DataGridViewCellMouseEventArgs)

      System.Windows.Forms.dll!System.Windows.Forms.DataGridView.OnMouseDown(System.Windows.Forms.MouseEventArgs)

      System.Windows.Forms.dll!System.Windows.Forms.Control.WmMouseDown(ref System.Windows.Forms.Message, System.Windows.Forms.MouseButtons, int)

      System.Windows.Forms.dll!System.Windows.Forms.Control.WndProc(ref System.Windows.Forms.Message)

      System.Windows.Forms.dll!System.Windows.Forms.DataGridView.WndProc(ref System.Windows.Forms.Message)

      System.Windows.Forms.dll!System.Windows.Forms.Control.ControlNativeWindow.WndProc(ref System.Windows.Forms.Message)

      System.Windows.Forms.dll!System.Windows.Forms.NativeWindow.DebuggableCallback(System.IntPtr, int, System.IntPtr, System.IntPtr)

      [Native to Managed Transition]

      [Managed to Native Transition]

      System.Windows.Forms.dll!System.Windows.Forms.Application.ComponentManager.System.Windows.Forms.UnsafeNativeMethods.IMsoComponentManager.FPushMessageLoop(int, int, int)

      System.Windows.Forms.dll!System.Windows.Forms.Application.ThreadContext.RunMessageLoopInner(int, System.Windows.Forms.ApplicationContext)

      System.Windows.Forms.dll!System.Windows.Forms.Application.ThreadContext.RunMessageLoop(int, System.Windows.Forms.ApplicationContext)

      WindowsFormsApplication7.exe!WindowsFormsApplication7.Program.Main() C#

     

     

    Inside of dataGridViewQuery_CellEnter, this is the relevant code:

     

     private void dataGridViewQuery_CellEnter(object sender, DataGridViewCellEventArgs e)

            {

           

            <snip>

           

          

               

                            cell = new DataGridViewComboBoxCell();

                            cell.DisplayStyle = DataGridViewComboBoxDisplayStyle.ComboBox;

                            this.setValuesForValues(fieldColumnValue, cell);

                            dataGridViewQuery.Rows[e.RowIndex].Cells[e.ColumnIndex] = cell;

     

                         

           <snip>

          

           }

          

          

    This code is replacing a cell at the specified row index and column index with one that you are creating. You are doing this from within the Cell Enter event, so in actuality you are replacing the current cell which just got the focus. This is not recommended from within these sorts of events and could lead to unexpected problems. The exception itself is telling you that it would result in re-entrancy occurring which is not a good thing in this case. What is not clear at this point is why exactly it requires the fourth column of the fourth row. The code itself above only appears to be replacing the cell when it is the fourth column (it checks the column index), but why it doesn't occur on one of the first three rows is not exactly clear. I would expect this to fail on the first row actually.

     

    Looking at the code, why would they need to replace the cell at all?  I don't see any reason why they actually need to replace the cell. It seems the major point of what they are doing is to define the items in the last ComboBoxCell. If that is all they need, they can just call their code "this.setValuesForValues" for the current cell, rather than creating a new cell and replacing it. For example, comment out these lines:

     

     

     

                            //cell = new DataGridViewComboBoxCell();

                            //cell.DisplayStyle = DataGridViewComboBoxDisplayStyle.ComboBox;

                            //this.setValuesForValues(fieldColumnValue, cell);

                            //dataGridViewQuery.Rows[e.RowIndex].Cells[e.ColumnIndex] = cell;

     

    And then add these lines:

     

     

                            cell = dataGridViewQuery.Rows[e.RowIndex].Cells[e.ColumnIndex] as DataGridViewComboBoxCell;

                            if (cell != null)

                            {

                                this.setValuesForValues(fieldColumnValue, cell);

                            }

     

     

    This has the same effect as the previous code, except we are modifying the items of the existing ComboBoxCell rather than creating a new one and replacing it.

     

    Hope this helps,



    bill boyce
    Tuesday, January 18, 2011 6:44 PM
  • Thanks for the reply.  I was replacing the cell is because based on the 2nd column value (Field Column) selected for that row, i need to change the type.

    For example if user select teh second column value as "Description" then i need to change that to a text box so that user can type it.

    If user selects 2nd column as "Priority" , i need to provide the user witha  combobox cell with values populated in it.

    - Girija


    Girija Shankar Beuria
    Thursday, January 20, 2011 7:00 AM
  • No responses still :-(.

     

    The above solution actually doensot work for me.

     

    - Girija


    Girija Shankar Beuria
    Tuesday, January 25, 2011 5:15 AM
  • Your  implementation should not be setting the Cell style during the Cell's Enter event. This has the chance of causing recursion when the DataGridView calls into SetCurrentCellAddressCore to set the currently selected cell. Since SetCurrentCellAddressCore may be on the stack already when the user enters the cell, trying to reset the cell from this event could lead to an exception.

     

    You should change the design such that you remove the code which sets the Cell from the Enter event. A better solution is to add that code on the Cell's CellValueChanged event. This event fires after the cell's value is modified. What you can do is check to see what the value is, and then set the last column's cell as needed from here before focus ever reaches the new cell. I tried this code and it seemed to work well. I have sent you the code for the CellValueChanged, as well as the new code for CellEnter.

     

     

    private void dataGridViewQuery_CellValueChanged(object sender, DataGridViewCellEventArgs e)

            {

                DataGridViewComboBoxCell cell = null;

                ValueCellType cellType;

                string fieldColumnValue = string.Empty;

     

     

                if ((e.ColumnIndex == 1 || e.ColumnIndex == 2) && e.RowIndex >= 0)

                {

                   cell = (DataGridViewComboBoxCell)dataGridViewQuery.Rows[e.RowIndex].Cells[1];

                    fieldColumnValue = Convert.ToString(cell.Value);

                    cellType = this.GetCellType(fieldColumnValue);

     

                    if (cellType == ValueCellType.Text)

                    {

                        dataGridViewQuery.Rows[e.RowIndex].Cells[3] = new DataGridViewTextBoxCell();

                    }

                    else if (cellType == ValueCellType.Combo)

                    {

                        cell = new DataGridViewComboBoxCell();

                        cell.DisplayStyle = DataGridViewComboBoxDisplayStyle.ComboBox;

                        this.setValuesForValues(fieldColumnValue, cell);

     

                        int rowIndex = e.RowIndex;

                        int columnIndex = 3;

     

                        dataGridViewQuery.Rows[rowIndex].Cells[columnIndex] = cell;

                    }

     

                }

            }

     

     

     

    private void dataGridViewQuery_CellEnter(object sender, DataGridViewCellEventArgs e)

            {

                   

                DataGridViewComboBoxCell cell = null;

            

                string fieldColumnValue = string.Empty;

     

                if (e.ColumnIndex == 0 && e.RowIndex != 0 && !isClauseValueSet)

                {

                    cell = (DataGridViewComboBoxCell)dataGridViewQuery.Rows[e.RowIndex].Cells[e.ColumnIndex];

                    cell.Items.Add("And");

                    cell.Items.Add("Or");

                    this.isClauseValueSet = true;

                }

     

                if (e.ColumnIndex == 1 && e.RowIndex != 0)

                {

                    cell = (DataGridViewComboBoxCell)dataGridViewQuery.Rows[e.RowIndex].Cells[0];

                    if (string.IsNullOrEmpty(Convert.ToString(cell.Value)))

                    {

                        MessageBox.Show("Please select a value in the clause column");

                    }

                }

     

                if (e.ColumnIndex == 2 && !isOperatorValueSet)

                {

                    cell = (DataGridViewComboBoxCell)dataGridViewQuery.Rows[e.RowIndex].Cells[1];

                    if (string.IsNullOrEmpty(Convert.ToString(cell.Value)))

                    {

                        MessageBox.Show("Please select a Value in the field column");

                    }

                    else

                    {

                        fieldColumnValue = Convert.ToString(cell.Value);

                        cell = (DataGridViewComboBoxCell)dataGridViewQuery.Rows[e.RowIndex].Cells[e.ColumnIndex];

                        this.setValuesForOperator(fieldColumnValue, cell);

                    }

                }

     

                if (e.ColumnIndex == 3 && !isFieldValueSet)

                {

     

                    cell = (DataGridViewComboBoxCell)dataGridViewQuery.Rows[e.RowIndex].Cells[1];

                    if (string.IsNullOrEmpty(Convert.ToString(cell.Value)))

                    {

                        MessageBox.Show("Please select a Value in the field column");

                    }

                    cell = (DataGridViewComboBoxCell)dataGridViewQuery.Rows[e.RowIndex].Cells[2];

                    if (string.IsNullOrEmpty(Convert.ToString(cell.Value)))

                    {

                        MessageBox.Show("Please select a Value in the operator column");

                    }

     

                }

                    

            }

     

    If it continues to be an issue you should contact Developer Support to see what various support options there are.

    There are various support options such as advisory and per issue. Please visit the below link to see the various paid

     

    support options that are available to better meet your needs.

     

    http://support.microsoft.com/default.aspx?id=fh;en-us;offerprophone

     

     

    Thanks


    bill boyce
    Friday, February 04, 2011 3:03 PM
  • The above worked. Thanks.

     

    - Girija


    Girija Shankar Beuria
    Tuesday, February 08, 2011 8:13 AM