2011-08-25 206 views
4

如何刪除鍵盤上的選定行刪除鍵按下(從dataGridView中刪除並從數據庫中刪除)?如何刪除鍵盤上的dataGridView行刪除鍵按? c#

這裏我怎麼填充的dataGridView:

private void GetDate() 
     { 

      SqlDataAdapter adapter = new SqlDataAdapter("SELECT id as [ID],description as [Description],unit as [Unit], qty as [Quantity],unitRate as [Unit Rate], amount as [Amount], _datetime as [Date] FROM tbl_BOQ WHERE projectId = "+id, conn); 
      adapter.SelectCommand.CommandType = CommandType.Text; 

      DataTable table = new DataTable(); 
      table.Clear(); 

      adapter.Fill(table); 


      dataGridView1.DataSource = table; 


     } 
+0

嗨,表現出一定的代碼,如何是你的網格人口稠密?你的頁面/表單看起來如何? ASP.NET或Windows窗體? –

+0

它是Windows窗體應用程序不是ASP.NET – Amer

+0

hei ..how你做了這個..plz幫助我 – minakshi

回答

0

您是否嘗試過使用KeyPress事件DataGridView的?

然後,您可以使用DataGridView的SelectedRows屬性。

+0

我認爲'CurrentRow'將是適當的 – V4Vendetta

+1

這將取決於您的MultiSelect設置。 http://msdn.microsoft.com/en-us/library/system.windows.forms.datagridview.multiselect.aspx – Paul

+0

它的不同集合在一起 – V4Vendetta

6

我使用的DataGridView的KeyDown事件,並在處理程序確定它是否被按下Delete鍵:

if e.KeyCode == Keys.Delete...

然後找項目/行而如果獲取SelectedRows屬性刪除,你的DataGridView的是FullRowSelect或RowHeaderSelect模式,否則,你可以確定的是這樣的行:

i = SelectedCells[0].RowIndex

則:

DataGridView.Rows[i].DataBoundItem

你會那麼只需從數據庫中刪除相應的記錄,並可能刷新DataGridView的depening其在如何綁...

+0

keydown事件不會爲我開火 – minakshi

+0

我用KeyUp因爲KeyDown沒有開火。 –