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 / February 2007

Tip: Looking for answers? Try searching our database.

How to make a DataGridViewComboBox column more reactive?

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Massimo - 25 Jan 2007 20:09 GMT
I'm developing an application where there's a DataGridView and one of its
columns is of DataGridViewComboBox type. The combo box behaves as it should,
but I find it's a little too unfriendly to users, because of its very
lazy-looking behaviour.

I, as an user of the application, would like to click on the combo box, have
its pull-down menu... well, pull down :-), select one item and watch the
application reacting to my input.

What actually happens is the following:

- I click on the combo box, but nothing happens if the grid wasn't already
the active control: this first click only moves the focus to it and/or
selects the row (I'm using the FullRowSelect selection mode here).
- If I click again on the combo box, or if the grid already had the focus,
no pull-down menu appears anyway, because this click only activates the cell
in the grid, without doing anything else.
- I then click *again*, quite disappointed, and now the control realizes
that someone is really yelling at it, so finally it wakes up and the menu
appears.
- I select an item, the menu disappears, but my event processing, which is
linked to the CellValueChanged event of the grid, doesn't start yet. I
suppose this is because at this point the cell is still in editing mode, so
its value actually didn't change.
- I then click anywhere on the grid, and now, at last, the cell becomes
inactive, its value changes, the event triggers and the application starts
doing its job.

I find this behaviour really annoying, and would like a way to make my
application's look&feel better.

In order to solve the last problem, I think I can link my processing to some
other event... but which one?

I really don't have any clue about removing the need for the first two
clicks, though :-(

Any help would be really appreciated.

Thanks

Massimo
Tim Van Wassenhove - 25 Jan 2007 20:40 GMT
Massimo schreef:
> I, as an user of the application, would like to click on the combo box,
> have its pull-down menu... well, pull down :-), select one item and
> watch the application reacting to my input.

I find that the DGV works nicer if i set the EditMode property to
EditOnEnter. In order to allow the user to select a fullrow i handle the
mouseclick event as following:

private void dataGridView1_MouseClick( object sender, MouseEventArgs e ) {
 DataGridView.HitTestInfo hitInfo = this.dataGridView1.HitTest(e.X, e.Y);
 if( hitInfo.Type == DataGridViewHitTestType.RowHeader ) {
  this.dataGridView1.EditMode = DataGridViewEditMode.EditOnKeystrokeOrF2;
  this.dataGridView1.EndEdit();
 }
 else
 {
  this.dataGridView1.EditMode = DataGridViewEditMode.EditOnEnter;
 }
}

Signature

Tim Van Wassenhove <url:http://www.timvw.be/>

Massimo - 26 Jan 2007 18:01 GMT
> I find that the DGV works nicer if i set the EditMode property to
> EditOnEnter.

It really does, thank you!

> In order to allow the user to select a fullrow i handle the mouseclick
> event as following:

Why is this needed?
I didn't use any of this code, and row selection works exactly the same as
it did before changing the EditMode.

Massimo
Tim Van Wassenhove - 26 Jan 2007 20:54 GMT
Massimo schreef:
>> In order to allow the user to select a fullrow i handle the mouseclick
>> event as following:
>
> Why is this needed?
> I didn't use any of this code, and row selection works exactly the same
> as it did before changing the EditMode.

Well, last time i tried it was impossible to select a row for deletion
with EditOnEnter.. Somehow i can't reproduce this problem anymore
though.. (Let's hope it's fixed now :))

Signature

Tim Van Wassenhove <url:http://www.timvw.be/>

Massimo - 26 Jan 2007 18:03 GMT
> I find that the DGV works nicer if i set the EditMode property to
> EditOnEnter.

Addendum: any solution to the problem of having to click again after
modifying a ComboBox cell in order for the grid to fire a CellValueChanged
event?

Massimo
Tim Van Wassenhove - 26 Jan 2007 21:10 GMT
Massimo schreef:

>> I find that the DGV works nicer if i set the EditMode property to
>> EditOnEnter.
>
> Addendum: any solution to the problem of having to click again after
> modifying a ComboBox cell in order for the grid to fire a
> CellValueChanged event?

The following code seems to work:

this.dataGridView1.EditingControlShowing +=
dataGridView1_EditingControlShowing;

void dataGridView1_EditingControlShowing(object sender,
DataGridViewEditingControlShowingEventArgs e)
{
 ComboBox comboBox = e.Control as ComboBox;
 if (comboBox != null) {
  // remove previously attached delegate
  comboBox.SelectionChangeCommitted -= comboBox_SelectionChangeCommitted;

  comboBox.SelectionChangeCommitted += comboBox_SelectionChangeCommitted;
  }
 }
}

void comboBox_SelectionChangeCommitted(object sender, EventArgs e)
{
 this.dataGridView1.EndEdit();
 this.dataGridView1.CurrentCell.Value = ((ComboBox)sender).SelectedItem;
}

Signature

Tim Van Wassenhove <url:http://www.timvw.be/>

SIVAKKAMY MATHU - 09 Feb 2007 10:38 GMT
private void dataGridView1_EditingControlShowing(object sender,
                   DataGridViewEditingControlShowingEventArgs e)
{
   ComboBox cb = e.Control as ComboBox;
   if (cb != null)
   {
       // first remove event handler to keep from attaching multiple:
       cb.SelectedIndexChanged -= new
        EventHandler(cb_SelectedIndexChanged);

       // now attach the event handler
       cb.SelectedIndexChanged += new
        EventHandler(cb_SelectedIndexChanged);
   }
}

void cb_SelectedIndexChanged(object sender, EventArgs e)
{
  // ----ADD CODES NEEDED TO ACEHIEVE THE TASK
}

EggHeadCafe.com - .NET Developer Portal of Choice
http://www.eggheadcafe.com

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.