如何检查DataGridView上是否选择了任何行?

时间:2016-02-28 05:21:16

标签: c# datagridview

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





尝试这个




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




例外




 <代码> System.Transactions Critical:0:&lt; TraceRecord xmlns =“http://schemas.microsoft.com/2004/10/E2ETraceEvent/TraceRecord”Severity =“Critical”&gt;&lt; TraceIdentifier&gt; http://msdn.microsoft。 com / TraceCodes / System / ActivityTracing / 2004/07 / Reliability / Exception / Unhandled&lt; / TraceIdentifier&gt;&lt; Description&gt;未处理的异常&lt; / Description&gt;&lt; AppDomain&gt; ControleUsuarios.vshost.exe&lt; / AppDomain&gt;&lt; Exception&gt;&lt; ExceptionType&gt; System.NullReferenceException,mscorlib,Version = 4.0.0.0,Culture = neutral,PublicKeyToken = b77a5c561934e089&lt; / ExceptionType&gt;&lt; Message&gt;对象引用未设置为对象的实例。&lt; / Message&gt;&lt; StackTrace&gt; at ControleUsuarios.Form1.btnExcluir_Click(Object sender,EventArgs e)位于c:\ Users \ fernando \ Documents \ Visual Studio 2012 \ Projects \ ControleUsuarios \ ControleUsuarios \ Form1.cs:第44行&#xA;在System.Windows.Forms.Control.OnClick(EventArgs e)&#xA;在System.Windows.Forms.Button.OnClick(EventArgs e)&#xA;在System.Windows.Forms.Button.OnMouseUp(MouseEventArgs mevent)&#xA;在System.Windows.Forms.Control.WmMouseUp(消息&amp; amp; amp; m,MouseButtons按钮,Int32点击)&#xA;在System.Windows.Forms.Control.WndProc(Message&amp; amp; amp; m)&#xA;在System.Windows.Forms.ButtonBase.WndProc(Message&amp; amp; amp; m)&#xA;在System.Windows.Forms.Button.WndProc(Message&amp; amp; amp; m)&#xA;在System.Windows.Forms.Control.ControlNativeWindow.OnMessage(Message&amp; amp; amp; m)&#xA;在System.Windows.Forms.Control.ControlNativeWindow.WndProc(Message&amp; amp; amp; m)&#xA;在System.Windows.Forms.NativeWindow.DebuggableCallback(IntPtr hWnd,Int32 msg,IntPtr wparam,IntPtr lparam)&#xA;在System.Windows.Forms.UnsafeNativeMethods.DispatchMessageW(MSG&amp; amp; amp; msg)&#xA;在System.Windows.Forms.Application.ComponentManager.System.Windows.Forms.UnsafeNativeMethods.IMsoComponentManager.FPushMessageLoop(IntPtr dwComponentID,Int32 reason,Int32 pvLoopData)&#xA;在System.Windows.Forms.Application.ThreadContext.RunMessageLoopInner(Int32 reason,ApplicationContext context)&#xA;在System.Windows.Forms.Application.ThreadContext.RunMessageLoop(Int32 reason,ApplicationContext context)&#xA;在System.Windows.Forms.Application.Run(Form mainForm)&#xA;在ControleUsuarios.Program.Main()在c:\ Users \ fernando \ Documents \ Visual Studio 2012 \ Projects \ ControleUsuarios \ ControleUsuarios \ Program.cs:第16行&#xA;在System.AppDomain._nExecuteAssembly(RuntimeAssembly assembly,String [] args)&#xA;在System.AppDomain.ExecuteAssembly(String assemblyFile,Evidence assemblySecurity,String [] args)&#xA;在Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly()&#xA;在System.Threading.ThreadHelper.ThreadStart_Context(对象状态)&#xA;在System.Threading.ExecutionContext.RunInternal(ExecutionContext executionContext,ContextCallback callback,Object state,Boolean preserveSyncCtx)&#xA;在System.Threading.ExecutionContext.Run(ExecutionContext executionContext,ContextCallback callback,Object state,Boolean preserveSyncCtx)&#xA;在System.Threading.ExecutionContext.Run(ExecutionContext executionContext,ContextCallback callback,Object state)&#xA;在System.Threading.ThreadHelper.ThreadStart()&lt; / StackTrace&gt;&lt; ExceptionString&gt; System.NullReferenceException:对象引用未设置为对象的实例。&#xA; at ControleUsuarios.Form1.btnExcluir_Click(Object sender,EventArgs e)位于c:\ Users \ fernando \ Documents \ Visual Studio 2012 \ Projects \ ControleUsuarios \ ControleUsuarios \ Form1.cs:第44行&#xA;在System.Windows.Forms.Control.OnClick(EventArgs e)&#xA;在System.Windows.Forms.Button.OnClick(EventArgs e)&#xA;在System.Windows.Forms.Button.OnMouseUp(MouseEventArgs mevent)&#xA;在System.Windows.Forms.Control.WmMouseUp(消息&amp; amp; amp; m,MouseButtons按钮,Int32点击)&#xA;在System.Windows.Forms.Control.WndProc(Message&amp; amp; amp; m)&#xA;在System.Windows.Forms.ButtonBase.WndProc(Message&amp; amp; amp; m)&#xA;在System.Windows.Forms.Button.WndProc(Message&amp; amp; amp; m)&#xA;在System.Windows.Forms.Control.ControlNativeWindow.OnMessage(Message&amp; amp; amp; m)&#xA;在System.Windows.Forms.Control.ControlNativeWindow.WndProc(Message&amp; amp; amp; m)&#xA;在System.Windows.Forms.NativeWindow.DebuggableCallback(IntPtr hWnd,Int32 msg,IntPtr wparam,IntPtr lparam)&#xA;在System.Windows.Forms.UnsafeNativeMethods.DispatchMessageW(MSG&amp; amp; amp; msg)&#xA;在System.Windows.Forms.Application.ComponentManager.System.Windows.Forms.UnsafeNativeMethods.IMsoComponentManager.FPushMessageLoop(IntPtr dwComponentID,Int32 reason,Int32 pvLoopData)&#xA;在System.Windows.Forms.Application.ThreadContext.RunMessageLoopInner(Int32 reason,ApplicationContext context)&#xA;在System.Windows.Forms.Application.ThreadContext.RunMessageLoop(Int32 reason,ApplicationContext context)&#xA;在System.Windows.Forms.Application.Run(Form mainForm)&#xA;在ControleUsuarios.Program.Main()在c:\ Users \ fernando \ Documents \ Visual Studio 2012 \ Projects \ ControleUsuarios \ ControleUsuarios \ Program.cs:第16行&#xA;在System.AppDomain._nExecuteAssembly(RuntimeAssembly assembly,String [] args)&#xA;在System.AppDomain.ExecuteAssembly(String assemblyFile,Evidence assemblySecurity,String [] args)&#xA;在Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly()&#xA;在System.Threading.ThreadHelper.ThreadStart_Context(对象状态)&#xA;在System.Threading.ExecutionContext.RunInternal(ExecutionContext executionContext,ContextCallback callback,Object state,Boolean preserveSyncCtx)&#xA;在System.Threading.ExecutionContext.Run(ExecutionContext executionContext,ContextCallback callback,Object state,Boolean preserveSyncCtx)&#xA;在System.Threading.ExecutionContext.Run(ExecutionContext executionContext,ContextCallback callback,Object state)&#xA;在System.Threading.ThreadHelper.ThreadStart()&lt; / ExceptionString&gt;&lt; / Exception&gt;&lt; / TraceRecord&gt;&#xA;程序'[4584] ControleUsuarios.vshost.exe:程序跟踪'已退出,代码为0( 0x0)。&#xA;程序'[4584] ControleUsuarios.vshost.exe:Managed(v4.0.30319)'已退出,代码为-1(0xffffffff)。&#xA;  
&# XA;

2 个答案:

答案 0 :(得分:1)

您可以使用SelectedRows属性进行验证。

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

答案 1 :(得分:0)

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

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