none
DataGridView - Invisible Cell Error

    Question

  • Hi, I have a potential problem with the datagridview.

    When I have an empty table, and start editing the first row, but press ESC to cancel the edit, I get an error "Current cell cannot be set to an invisible cell.". Is this a known problem, or is there a way to trap the error, and move the current cell to a cell that is not invisible?

    Thursday, January 26, 2006 5:11 PM

Answers

  • This is a bug when the first column is hidden. I don’t know any workaround at this time.

     

    -mark

    DataGridViewProgram Manager

    Microsoft

    This post is provided "as-is"

     

    Friday, January 27, 2006 6:22 PM

All replies

  • We have just run into this same problem.
    Friday, January 27, 2006 5:16 PM
  • This is a bug when the first column is hidden. I don’t know any workaround at this time.

     

    -mark

    DataGridViewProgram Manager

    Microsoft

    This post is provided "as-is"

     

    Friday, January 27, 2006 6:22 PM
  • Thanks for the quick reply.  In my case I was able to reorder my first column so I was able to get around it.
    Friday, January 27, 2006 6:47 PM
  • In my case when I reorder my first column it still threw the 'System.InvalidOperationException' exception. So what I did as a work around for now was override the sort function.

     public override void Sort(DataGridViewColumn dataGridViewColumn, ListSortDirection direction)
    {
                try
                {
                    base.Sort(dataGridViewColumn, direction);
                }
                catch {}
    }


    All seems to work fine now, just wondering would this cause any implications ?
    Monday, February 27, 2006 1:03 AM
  • Dear Mark,

    we also have this problem though the first cell is not invisible. Is there any workaround for this bug ??

     

    Thursday, November 23, 2006 4:27 PM
  •  Mark Rideout wrote:

    This is a bug when the first column is hidden. I don’t know any workaround at this time.

    -mark

    DataGridViewProgram Manager

    Microsoft

    This post is provided "as-is"



    It isn't correct.
    This is a bug when one o more column are hidden.
    Why you didn't solve this bug in SP1 of framework 2.0 ?

    :-(((((((((
    Thursday, December 28, 2006 9:49 AM
  • Hi,
    I think I've found a workaround.

    I have used the method AutoGenerateColumns of Datagridview set to false;
    In this way, the datagridview does not create the columns alone, but we must be we to decide what columns to show and what no..
    Adding some invisible columns is also possible, it is enough that they are not placed to the first places of the datagridview.
    I enclose example.


    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Text;
    using System.Windows.Forms;
    using System.Data.Sql;
    using System.Data.SqlClient;

    namespace WindowsApplication2
    {
        public class Form2 : Form
        {
            private System.Windows.Forms.DataGridView dataGridView1;
            private System.Windows.Forms.DataGridViewTextBoxColumn Column1;
            private System.Windows.Forms.DataGridViewTextBoxColumn Column2;
            private System.Windows.Forms.DataGridViewTextBoxColumn Column3;

            DataTable dt;
            DataView dw;
            public Form2()
            {
                dt = new DataTable("MyTable");
                dw = new DataView(dt);
                dt.Columns.Add("Column1");
                dt.Columns.Add("Column2");
                dt.Columns.Add("Column3");

                InitializeComponent();
                //
                // Column1
                //
                this.Column1.DataPropertyName = "ProductId";
                this.Column1.HeaderText = "Column1";
                this.Column1.Name = "Column1";
                this.Column1.Visible = false;
                //
                // Column2
                //
                this.Column2.DataPropertyName = "Column2";
                this.Column2.HeaderText = "Column2";
                this.Column2.Name = "Column2";
                //
                // Column3
                //
                this.Column3.DataPropertyName = "Column3";
                this.Column3.HeaderText = "Column3";
                this.Column3.Name = "Column3";

                // Here I've error
                this.dataGridView1.Columns.AddRange(new System.Windows.Forms.DataGridViewColumn[] {
                this.Column1,
                this.Column2,
                this.Column3});

                // If I remove this.Column1
                // I haven't error
                //this.dataGridView1.Columns.AddRange(new System.Windows.Forms.DataGridViewColumn[] {
                //this.Column2,
                //this.Column3});

                // OR
                // Invert Column1 with Column2 in this way
                //this.dataGridView1.Columns.AddRange(new System.Windows.Forms.DataGridViewColumn[] {
                //this.Column2,
                //this.Column1,
                //this.Column3});


                dataGridView1.AutoGenerateColumns = false;
                dataGridView1.DataSource = dt;
                this.Validate();
            }

            private void InitializeComponent()
            {
                this.dataGridView1 = new System.Windows.Forms.DataGridView();
                this.Column1 = new System.Windows.Forms.DataGridViewTextBoxColumn();
                this.Column2 = new System.Windows.Forms.DataGridViewTextBoxColumn();
                this.Column3 = new System.Windows.Forms.DataGridViewTextBoxColumn();
                ((System.ComponentModel.ISupportInitialize)(this.dataGridView1)).BeginInit();
                this.SuspendLayout();
                //
                // dataGridView1
                //
                this.dataGridView1.ColumnHeadersHeightSizeMode = System.Windows.Forms.DataGridViewColumnHeadersHeightSizeMode.AutoSize;
                this.dataGridView1.EditMode = System.Windows.Forms.DataGridViewEditMode.EditOnEnter;
                this.dataGridView1.Location = new System.Drawing.Point(21, 12);
                this.dataGridView1.Name = "dataGridView1";
                this.dataGridView1.Size = new System.Drawing.Size(296, 150);
                this.dataGridView1.TabIndex = 0;
                //
                // Column1
                //
                this.Column1.Name = "Column1";
                //
                // Column2
                //
                this.Column2.Name = "Column2";
                //
                // Column3
                //
                this.Column3.Name = "Column3";
                //
                // Form2
                //
                this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
                this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
                this.ClientSize = new System.Drawing.Size(453, 289);
                this.Controls.Add(this.dataGridView1);
                this.Name = "Form2";
                this.Text = "Form2";
                ((System.ComponentModel.ISupportInitialize)(this.dataGridView1)).EndInit();
                this.ResumeLayout(false);

            }

            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);
            }
        }

    }

    Sunday, January 07, 2007 4:11 PM
  •  

     

    My workaround if I've to set a Row which has same column not visible would be:

    public int SelectRow
    {
          get
          {
            return this.CurrentCell.RowIndex;
          }
          set
          {
            this.FirstDisplayedScrollingRowIndex = value;
            this.CurrentCell = this[GetFirstVisibleColumn(), value];
          }
    }

    public int GetFirstVisibleColumn()
    {
          foreach (DataGridViewColumn c in this.Columns)
          {
            if (c.Visible == true)
              return c.Index;
          }

          return -1;
    }

    Thursday, February 22, 2007 2:49 PM
  • Hi!

     

    I solved this by putting all my invisible columns at the end of the datagrid....

    Tuesday, April 03, 2007 5:54 AM
  • I found that by writing : 
    DataGridView1.CurrentCell = DataGridView1.Rows(x).Cells("myName") (name)

     

    Instead of :

    DataGridView1.CurrentCell = DataGridView1.Rows(x).Cells(0)  (index)
    
    
    was working. looks like with the index im getting the invisible cell error and not when I go with the name.

    Friday, October 29, 2010 2:01 PM
  • public class DataGridViewWrapper : DataGridView
        {
            public new DataGridViewCell CurrentCell
            {
                get
                {
                    if (base.CurrentCell != null && !base.CurrentCell.Visible)
                    {
                        return FirstDisplayedCell;
                    }
                    return base.CurrentCell;
                }
                set { base.CurrentCell = value; }
            }
        }

    elmolives
    Thursday, June 23, 2011 12:27 PM
  • Hi , this error comes when you try to select an hidden cell.
    Monday, September 26, 2011 7:35 AM
  • Dear Friend

    No Solution for this problem, Question raised on 2006 upto now.....,

    set CurrentCell = Invisible Cell .....???...!!!!

    Thanks

    Friday, December 07, 2012 10:46 AM
  • While catching the exception during sorting as Quang suggested does work, I also found that simply setting the current cell to nothing (or your language's equivalent) before the sort occurs can also be used to prevent the error.  You can do this by setting the columns to programmatic sort and then using the ColumnHeaderMouseClick event.

    I think perhaps what this is saying is that at the end of the sort (and perhaps other operations like editing which OP mentioned), it sets the current cell to the previous current which may or may not be visible depending on whether row order has changed.  But this is just a theory.

    Sunday, September 07, 2014 1:28 AM