2016-02-28 100 views
0

我想检查是否有DataGridView选择任何行。问题是当我尝试执行下面的行。我怎么能解决这个问题?如何检查在DataGridView上是否选择了任何行?

尝试这种

private void btnExcluir_Click(object sender, EventArgs e) { 
      int row = gridUsuarios.CurrentRow.Index; 
      Console.WriteLine(row); 
} 

例外

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>ControleUsuarios.vshost.exe</AppDomain><Exception><ExceptionType>System.NullReferenceException, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089</ExceptionType><Message>Object reference not set to an instance of an object.</Message><StackTrace> at ControleUsuarios.Form1.btnExcluir_Click(Object sender, EventArgs e) in c:\Users\fernando\Documents\Visual Studio 2012\Projects\ControleUsuarios\ControleUsuarios\Form1.cs:line 44 
    at System.Windows.Forms.Control.OnClick(EventArgs e) 
    at System.Windows.Forms.Button.OnClick(EventArgs e) 
    at System.Windows.Forms.Button.OnMouseUp(MouseEventArgs mevent) 
    at System.Windows.Forms.Control.WmMouseUp(Message&amp;amp; m, MouseButtons button, Int32 clicks) 
    at System.Windows.Forms.Control.WndProc(Message&amp;amp; m) 
    at System.Windows.Forms.ButtonBase.WndProc(Message&amp;amp; m) 
    at System.Windows.Forms.Button.WndProc(Message&amp;amp; m) 
    at System.Windows.Forms.Control.ControlNativeWindow.OnMessage(Message&amp;amp; m) 
    at System.Windows.Forms.Control.ControlNativeWindow.WndProc(Message&amp;amp; m) 
    at System.Windows.Forms.NativeWindow.DebuggableCallback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam) 
    at System.Windows.Forms.UnsafeNativeMethods.DispatchMessageW(MSG&amp;amp; msg) 
    at System.Windows.Forms.Application.ComponentManager.System.Windows.Forms.UnsafeNativeMethods.IMsoComponentManager.FPushMessageLoop(IntPtr 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) 
    at ControleUsuarios.Program.Main() in c:\Users\fernando\Documents\Visual Studio 2012\Projects\ControleUsuarios\ControleUsuarios\Program.cs:line 16 
    at System.AppDomain._nExecuteAssembly(RuntimeAssembly assembly, String[] args) 
    at System.AppDomain.ExecuteAssembly(String assemblyFile, Evidence assemblySecurity, String[] args) 
    at Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly() 
    at System.Threading.ThreadHelper.ThreadStart_Context(Object state) 
    at System.Threading.ExecutionContext.RunInternal(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx) 
    at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx) 
    at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state) 
    at System.Threading.ThreadHelper.ThreadStart()</StackTrace><ExceptionString>System.NullReferenceException: Object reference not set to an instance of an object. 
    at ControleUsuarios.Form1.btnExcluir_Click(Object sender, EventArgs e) in c:\Users\fernando\Documents\Visual Studio 2012\Projects\ControleUsuarios\ControleUsuarios\Form1.cs:line 44 
    at System.Windows.Forms.Control.OnClick(EventArgs e) 
    at System.Windows.Forms.Button.OnClick(EventArgs e) 
    at System.Windows.Forms.Button.OnMouseUp(MouseEventArgs mevent) 
    at System.Windows.Forms.Control.WmMouseUp(Message&amp;amp; m, MouseButtons button, Int32 clicks) 
    at System.Windows.Forms.Control.WndProc(Message&amp;amp; m) 
    at System.Windows.Forms.ButtonBase.WndProc(Message&amp;amp; m) 
    at System.Windows.Forms.Button.WndProc(Message&amp;amp; m) 
    at System.Windows.Forms.Control.ControlNativeWindow.OnMessage(Message&amp;amp; m) 
    at System.Windows.Forms.Control.ControlNativeWindow.WndProc(Message&amp;amp; m) 
    at System.Windows.Forms.NativeWindow.DebuggableCallback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam) 
    at System.Windows.Forms.UnsafeNativeMethods.DispatchMessageW(MSG&amp;amp; msg) 
    at System.Windows.Forms.Application.ComponentManager.System.Windows.Forms.UnsafeNativeMethods.IMsoComponentManager.FPushMessageLoop(IntPtr 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) 
    at ControleUsuarios.Program.Main() in c:\Users\fernando\Documents\Visual Studio 2012\Projects\ControleUsuarios\ControleUsuarios\Program.cs:line 16 
    at System.AppDomain._nExecuteAssembly(RuntimeAssembly assembly, String[] args) 
    at System.AppDomain.ExecuteAssembly(String assemblyFile, Evidence assemblySecurity, String[] args) 
    at Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly() 
    at System.Threading.ThreadHelper.ThreadStart_Context(Object state) 
    at System.Threading.ExecutionContext.RunInternal(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx) 
    at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx) 
    at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state) 
    at System.Threading.ThreadHelper.ThreadStart()</ExceptionString></Exception></TraceRecord> 
The program '[4584] ControleUsuarios.vshost.exe: Program Trace' has exited with code 0 (0x0). 
The program '[4584] ControleUsuarios.vshost.exe: Managed (v4.0.30319)' has exited with code -1 (0xffffffff). 
+1

您还没有选择任何行,所以CurrentRow属性为null。添加一个空检查。 – kennyzx

回答

1

您可以验证使用SelectedRows财产。

if(gridUsuarios.SelectedRows != null && gridUsuarios.SelectedRows.Count > 0) 
{ 
    // logic. 
} 
+0

完美。谢谢 ! – FernandoPaiva

+1

我很高兴它有帮助。 –

0

@HariPrasad的答案是正确的,但效率低下。即使您只想知道是否选择了至少一个并且行取消共享,它也会使用Countsee)。如果你的子类DataGridView(我会建议为每个组件,都在项目中使用),可以使该属性:

[Browsable(false)] 
public bool SelectedOneOrMore 
{ 
    get 
    { 
     return Rows.GetFirstRow(DataGridViewElementStates.Selected) != -1; 
    } 
} 
相关问题