Windows Develop Bookmark and Share   
 index > Windows Forms Data Controls and Databinding > Disable navigation in datagridview
 

Disable navigation in datagridview

Is it possible to disable the navigation within a datagridview completely?

I'm referring to this - this property below can have values such as:
DataGridView1.SelectionMode = DataGridViewSelectionMode.CellSelect
or
DataGridView1.SelectionMode = DataGridViewSelectionMode.FullRowSelect
etc.

What I wantto do issomething like
DataGridView1.SelectionMode = DataGridViewSelectionMode.None

Now I know there is no 'none' value available for the selectionmode property on a datagridview, but that is what I'm looking at implementing, through any possible manner.

So, is this possible? If so, how can I do it?

reachrishikh  Tuesday, March 31, 2009 1:46 PM
Hi reachrishikh,

Yes, we can disabled completely. Because the first cell gets selected after the form is Shown, we can handle the Showing event handler in which we clear the selection.

I've made some changes to my original code which we can still edit the cell while we can't when using my previous code.

    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
            this.dataGridView1.SelectionMode = DataGridViewSelectionMode.CellSelect;
            this.dataGridView1.CellClick += new DataGridViewCellEventHandler(dataGridView1_CellClick);
            this.dataGridView1.DoubleClick += new EventHandler(dataGridView1_DoubleClick);
            this.dataGridView1.ColumnCount = 2;
            this.dataGridView1.RowCount = 5;
            this.dataGridView1.ClearSelection();
            this.Shown += new EventHandler(Form1_Shown);
        }

        void Form1_Shown(object sender, EventArgs e)
        {
            this.dataGridView1.ClearSelection();

            //or you can try with the following statement
            //this.dataGridView1[0, 0].Selected = false;
        }

        void dataGridView1_DoubleClick(object sender, EventArgs e)
        {
            this.dataGridView1.CurrentCell.Selected = false;
        }

        void dataGridView1_CellClick(object sender, DataGridViewCellEventArgs e)
        {
           
            this.dataGridView1.CurrentCell.Selected = false;
        }
    }

Try and let me know your test result.

Best regards,
Bruce Zhou
Please mark the replies as answers if they help and unmark if they don't.
Bruce.Zhou  Thursday, April 02, 2009 6:57 AM
Hi reachrishikh,

I don't you want to create a customControl before. However, it's never late for me to know this now.

We can derive from the DataGridView, and override the OnGetFocus, OnCellMouseClick, OnCellMouseDoubleClick method, and define a bool variable to indicate if the DataGridView supports "Can't be selected" functionality.

Just for your convenience, I paste all of the code now. It can work pretty well on my side.

    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
            DataGridViewEx dgvE = new DataGridViewEx();
            this.Controls.Add(dgvE);
            dgvE.IsNotAllowSelection = true;
            dgvE.ColumnCount = 2;
            dgvE.RowCount = 5;
        }
    }
    public class DataGridViewEx : DataGridView
    {
        private bool isNotAllowSelection;

        public bool IsNotAllowSelection
        {
            get { return isNotAllowSelection; }
            set { isNotAllowSelection = value; }
        }
        protected override void OnGotFocus(EventArgs e)
        {
            base.OnGotFocus(e);
            if (isNotAllowSelection)
            {
                this.ClearSelection();
            }
        }
        protected override void OnCellMouseClick(DataGridViewCellMouseEventArgs e)
        {
            if (isNotAllowSelection)
            {
                this.ClearSelection();
            }
        }
        protected override void OnCellMouseDoubleClick(DataGridViewCellMouseEventArgs e)
        {
            if (isNotAllowSelection)
            {
                this.ClearSelection();
            }
        }
    }

Let me know if you have problem with the code.

Best regards,
Bruce Zhou
Please mark the replies as answers if they help and unmark if they don't.
Bruce.Zhou  Friday, April 03, 2009 6:05 AM
I am not sure exactly what you are trying to do here. Would disabling the datagridview work?
Orlando Code Camp March 28, 2009 http://www.orlandocodecamp.com
Ken Tucker  Tuesday, March 31, 2009 6:09 PM
Nope, it doesn't make the selection rectangle go away.

What I'm saying is, if there are property values to either select individual cells, or full rows, or full columns, or full row headers, or full column headers (via the DataGridView.SelectionMode property), where the programmer can set how theuser will select values inside theDataGridView,then there should similarly be a value to select nothing inside the grid at all, where no selection rectangle appears.

I think this feature is implemented in the newer WPF third-partyDataGrid controls, but is there any such feature in windows forms, where I can prevent any selection rectangle from appearing inside a datagridview even when it is enabled and non-readonly, and the currently selected control?
reachrishikh  Tuesday, March 31, 2009 8:38 PM
Hi reachrishikh,

The DataGridView doesn't have this feature by default. However, we can simulate this. Have a try with the following code:


        public Form1()
        {
            InitializeComponent();
            this.dataGridView1.SelectionMode = DataGridViewSelectionMode.CellSelect;
            this.dataGridView1.CellClick += new DataGridViewCellEventHandler(dataGridView1_CellClick);
            this.dataGridView1.DoubleClick += new EventHandler(dataGridView1_DoubleClick);
            this.dataGridView1.ColumnCount = 2;
            this.dataGridView1.RowCount = 5;
        }

        void dataGridView1_DoubleClick(object sender, EventArgs e)
        {
            this.dataGridView1.CurrentCell = null;
        }

        void dataGridView1_CellClick(object sender, DataGridViewCellEventArgs e)
        {
            this.dataGridView1.CurrentCell = null;
        }



Best regards,
Bruce Zhou
Please mark the replies as answers if they help and unmark if they don't.
Bruce.Zhou  Thursday, April 02, 2009 1:57 AM
Thanks, that helped, but it will still require a lot of coding to implement completely. And I tried putting the Me.DataGridView1.CurrentCell = Nothing in the form load event handler, why isn't it working? It works fine for the click and double click events, but I need it disabled completely, right from the start. Is there any other workaround?
reachrishikh  Thursday, April 02, 2009 6:40 AM
Hi reachrishikh,

Yes, we can disabled completely. Because the first cell gets selected after the form is Shown, we can handle the Showing event handler in which we clear the selection.

I've made some changes to my original code which we can still edit the cell while we can't when using my previous code.

    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
            this.dataGridView1.SelectionMode = DataGridViewSelectionMode.CellSelect;
            this.dataGridView1.CellClick += new DataGridViewCellEventHandler(dataGridView1_CellClick);
            this.dataGridView1.DoubleClick += new EventHandler(dataGridView1_DoubleClick);
            this.dataGridView1.ColumnCount = 2;
            this.dataGridView1.RowCount = 5;
            this.dataGridView1.ClearSelection();
            this.Shown += new EventHandler(Form1_Shown);
        }

        void Form1_Shown(object sender, EventArgs e)
        {
            this.dataGridView1.ClearSelection();

            //or you can try with the following statement
            //this.dataGridView1[0, 0].Selected = false;
        }

        void dataGridView1_DoubleClick(object sender, EventArgs e)
        {
            this.dataGridView1.CurrentCell.Selected = false;
        }

        void dataGridView1_CellClick(object sender, DataGridViewCellEventArgs e)
        {
           
            this.dataGridView1.CurrentCell.Selected = false;
        }
    }

Try and let me know your test result.

Best regards,
Bruce Zhou
Please mark the replies as answers if they help and unmark if they don't.
Bruce.Zhou  Thursday, April 02, 2009 6:57 AM
Yes that worked, thanks a lot.

But if there is already a ClearSelection() method which clears the datagridview of all selected items, wouldn't it be better for me to use this method in all the places in your code above instead of setting the DataGridView.CurrentCell.Selected property to false for individual cells?


Again, for my implementation, it will require a lot of coding to get the exact behaviour I'm looking for. I think I'll be done in a few hours. Although you've more or less answered my originalissue,I'll just leave this thread open till then just in case I have any more questions.

Again, thanks for the help Bruce.
reachrishikh  Thursday, April 02, 2009 8:43 AM
Yes, you can use it as long as it works.

Best regards,
Bruce Zhou
Please mark the replies as answers if they help and unmark if they don't.
Bruce.Zhou  Thursday, April 02, 2009 8:48 AM
Okay, you said to handle the Showing event because the first cell gets selected after the form is Shown.
But in my case I had to write a custom control with two datagridviews in it, and some custom code for navigating between the two, with one grid selected upon form load and the other not selected.

A custom control doesn't expose the 'Shown' event, in order to handle it to make one datagridview not selected upon load, I have to go and handle it in the form in which I am using the custom control.
Is there any other similar event inside a custom control that I can handle instead to prevent a datagridview from showing a selection rectangle upon load?
reachrishikh  Thursday, April 02, 2009 4:05 PM
Hi reachrishikh,

I don't you want to create a customControl before. However, it's never late for me to know this now.

We can derive from the DataGridView, and override the OnGetFocus, OnCellMouseClick, OnCellMouseDoubleClick method, and define a bool variable to indicate if the DataGridView supports "Can't be selected" functionality.

Just for your convenience, I paste all of the code now. It can work pretty well on my side.

    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
            DataGridViewEx dgvE = new DataGridViewEx();
            this.Controls.Add(dgvE);
            dgvE.IsNotAllowSelection = true;
            dgvE.ColumnCount = 2;
            dgvE.RowCount = 5;
        }
    }
    public class DataGridViewEx : DataGridView
    {
        private bool isNotAllowSelection;

        public bool IsNotAllowSelection
        {
            get { return isNotAllowSelection; }
            set { isNotAllowSelection = value; }
        }
        protected override void OnGotFocus(EventArgs e)
        {
            base.OnGotFocus(e);
            if (isNotAllowSelection)
            {
                this.ClearSelection();
            }
        }
        protected override void OnCellMouseClick(DataGridViewCellMouseEventArgs e)
        {
            if (isNotAllowSelection)
            {
                this.ClearSelection();
            }
        }
        protected override void OnCellMouseDoubleClick(DataGridViewCellMouseEventArgs e)
        {
            if (isNotAllowSelection)
            {
                this.ClearSelection();
            }
        }
    }

Let me know if you have problem with the code.

Best regards,
Bruce Zhou
Please mark the replies as answers if they help and unmark if they don't.
Bruce.Zhou  Friday, April 03, 2009 6:05 AM
Thanks but I've already got all of that functionality working fine. I just need to know if there is an equivalent event in a Custom Control, similar to a Form's Shown event. I have two datagridviews in the control aligned side by side. When the control loads, I want one of them to have the selection rectangle, and the other one to not have it. Currently, since there is no such event in custom control classes, I am using a workaround to accomplish this. I have to put the control on my form first, and then handle the shown event of the form inside it I set the control's second datagridview to clear its selection. I would like to do that within the control itself. Putting that code within the Control's load event doesn't work, same as with a Form's load event.
reachrishikh  Friday, April 03, 2009 8:19 AM
Hi reachrishikh,

I don't know if you have gone through my code completely. To override the OnGotFocus method can have the same function as in the form's shown event.

It's very easy for you to use the extended DataGridView in my previous post--------"DataGridViewEx"..

To achieve your goal.

Create a custom control.

Drag two DataGridViewEx controls to your custom control interface. To the one you don't want to show selection Rectangle, you can set the IsNotAllowSelection property to true.

That's all.

Best regards,
Bruce Zhou
Please mark the replies as answers if they help and unmark if they don't.
Bruce.Zhou  Friday, April 03, 2009 8:26 AM
Ah! Okay, now I get it! Thanks a lot.
reachrishikh  Friday, April 03, 2009 8:51 AM
I offer an alternative solution. Add this to the form constructor code:

DataGridView1.DefaultCellStyle.SelectionForeColor = Color.Black;
DataGridView1.DefaultCellStyle.SelectionBackColor = Color.White;

This will workif theDefaultCellStyle has not been modified. If it has, just change them to the colors of the DefaultCellStyle.
  • Edited byJohnnyX147 Wednesday, August 05, 2009 4:56 PM
  •  
JohnnyX147  Wednesday, August 05, 2009 4:04 PM

You can use google to search for other answers

Custom Search

More Threads

• Save/Restore image to SSE database
• Can't hide first column in DataGridView
• Bind data source to DataGridView RowHeader value
• Help needed(visual basic 2008): extract cells from .mdb database
• Navigating using BindingContext
• Accessing Non-Public Members
• Editable datagrid
• How to add foreign key automatically
• show Infos from related tables in Datagrid
• Newer code sample for sorting bound data in columns?