Dave

Dave

  • NA
  • 161
  • 0

Change CurrentRow in DataGridView

Jul 9 2008 3:05 AM
I have never really successfully been able to programmatically change the current row in a DataGridView. Let's take a look at this:

DataGridView aView = Application.OpenForms["frmMain"].Controls["dgvPlaylist"] as DataGridView;            
aView.Rows[aView.CurrentRow.Index + 1].Selected = true;


One would think that this would do the trick, especially in light of the fact that I have the property SelectionMode set to FullRowSelect. However, that is a trap for young players. All that does is make it appear on the screen that the row is selected (that is, it is highlighted as being selected on the screen). However, further inspection reveals that the CurrentRow is still the previous row.

The MSDN documentation states that you must change the CurrentCell in order to change the CurrentRow (this property is both get and set). So I try that:

int i = aView.CurrentRow.Index + 1;
aView.CurrentCell = aView.Rows[i].Cells[0];


This throws an exception deep inside the framework. The stack trace is as follows:

'audioPissabout.vshost.exe' (Managed): Loaded 'C:\WINDOWS\assembly\GAC_32\mscorlib\2.0.0.0__b77a5c561934e089\mscorlib.dll', No symbols loaded.
'audioPissabout.vshost.exe' (Managed): Loaded 'C:\WINDOWS\assembly\GAC_MSIL\Microsoft.VisualStudio.HostingProcess.Utilities\8.0.0.0__b03f5f7f11d50a3a\Microsoft.VisualStudio.HostingProcess.Utilities.dll', No symbols loaded.
'audioPissabout.vshost.exe' (Managed): Loaded 'C:\WINDOWS\assembly\GAC_MSIL\System.Windows.Forms\2.0.0.0__b77a5c561934e089\System.Windows.Forms.dll', No symbols loaded.
'audioPissabout.vshost.exe' (Managed): Loaded 'C:\WINDOWS\assembly\GAC_MSIL\System\2.0.0.0__b77a5c561934e089\System.dll', No symbols loaded.
'audioPissabout.vshost.exe' (Managed): Loaded 'C:\WINDOWS\assembly\GAC_MSIL\System.Drawing\2.0.0.0__b03f5f7f11d50a3a\System.Drawing.dll', No symbols loaded.
'audioPissabout.vshost.exe' (Managed): Loaded 'C:\WINDOWS\assembly\GAC_MSIL\Microsoft.VisualStudio.HostingProcess.Utilities.Sync\8.0.0.0__b03f5f7f11d50a3a\Microsoft.VisualStudio.HostingProcess.Utilities.Sync.dll', No symbols loaded.
'audioPissabout.vshost.exe' (Managed): Loaded 'D:\documents\Visual Studio Projects\cSharp\audio\audioPissabout_C\audioPissabout\bin\Debug\audioPissabout.vshost.exe', No symbols loaded.
'audioPissabout.vshost.exe' (Managed): Loaded 'C:\WINDOWS\assembly\GAC_MSIL\Microsoft.VisualBasic\8.0.0.0__b03f5f7f11d50a3a\Microsoft.VisualBasic.dll', No symbols loaded.
'audioPissabout.vshost.exe' (Managed): Loaded 'C:\WINDOWS\assembly\GAC_MSIL\Microsoft.VisualBasic.Compatibility\8.0.0.0__b03f5f7f11d50a3a\Microsoft.VisualBasic.Compatibility.dll', No symbols loaded.
'audioPissabout.vshost.exe' (Managed): Loaded 'C:\WINDOWS\assembly\GAC\stdole\7.0.3300.0__b03f5f7f11d50a3a\stdole.dll', No symbols loaded.
'audioPissabout.vshost.exe' (Managed): Loaded 'C:\WINDOWS\assembly\GAC_32\System.Data\2.0.0.0__b77a5c561934e089\System.Data.dll', No symbols loaded.
'audioPissabout.vshost.exe' (Managed): Loaded 'C:\WINDOWS\assembly\GAC_MSIL\System.Deployment\2.0.0.0__b03f5f7f11d50a3a\System.Deployment.dll', No symbols loaded.
'audioPissabout.vshost.exe' (Managed): Loaded 'C:\WINDOWS\assembly\GAC_MSIL\System.Design\2.0.0.0__b03f5f7f11d50a3a\System.Design.dll', No symbols loaded.
'audioPissabout.vshost.exe' (Managed): Loaded 'C:\WINDOWS\assembly\GAC_32\System.Web\2.0.0.0__b03f5f7f11d50a3a\System.Web.dll', No symbols loaded.
'audioPissabout.vshost.exe' (Managed): Loaded 'C:\WINDOWS\assembly\GAC_MSIL\System.Xml\2.0.0.0__b77a5c561934e089\System.Xml.dll', No symbols loaded.
The thread 0x1440 has exited with code 0 (0x0).
The thread 0x1420 has exited with code 0 (0x0).
'audioPissabout.vshost.exe' (Managed): Loaded 'D:\documents\Visual Studio Projects\cSharp\audio\audioPissabout_C\audioPissabout\bin\Debug\audioPissabout.exe', Symbols loaded.
'audioPissabout.vshost.exe' (Managed): Loaded 'D:\documents\Visual Studio Projects\cSharp\audio\audioPissabout_C\audioPissabout\bin\Debug\Bass.Net.dll', No symbols loaded.
'audioPissabout.vshost.exe' (Managed): Loaded 'C:\WINDOWS\assembly\GAC_32\System.Data.SQLite\1.0.46.0__db937bc2d44ff139\System.Data.SQLite.dll', No symbols loaded.
'audioPissabout.vshost.exe' (Managed): Loaded 'C:\WINDOWS\assembly\GAC_32\System.Transactions\2.0.0.0__b77a5c561934e089\System.Transactions.dll', No symbols loaded.
'audioPissabout.vshost.exe' (Managed): Loaded 'C:\WINDOWS\assembly\GAC_MSIL\System.Configuration\2.0.0.0__b03f5f7f11d50a3a\System.Configuration.dll', No symbols loaded.
'audioPissabout.vshost.exe' (Managed): Loaded 'C:\WINDOWS\assembly\GAC_32\System.EnterpriseServices\2.0.0.0__b03f5f7f11d50a3a\System.EnterpriseServices.dll', No symbols loaded.
A first chance exception of type 'System.InvalidOperationException' occurred in System.Windows.Forms.dll
A first chance exception of type 'System.Reflection.TargetInvocationException' occurred in mscorlib.dll
System.Transactions Critical: 0 : <TraceRecord xmlns="http://schemas.microsoft.com/2004/10/E2ETraceEvent/TraceRecord" Severity="Critical"><TraceIdentifier>http://msdn.microsoft.com/TraceCodes/System/ActivityTracing/2004/07/Reliability/Exception/Unhandled</TraceIdentifier><Description>Unhandled exception</Description><AppDomain>audioPissabout.vshost.exe</AppDomain><Exception><ExceptionType>System.Reflection.TargetInvocationException, mscorlib, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089</ExceptionType><Message>Exception has been thrown by the target of an invocation.</Message><StackTrace>   at System.RuntimeMethodHandle._InvokeMethodFast(Object target, Object[] arguments, SignatureStruct&amp;amp; sig, MethodAttributes methodAttributes, RuntimeTypeHandle typeOwner)
   at System.RuntimeMethodHandle.InvokeMethodFast(Object target, Object[] arguments, Signature sig, MethodAttributes methodAttributes, RuntimeTypeHandle typeOwner)
   at System.Reflection.RuntimeMethodInfo.Invoke(Object obj, BindingFlags invokeAttr, Binder binder, Object[] parameters, CultureInfo culture, Boolean skipVisibilityChecks)
   at System.Delegate.DynamicInvokeImpl(Object[] args)
   at System.Delegate.DynamicInvoke(Object[] args)
   at Un4seen.Bass.BASSTimer.InvokeDelegate(Delegate del, Object[] args)
   at Un4seen.Bass.BASSTimer.ProcessDelegate(Delegate del, Object[] args)
   at Un4seen.Bass.BASSTimer.timer_Tick(Object state)
   at System.Threading._TimerCallback.TimerCallback_Context(Object state)
   at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)
   at System.Threading._TimerCallback.PerformTimerCallback(Object state)</StackTrace><ExceptionString>System.Reflection.TargetInvocationException: Exception has been thrown by the target of an invocation. ---&amp;gt; System.InvalidOperationException: Cross-thread operation not valid: Control 'dgvPlaylist' accessed from a thread other than the thread it was created on.
   at System.Windows.Forms.Control.get_Handle()
   at System.Windows.Forms.Control.get_Focused()
   at System.Windows.Forms.DataGridView.SetCurrentCellAddressCore(Int32 columnIndex, Int32 rowIndex, Boolean setAnchorCellAddress, Boolean validateCurrentCell, Boolean throughMouseClick)
   at System.Windows.Forms.DataGridView.set_CurrentCell(DataGridViewCell value)
   at audioPissabout.Audio.PlayNext(Object Source, SongsEventArgs e) in D:\documents\Visual Studio Projects\cSharp\audio\audioPissabout_C\audioPissabout\Audio.cs:line 134
   at audioPissabout.Audio.OnNextSong(SongsEventArgs e) in D:\documents\Visual Studio Projects\cSharp\audio\audioPissabout_C\audioPissabout\Audio.cs:line 121
   at audioPissabout.Audio.timerUpdate_Tick(Object sender, EventArgs e) in D:\documents\Visual Studio Projects\cSharp\audio\audioPissabout_C\audioPissabout\Audio.cs:line 109
   --- End of inner exception stack trace ---
   at System.RuntimeMethodHandle._InvokeMethodFast(Object target, Object[] arguments, SignatureStruct&amp;amp; sig, MethodAttributes methodAttributes, RuntimeTypeHandle typeOwner)
   at System.RuntimeMethodHandle.InvokeMethodFast(Object target, Object[] arguments, Signature sig, MethodAttributes methodAttributes, RuntimeTypeHandle typeOwner)
   at System.Reflection.RuntimeMethodInfo.Invoke(Object obj, BindingFlags invokeAttr, Binder binder, Object[] parameters, CultureInfo culture, Boolean skipVisibilityChecks)
   at System.Delegate.DynamicInvokeImpl(Object[] args)
   at System.Delegate.DynamicInvoke(Object[] args)
   at Un4seen.Bass.BASSTimer.InvokeDelegate(Delegate del, Object[] args)
   at Un4seen.Bass.BASSTimer.ProcessDelegate(Delegate del, Object[] args)
   at Un4seen.Bass.BASSTimer.timer_Tick(Object state)
   at System.Threading._TimerCallback.TimerCallback_Context(Object state)
   at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)
   at System.Threading._TimerCallback.PerformTimerCallback(Object state)</ExceptionString><InnerException><ExceptionType>System.InvalidOperationException, mscorlib, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089</ExceptionType><Message>Cross-thread operation not valid: Control 'dgvPlaylist' accessed from a thread other than the thread it was created on.</Message><StackTrace>   at System.Windows.Forms.Control.get_Handle()
   at System.Windows.Forms.Control.get_Focused()
   at System.Windows.Forms.DataGridView.SetCurrentCellAddressCore(Int32 columnIndex, Int32 rowIndex, Boolean setAnchorCellAddress, Boolean validateCurrentCell, Boolean throughMouseClick)
   at System.Windows.Forms.DataGridView.set_CurrentCell(DataGridViewCell value)
   at audioPissabout.Audio.PlayNext(Object Source, SongsEventArgs e) in D:\documents\Visual Studio Projects\cSharp\audio\audioPissabout_C\audioPissabout\Audio.cs:line 134
   at audioPissabout.Audio.OnNextSong(SongsEventArgs e) in D:\documents\Visual Studio Projects\cSharp\audio\audioPissabout_C\audioPissabout\Audio.cs:line 121
   at audioPissabout.Audio.timerUpdate_Tick(Object sender, EventArgs e) in D:\documents\Visual Studio Projects\cSharp\audio\audioPissabout_C\audioPissabout\Audio.cs:line 109</StackTrace><ExceptionString>System.InvalidOperationException: Cross-thread operation not valid: Control 'dgvPlaylist' accessed from a thread other than the thread it was created on.
   at System.Windows.Forms.Control.get_Handle()
   at System.Windows.Forms.Control.get_Focused()
   at System.Windows.Forms.DataGridView.SetCurrentCellAddressCore(Int32 columnIndex, Int32 rowIndex, Boolean setAnchorCellAddress, Boolean validateCurrentCell, Boolean throughMouseClick)
   at System.Windows.Forms.DataGridView.set_CurrentCell(DataGridViewCell value)
   at audioPissabout.Audio.PlayNext(Object Source, SongsEventArgs e) in D:\documents\Visual Studio Projects\cSharp\audio\audioPissabout_C\audioPissabout\Audio.cs:line 134
   at audioPissabout.Audio.OnNextSong(SongsEventArgs e) in D:\documents\Visual Studio Projects\cSharp\audio\audioPissabout_C\audioPissabout\Audio.cs:line 121
   at audioPissabout.Audio.timerUpdate_Tick(Object sender, EventArgs e) in D:\documents\Visual Studio Projects\cSharp\audio\audioPissabout_C\audioPissabout\Audio.cs:line 109</ExceptionString></InnerException></Exception></TraceRecord>
An unhandled exception of type 'System.Reflection.TargetInvocationException' occurred in mscorlib.dll

Additional information: Exception has been thrown by the target of an invocation.

The program '[3680] audioPissabout.vshost.exe: Managed' has exited with code 0 (0x0).


The weird thing is, it lets you do some operations using that notation without throwing an exception:

MessageBox.Show(aView.Rows[i].Cells[0].Value.ToString());
aView.Rows[i].Cells[0].Value = “something else”;


So, if anyone with heaps of .NET knowledge wants to have a crack at this problem, I'd love to hear from them. This is definitely a challenging issue. One that I have plugged away at several times and never been able to resolve.

Thanks


Answers (4)