Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / Windows Forms / WinForm General / March 2007

Tip: Looking for answers? Try searching our database.

How do you bypass cells in a "DataGridView"

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Michael Torville - 09 Mar 2007 17:13 GMT
Does anyone know if it's possible to prevent a user from entering a
particular "DataGridView" cell. I simply want to redirect them into the next
available cell but nothing I try works. Setting the "CurrentCell" property
for instance causes no end of problems. If I call it in a "CellEnter"
handler I get the error "Operation is not valid because it results in a
reentrant call to the SetCurrentCellAddressCore function" (which others on
the web have also reported). If I call it in a "CellMouseDown" handler it
works when right-clicking but not left-clicking. In other handlers it causes
a stack overflow. Does anyone know how to pull this off. Thanks.
justin creasy - 09 Mar 2007 21:07 GMT
> Does anyone know if it's possible to prevent a user from entering a
> particular "DataGridView" cell. I simply want to redirect them into the next
[quoted text clipped - 5 lines]
> works when right-clicking but not left-clicking. In other handlers it causes
> a stack overflow. Does anyone know how to pull this off. Thanks.

Michael:

I have a few thoughts. First, have you set the DataGridView's
MultiSelect, SelectionMode, or EditMode attributes or left them on
their default settings? If you could show us your code for when you
create your DataGridView and when you set it's attributes that would
help.

I agree that using the CellEnter event is a bad idea since that could
cause an infinite loop. I cannot replicate your problem with left
clicked when using the CellMouseDown event. I have tried using both
the CellMouseDown and the CellMouseClick events and they both register
for left-click and right-click. I'm hoping that once I know how you
have your attributes set I will be able to replicate your problem. If
possible please also supply your code for the CellMouseDown event.

~ Justin Creasy
  www.immergetech.com
  www.immergecomm.com
ClayB - 10 Mar 2007 00:06 GMT
Here is some code that postpones setting the current cell using a
timer in CellEnter to prevent cell 2,2 becoming current. There is a
flash when you try clicking, but the arrow keys and tab keys seem to
work better.

       private int skipRow = 2;
       private int skipCol = 2;
       private int oldRow = -2;
       private int oldColumn = -2;

       void dataGridView1_CellEnter(object sender,
DataGridViewCellEventArgs e)
       {
           if (e.RowIndex == skipRow && e.ColumnIndex == skipCol)
           {
               if (Control.MouseButtons == MouseButtons.None)
               {
                   if (oldRow == skipRow - 1 && oldColumn == skipCol)
                   {
                       oldRow = skipRow + 1;

                   }
                   else if (oldRow == skipRow && oldColumn == skipCol
- 1)
                   {
                       oldColumn = skipCol + 1;
                   }
                   else if (oldRow == skipRow + 1 && oldColumn ==
skipCol)
                   {
                       oldRow = skipRow - 1;

                   }
                   else if (oldRow == skipRow && oldColumn == skipCol
+ 1)
                   {
                       oldColumn = skipCol - 1;
                   }
               }
               Timer t = new Timer();
               t.Interval = 5;
               t.Tick += new EventHandler(t_Tick);
               t.Start();
           }
       }

       void t_Tick(object sender, EventArgs e)
       {
           Timer t = sender as Timer;
           t.Stop();
           this.dataGridView1.CurrentCell =
this.dataGridView1[oldColumn, oldRow];
           t.Tick -= new EventHandler(t_Tick);
       }

        void dataGridView1_CellLeave(object sender,
DataGridViewCellEventArgs e)
       {
            oldRow = e.RowIndex;
            oldColumn = e.ColumnIndex;
       }
==============
Clay Burch
Syncfusion, Inc.
Michael Torville - 12 Mar 2007 14:35 GMT
Thanks. I'll take a look at this but in all honesty, the timer makes me
nervous. I'm not sure why a timer is needed here but I'll reserve judgment
until I review it :) Thanks again.
Michael Torville - 12 Mar 2007 14:18 GMT
> I have a few thoughts. First, have you set the DataGridView's
> MultiSelect, SelectionMode, or EditMode attributes or left them on
> their default settings? If you could show us your code for when you
> create your DataGridView and when you set it's attributes that would
> help.

Thanks for the feedback. I've have in fact set all of the above using the VS
forms designer so it all takes place in "InitializeComponent()"

MultiSelect =  false
SelectionMode = CellSelect
EditMode = EditOnKeystrokeOrF2

> I agree that using the CellEnter event is a bad idea since that could
> cause an infinite loop.

I've taken precautions against this. The error I cited previously however
appears to be a bug not only because I've read about others who have the
same problem, but because the manager of the "DataGridView" class at MSFT
(Mark Rideout) acknowledged it's a bug under other circumstances (on the
MSFT forums site).

> I cannot replicate your problem with left
> clicked when using the CellMouseDown event. I have tried using both
> the CellMouseDown and the CellMouseClick events and they both register
> for left-click and right-click. I'm hoping that once I know how you
> have your attributes set I will be able to replicate your problem. If
> possible please also supply your code for the CellMouseDown event.

Well, I just tried this by creating a new app out-of-the-box. I added a
"DataGridView" as the only control on the form with two textbox columns. I
then created a "CellMouseDown" handler as follows:

private void m_Grid_CellMouseDown(object sender,
DataGridViewCellMouseEventArgs e)
{
   if (e.ColumnIndex == 0)
   {
       m_Grid.CurrentCell = m_Grid[1, 0];
   }
}

When the app starts, column 0 is automatically the current column. If I now
right-click column 0 then the handler is called (just once) and column 1
becomes current as expected. If I left-click column 0 however then the
handler is also called (just once) but column 0 remains current. I can
understand why this is probably occurring (left-clicking makes the cell
current *after* the handler exits) but it's incorrect behaviour IMO (since
the handler's code should override the left-mouse click). Any ideas? Thanks.

Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.