Windows Develop Bookmark and Share   
 index > Windows Forms Data Controls and Databinding > DataGridView .net 2.0 Last Visible Cell Tab Problem
 

DataGridView .net 2.0 Last Visible Cell Tab Problem

I have a datagridview control and last visible column is combobox column. When ever i tab the last cell of row, the next row first visible cell is selected. When i press tab on last visible cell, an exception is thrown.

===========================================================================

System.InvalidOperationException: Cell is not in a DataGridView. The cell cannot retrieve the inherited cell style.

at System.Windows.Forms.DataGridViewCell.GetInheritedStyle(DataGridViewCellStyle inheritedCellStyle, Int32 rowIndex, Boolean includeColors)

at System.Windows.Forms.DataGridView.OnCellValidating(DataGridViewCell& dataGridViewCell, Int32 columnIndex, Int32 rowIndex, DataGridViewDataErrorContexts context)

at System.Windows.Forms.DataGridView.CommitEdit(DataGridViewCell& dataGridViewCurrentCell, DataGridViewDataErrorContexts context, DataGridViewValidateCellInternal validateCell, Boolean fireCellLeave, Boolean fireCellEnter, Boolean fireRowLeave, Boolean fireRowEnter, Boolean fireLeave)

at System.Windows.Forms.DataGridView.EndEdit(DataGridViewDataErrorContexts context, DataGridViewValidateCellInternal validateCell, Boolean fireCellLeave, Boolean fireCellEnter, Boolean fireRowLeave, Boolean fireRowEnter, Boolean fireLeave, Boolean keepFocus, Boolean resetCurrentCell, Boolean resetAnchorCell)

at System.Windows.Forms.DataGridView.ProcessDialogKey(Keys keyData)

at System.Windows.Forms.Control.ProcessDialogKey(Keys keyData)

at System.Windows.Forms.Control.ProcessDialogKey(Keys keyData)

at System.Windows.Forms.Control.PreProcessMessage(Message& msg)

at System.Windows.Forms.Control.PreProcessControlMessageInternal(Control target, Message& msg)

at System.Windows.Forms.Application.ThreadContext.PreTranslateMessage(MSG& msg)

at System.Windows.Forms.Application.ThreadContext.System.Windows.Forms.UnsafeNativeMethods.IMsoComponent.FPreTranslateMessage(MSG& msg)

at System.Windows.Forms.Application.ComponentManager.System.Windows.Forms.UnsafeNativeMethods.IMsoComponentManager.FPushMessageLoop(Int32 dwComponentID, Int32 reason, Int32 pvLoopData)

at System.Windows.Forms.Application.ThreadContext.RunMessageLoopInner(Int32 reason, ApplicationContext context)

at System.Windows.Forms.Application.ThreadContext.RunMessageLoop(Int32 reason, ApplicationContext context)

at System.Windows.Forms.Application.Run(Form mainForm)

======================================================================

I have written the following code on DataGridView Leave Event:

this.dataGridViewRM.EndEdit();
this.BindingContext[this.dataGridViewRM].EndCurrentEdit();
this.dataTableClientTier.AcceptChanges();


Accept Changes is causing this exception, but till now i havent found the reason and workaround.

Can any one help me out?
  • Edited bysmohsin83 Thursday, July 30, 2009 5:19 AM
  •  
smohsin83  Thursday, July 30, 2009 5:14 AM

Hi smohsin83,

This is the code snippet you provided:
this.dataGridViewRM.EndEdit();
this.BindingContext[this.dataGridViewRM].EndCurrentEdit();
this.dataTableClientTier.AcceptChanges();

From my experience, the second line needs to be changed. The parameter in [] ought to be the data source of the DataGridView, not the DataGridView itself. The code ought to be:
this.BindingContext[this.dataGridViewRM.DataSource].EndCurrentEdit();

I have tested your code in a similar environment, but I did not meet the error. Could you provide more code snippets so that I can reproduce the error?

Let me know if this helps.
Aland Li


Please mark the replies as answers if they help and unmark if they don't. This can be beneficial to other community members reading the thread.
Aland Li  Friday, July 31, 2009 7:36 AM

Ok Take this scenario

I have two column in a datatable which is bound to a datagridview. Binding Source is used.

private void Form1_Load(object sender, EventArgs e)
{
DataRow row = dataTable1.NewRow();
row["Value"] = 122;
row["Tier"] = "tier1";
dataTable1.Rows.Add(row);

row = dataTable1.NewRow();

row["Value"] = 123;
row["Tier"] = "tier2";
dataTable1.Rows.Add(row);
}


private void dataGridView1_Leave(object sender, EventArgs e)
{
dataTable1.AcceptChanges();
}

Just change the Editmode property of the datagridview to editonenter and press tab standing on the last row last column.

smohsin83  Monday, August 03, 2009 5:05 AM

Hi smohsin83,

I have tested the code and met the same problem. From my experience, it is not very good to call DataTable.AcceptChanges in the Leave event handler of the DataGridView. The AcceptChanges method calling would affect the data source of the DataGridView, which would further affect the cells of the DataGridView. But in the Leave event handler, the current cell is in edit mode and this would lead some unexpected errors.

When we call the DataTable.AcceptChanges method, we often want to save the, so I suggest calling that method just before we save the data.

Let me know if this helps.
Aland Li


Please mark the replies as answers if they help and unmark if they don't. This can be beneficial to other community members reading the thread.
Aland Li  Monday, August 03, 2009 7:16 AM
Accept Changes is called because im using a dropdown column. if changes are not accepted, drop down starts doing some funny kind of things. Another strange thing is that if i use a button column as last column than the exception is not thrown
smohsin83  Monday, August 03, 2009 9:49 AM

You can use google to search for other answers

Custom Search

More Threads

• DataGridView - Checkboxes
• Getting the dataGridName.CurrentRowIndex in click events
• datagrid
• Error when deleting a row from a gridview
• CHANGE VALUE IN DATAGRIDCOMBOBOXCOLUMN
• DataGridView column header
• how to autogenerate date
• I have some Problem ProcessCmdKey?
• Refresh datagrid content
• BindingNavigator