刪除GridView中選定的行

來源:互聯網
上載者:User

     之前為了實現這個問題,百度了很多文章,也還是沒能看明白,或許別人需要的功能太複雜,或許……

------------------廢話分割線----

首先擷取被選中的行序號:

  int selected = productDataDataGridView.SelectedRows[0].Cells[0].RowIndex;//只讓選一行,就直接0,0了。

拿到被選中的那行:

   DataRow dr = productDataSet.ProductData.Rows[selected];
標記刪除:

   dr.Delete();

更新資料庫:
 productDataTableAdapter.Update(productDataSet);

搞定。

------------------一個小問題------------------

之前刪除行使用的是dataTable.RemoveAt(selected)

結果導致了再GridView裡面顯示已經刪除,而當程式重啟後,該行依然存在,即資料庫中的行並沒有刪除。

後來百度得知,原因是使用RemoveAt將行移除,導致無法為更新資料庫sql語句提供行。

害我還自作聰明的使用RemoveAt……

 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.