按下网格视图上没有选择的按钮

时间:2012-10-29 13:34:00

标签: c# gridview

我在win表单上有一个按钮,删除网格视图上的行。

numberSettingTable.SelectedRows[0]

问题是,当我按下没有行选择的按钮时它会发送null,在java -1中返回,它表示没有选择任何行。那么如何在c#中实现相同的目标呢?

我试过if语句

numberSettingTable.SelectedRows[0] != null

但它没有用。

以下是错误详情。

System.ArgumentOutOfRangeException未处理   消息=索引超出范围。必须是非负数且小于集合的大小。 参数名称:index   来源= mscorlib程序   PARAMNAME =指数   堆栈跟踪:        在System.Collections.ArrayList.get_Item(Int32索引)        在System.Windows.Forms.DataGridViewSelectedRowCollection.get_Item(Int32 index)        在C:\ WorkSpace \ VetoSmsServer \ VetoSmsServer \ mainForm.cs中的VetoSmsServer.mainForm.removeEntryBt_Click(Object sender,EventArgs e):第310行        在System.Windows.Forms.Control.OnClick(EventArgs e)        在System.Windows.Forms.Button.OnClick(EventArgs e)        在System.Windows.Forms.Button.OnMouseUp(MouseEventArgs mevent)        在System.Windows.Forms.Control.WmMouseUp(消息& m,MouseButtons按钮,Int32点击)        在System.Windows.Forms.Control.WndProc(消息& m)        在System.Windows.Forms.ButtonBase.WndProc(消息& m)        在System.Windows.Forms.Button.WndProc(消息& m)        在System.Windows.Forms.Control.ControlNativeWindow.OnMessage(Message& m)        在System.Windows.Forms.Control.ControlNativeWindow.WndProc(Message& m)        在System.Windows.Forms.NativeWindow.DebuggableCallback(IntPtr hWnd,Int32 msg,IntPtr wparam,IntPtr lparam)        在System.Windows.Forms.UnsafeNativeMethods.DispatchMessageW(MSG& msg)        在System.Windows.Forms.Application.ComponentManager.System.Windows.Forms.UnsafeNativeMethods.IMsoComponentManager.FPushMessageLoop(IntPtr dwComponentID,Int32 reason,Int32 pvLoopData)        在System.Windows.Forms.Application.ThreadContext.RunMessageLoopInner(Int32 reason,ApplicationContext context)        在System.Windows.Forms.Application.ThreadContext.RunMessageLoop(Int32 reason,ApplicationContext context)        在System.Windows.Forms.Application.Run(Form mainForm)        在C:\ WorkSpace \ VetoSmsServer \ VetoSmsServer \ Program.cs中的VetoSmsServer.Program.Main():第18行        在System.AppDomain._nExecuteAssembly(RuntimeAssembly程序集,String [] args)        在System.AppDomain.ExecuteAssembly(String assemblyFile,Evidence assemblySecurity,String [] args)        在Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly()        在System.Threading.ThreadHelper.ThreadStart_Context(对象状态)        at System.Threading.ExecutionContext.Run(ExecutionContext executionContext,ContextCallback callback,Object state,Boolean ignoreSyncCtx)        在System.Threading.ExecutionContext.Run(ExecutionContext executionContext,ContextCallback回调,对象状态)        在System.Threading.ThreadHelper.ThreadStart()   InnerException:

4 个答案:

答案 0 :(得分:1)

执行此检查

numberSettingTable.SelectedRows != null

答案 1 :(得分:1)

您可以使用此代码

   // Get the currently selected row using the SelectedRow property.
    GridViewRow row = YourGridView.SelectedRow;
   if(row != null)
   {
     . . . .. 
   }  

链接完整示例:http://msdn.microsoft.com/fr-fr/library/system.web.ui.webcontrols.gridview.selectedrow.aspx

2基于RowCommand event

的另一种解决方案
void GridView_RowCommand(Object sender, GridViewCommandEventArgs e)
  {
    // If multiple buttons are used in a GridView control, use the
    // CommandName property to determine which button was clicked.
    if(e.CommandName=="Test")
    {
      // Convert the row index stored in the CommandArgument
      // property to an Integer.
      int index = Convert.ToInt32(e.CommandArgument);
    }
}

链接:http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.gridview.rowcommand.aspx

答案 2 :(得分:1)

您需要检查DataGridView控件中是否选择了任何行。如果有,您可以删除第一个选中或全部选中。以下代码段演示了删除所选的唯一行。此外,您应该注意不要尝试删除未提交的新行。因此,您可以使用以下行:

DataGridViewRow dgrvr = numberSettingTable.SelectedRows[0];
          if(!dgrvr.IsNewRow)

完整的代码段如下:

if(numberSettingTable.SelectedRows.Count>0)
{
      DataGridViewRow dgrvr = numberSettingTable.SelectedRows[0];
      if(!dgrvr.IsNewRow)
           numberSettingTable.Rows.Remove(dgrvr);   
}

为了删除多个选定的行,只需通过SelectedRows集合进行迭代。

答案 3 :(得分:0)

numberSettingsTable.SelectedRows永远不会为null,但是如果没有选择任何行,则其Count属性将为0(零)。

请检查:

if(dataGridView1.SelectedRows.Count > 0)
{

}
相关问题