2012-10-29 55 views
0

我有一個贏的窗體上的按鈕,其中刪除網格視圖上的行。沒有選擇的按下網格視圖的按鈕

numberSettingTable.SelectedRows[0] 

問題是當我按下按鈕沒有行選擇它發送null,在Java -1是返回它告訴沒有行被選中。那麼我怎樣才能在c#中實現相同?

我已經試過if語句

numberSettingTable.SelectedRows[0] != null 

但它沒有工作。

以下是錯誤的詳細信息。

System.ArgumentOutOfRangeException未處理 Message = Index超出範圍。必須是非負數且小於集合的大小。 參數名稱:索引 源= mscorlib程序 PARAMNAME =指數 堆棧跟蹤: 在System.Collections.ArrayList.get_Item(的Int32指數) 在System.Windows.Forms.DataGridViewSelectedRowCollection.get_Item(的Int32指數) 在VetoSmsServer.mainForm。 C:\ WorkSpace \ VetoSmsServer \ VetoSmsServer \ mainForm.cs中的removeEntryBt_Click(Object sender,EventArgs e):行310 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 & m,MouseButtons button,Int32 clicks) 在System.Windows.Forms.Control.WndProc(消息&米) 在System.Windows.Forms.ButtonBase.WndProc(消息&米) 在System.Windows.Forms.Button.WndProc(消息&米) 在System.Windows.Forms.Control.ControlNativeWindow.OnMessage(消息&米) 在System.Windows.Forms.Control.ControlNativeWindow.WndProc(消息&米) 在System.Windows.Forms.NativeWindow.DebuggableCallback(IntPtr的的HWND,的Int32 msg,IntPtr wparam,IntPtr lparam) at System.Windows.Forms.UnsafeNativeMethods.DispatchMessageW(MSG & msg) at System.Windows.Forms.Application.ComponentManager.System.Windows.For ms.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 VetoSmsServer.Program.Main()in C:\ WorkSpace \ VetoSmsServer \ VetoSmsServer \ Program.cs:line 18 at System .AppDomain._nExecuteAssembly(RuntimeAssembly組件,字串[] args) 在System.AppDomain.ExecuteAssembly(字符串assemblyFile,證據assemblySecurity,字串[] args) 在Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly() 在SY在System.Threading.ExecutionContext.Run(ExecutionContext executionContext,ContextCallback回調,對象狀態,布爾ignoreSyncCtx) at System.Threading.ExecutionContext.Run(ExecutionContext executionContext,ContextCallback callback,Object狀態) 在System.Threading.ThreadHelper。的ThreadStart() 的InnerException:

+0

已經嘗試了上述 – greatmajestics

回答

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集合。

+0

解決..謝謝。 – greatmajestics

1

進行這樣的檢查,而不是

numberSettingTable.SelectedRows != null 
+0

嘗試,但得到了同樣的錯誤如上 – greatmajestics

+0

你有消息的ArgumentOutOfRangeException * =索引超出範圍*當您試圖訪問一個數組,在這裏你不是想訪問任何數組,所以我不明白你如何得到這個錯誤,除非你正在訪問一個數組 – Gabber

+0

我已經提到我正在訪問一個數組。也在選定的行上返回。 System.Windows.Forms.DataGridViewSelectedRowCollection – greatmajestics

1

您可以使用此代碼

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

鏈接完整的示例:基於RowCommand event

http://msdn.microsoft.com/fr-fr/library/system.web.ui.webcontrols.gridview.selectedrow.aspx

2另一種解決方案

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

+0

對不起但在我的網格視圖 – greatmajestics

+0

沒有selectedrow最佳做法,您還可以與RowCommand事件試着在你的網格視圖 –

+0

謝謝你的幫助。 – greatmajestics

0

numberSettingsTable.SelectedRows永遠不會爲空,但是,如果沒有行選擇其Count屬性將是0(零)。

檢查是不是:

if(dataGridView1.SelectedRows.Count > 0) 
{ 

}