From: jomree on 7 Apr 2010 08:40 Thanks for the trick! colbert wrote: From my test, the problem regarding to the ENTER key only exists when the 07-Jan-09 From my test, the problem regarding to the ENTER key only exists when the current edit row is the last row in the DataGridView. So my though is to register the ComboBox's PreviewKeyDown event. When this event catch the ENTER key pressing, we add a new row to the DataGridView, this will fix the ENTER key issue. The last thing we need to make it work is removing the new added row. void Form1_PreviewKeyDown(object sender, PreviewKeyDownEventArgs e) { if (e.KeyCode == Keys.Enter) { this.dataGridView1.Rows.RemoveAt(this.dataGridView1.Rows.Add()); } } We have to register the ComboBox's event in the EditingControlShowing event handle because we can not access the instance of the ComboBox in the other places. And before we register the PreviewKeyDown event, we firstly remove the last registered this event. So the handle function will not get called twice. void dataGridView1_EditingControlShowing(object sender, DataGridViewEditingControlShowingEventArgs e) { try { if (e.Control.GetType() == typeof(DataGridViewComboBoxEditingControl)) { ((ComboBox)e.Control).DropDownStyle = ComboBoxStyle.DropDown; ((ComboBox)e.Control).PreviewKeyDown -= new PreviewKeyDownEventHandler(Form1_PreviewKeyDown); ((ComboBox)e.Control).PreviewKeyDown += new PreviewKeyDownEventHandler(Form1_PreviewKeyDown); } } catch (Exception ex) { Debug.Print(ex.Message); } } I did a lot investigation to find this tricky solution. And it will not be well document and supported. It just works fine from my test. So, I suggest you also do some tests in you side and then make the decision whether to use it to avoid the ENTER key issue, or just let the customer know they need to use the TAB key or the Mouse to post back the ComboBox values. Hope this clarifies. And any questions or concerns, please feel free to update the thread. I am very pleased if I can provide any future assistance. Have a nice day! Best regards, Colbert Zhou (colbertz(a)online.microsoft.com, remove 'online.') Microsoft Online Community Support Delighting our customers is our #1 priority. We welcome your comments and suggestions about how we can improve the support we provide to you. Please feel free to let my manager know what you think of the level of service provided. You can send feedback directly to my manager at: msdnmg(a)microsoft.com. Previous Posts In This Thread: On Monday, January 05, 2009 11:43 PM anbaesiva wrote: Editable DataGridViewComboBoxCell In my application there is a need for editable DataGridViewComboBoxColumn. In normal DataGridViewComboBoxCell, user can't enter his custom value, only he can select a value in the drop-down box. In my application, the user also need to enter his own value. I got the following sample from msdn forum for this requirement. <sample code> private void dataGridView1_CellValidating(object sender, DataGridViewCellValidatingEventArgs e) { if (dataGridView1.CurrentCell.IsInEditMode) { if (dataGridView1.CurrentCell.GetType() == typeof(DataGridViewComboBoxCell)) { DataGridViewComboBoxCell cell = (DataGridViewComboBoxCell)dataGridView1.Rows[e.RowIndex].Cells[e.ColumnIndex]; if (!cell.Items.Contains(e.FormattedValue)) { cell.Items.Add(e.FormattedValue); cell.Value = e.FormattedValue; //keep the new value in the member variable newCellValue newCellValue = e.FormattedValue; } } } } private void dataGridView1_EditingControlShowing(object sender, DataGridViewEditingControlShowingEventArgs e) { if (e.Control.GetType() == typeof(DataGridViewComboBoxEditingControl)) { ((ComboBox)e.Control).DropDownStyle = ComboBoxStyle.DropDown; } } private void dataGridView1_CellValidated(object sender, DataGridViewCellEventArgs e) { if (dataGridView1.CurrentCell.GetType() == typeof(DataGridViewComboBoxCell)) { DataGridViewCell cell = dataGridView1.Rows[e.RowIndex].Cells[e.ColumnIndex]; cell.Value = newCellValue; } } <sample code> Using the above sample code, I can enter my own value. But the entered value is displayed only combox-box is not clicked(when dropdown box is not shown). If the combobox in the cell is clicked, the entered value is not displayed. Please provide me the solution for my requirement. Thanks. On Tuesday, January 06, 2009 3:43 AM colbert wrote: Hello Anbaesivam, Do you mean the text you input disappears after clicking the Hello Anbaesivam, Do you mean the text you input disappears after clicking the drop down list of the combobox? I have used the exactly same codes as yours, but they work fine in my side. The text I input always appears no matter whether the dropdown list is visible, if I press Tab to post them back. My guess is that, in your side, you may not post back the inputted text. Therefore they are lost. Based on my test, after entering text in such kind of DataGridViewComboBoxCell, we need to press the Tab key to make the cell value to be invalidated. And then the Invalidating and Invalidated event will fire. Pressing Enter key will not cause Invalidating and Invalidated event fire. So, our codes adding combobox item and setting cell value does not get executed indeed. Please give it a try in your side and let me know if pressing Tab key will resolve the issue. Any future questions or concerns, please feel free to let me know! Have a good day! Best regards, Colbert Zhou (colbertz @online.microsoft.com, remove 'online.') Microsoft Online Community Support Delighting our customers is our #1 priority. We welcome your comments and suggestions about how we can improve the support we provide to you. Please feel free to let my manager know what you think of the level of service provided. You can send feedback directly to my manager at: msdnmg(a)microsoft.com. ================================================== Get notification to my posts through email? Please refer to http://msdn.microsoft.com/en-us/subscriptions/aa948868.aspx#notifications. Note: The MSDN Managed Newsgroup support offering is for non-urgent issues where an initial response from the community or a Microsoft Support Engineer within 1 business day is acceptable. Please note that each follow up response may take approximately 2 business days as the support professional working with you may need further investigation to reach the most efficient resolution. The offering is not appropriate for situations that require urgent, real-time or phone-based interactions or complex project analysis and dump analysis issues. Issues of this nature are best handled working with a dedicated Microsoft Support Engineer by contacting Microsoft Customer Support Services (CSS) at http://support.microsoft.com/select/default.aspx?target=assistance&ln=en-us. ================================================== This posting is provided "AS IS" with no warranties, and confers no rights. On Tuesday, January 06, 2009 3:56 AM anbaesiva wrote: When I press TAB key it works fine. When I press TAB key it works fine. Please let me know what are changes needed to make this code work even when the user press ENTER key. Thanks. "Colbert Zhou [MSFT]" wrote: On Tuesday, January 06, 2009 6:07 AM colbert wrote: Hello Anbaesivam, Try to use the following version of Hello Anbaesivam, Try to use the following version of dataGridView1_EditingControlShowing, void dataGridView1_EditingControlShowing(object sender, DataGridViewEditingControlShowingEventArgs e) { try { if (e.Control.GetType() == typeof(DataGridViewComboBoxEditingControl)) { ((ComboBox)e.Control).DropDownStyle = ComboBoxStyle.DropDown; ((ComboBox)e.Control).PreviewKeyDown -= new PreviewKeyDownEventHandler(Form1_PreviewKeyDown); ((ComboBox)e.Control).PreviewKeyDown += new PreviewKeyDownEventHandler(Form1_PreviewKeyDown); } } catch (Exception ex) { Debug.Print(ex.Message); } } And add the following event handle void Form1_PreviewKeyDown(object sender, PreviewKeyDownEventArgs e) { if (e.KeyCode == Keys.Enter) { this.dataGridView1.Rows.RemoveAt(this.dataGridView1.Rows.Add()); } } This is just a tricky solution. Hope this helps! Best regards, Colbert Zhou (colbertz @online.microsoft.com, remove 'online.') Microsoft Online Community Support Delighting our customers is our #1 priority. We welcome your comments and suggestions about how we can improve the support we provide to you. Please feel free to let my manager know what you think of the level of service provided. You can send feedback directly to my manager at: msdnmg(a)microsoft.com. On Tuesday, January 06, 2009 7:08 AM anbaesiva wrote: Thanks for your solution. Can you please explain me the code? Thanks for your solution. Can you please explain me the code?, i need to do some customization. "Colbert Zhou [MSFT]" wrote: On Wednesday, January 07, 2009 12:00 AM colbert wrote: From my test, the problem regarding to the ENTER key only exists when the From my test, the problem regarding to the ENTER key only exists when the current edit row is the last row in the DataGridView. So my though is to register the ComboBox's PreviewKeyDown event. When this event catch the ENTER key pressing, we add a new row to the DataGridView, this will fix the ENTER key issue. The last thing we need to make it work is removing the new added row. void Form1_PreviewKeyDown(object sender, PreviewKeyDownEventArgs e) { if (e.KeyCode == Keys.Enter) { this.dataGridView1.Rows.RemoveAt(this.dataGridView1.Rows.Add()); } } We have to register the ComboBox's event in the EditingControlShowing event handle because we can not access the instance of the ComboBox in the other places. And before we register the PreviewKeyDown event, we firstly remove the last registered this event. So the handle function will not get called twice. void dataGridView1_EditingControlShowing(object sender, DataGridViewEditingControlShowingEventArgs e) { try { if (e.Control.GetType() == typeof(DataGridViewComboBoxEditingControl)) { ((ComboBox)e.Control).DropDownStyle = ComboBoxStyle.DropDown; ((ComboBox)e.Control).PreviewKeyDown -= new PreviewKeyDownEventHandler(Form1_PreviewKeyDown); ((ComboBox)e.Control).PreviewKeyDown += new PreviewKeyDownEventHandler(Form1_PreviewKeyDown); } } catch (Exception ex) { Debug.Print(ex.Message); } } I did a lot investigation to find this tricky solution. And it will not be well document and supported. It just works fine from my test. So, I suggest you also do some tests in you side and then make the decision whether to use it to avoid the ENTER key issue, or just let the customer know they need to use the TAB key or the Mouse to post back the ComboBox values. Hope this clarifies. And any questions or concerns, please feel free to update the thread. I am very pleased if I can provide any future assistance. Have a nice day! Best regards, Colbert Zhou (colbertz(a)online.microsoft.com, remove 'online.') Microsoft Online Community Support Delighting our customers is our #1 priority. We welcome your comments and suggestions about how we can improve the support we provide to you. Please feel free to let my manager know what you think of the level of service provided. You can send feedback directly to my manager at: msdnmg(a)microsoft.com. Submitted via EggHeadCafe - Software Developer Portal of Choice WPF Reflection Effect http://www.eggheadcafe.com/tutorials/aspnet/8cc84aa8-3b44-4037-beab-49bd76e20b9b/wpf-reflection-effect.aspx
From: Nick Gilbert on 21 Apr 2010 08:20 I don't know who you were thanking, but as you changed the subject line, it's very unlikely they'll see your message. You shouldn't alter subject lines in newsgroup threads as it effectively creates a whole new thread in many news reader clients. Nick On 07/04/2010 13:40, jomree wrote: > Thanks for the trick!
|
Pages: 1 Prev: Encryption algorithms for product licensing Next: Reflection on existing variable |