DataGridView - Invisible Cell Error

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



Answer this question

DataGridView - Invisible Cell Error

  • Learning 2 Code

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

    }


  • cspoon

    Dear Mark,

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


  • Daniel Hern&amp;#225;n Pallarez

    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"


  • Amnesiasoft

    We have just run into this same problem.

  • Andrew Jones

    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

    :-(((((((((

  • Erwin De Leon

    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

  • Ben Luna

    Thanks for the quick reply. In my case I was able to reorder my first column so I was able to get around it.
  • Trinh Ng.

    Hi!

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


  • preim

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


  • DataGridView - Invisible Cell Error