xml - How to delete a row from datagridview using c# in Windows application -


i have database in xml xml file is:

      <?xml version="1.0" encoding="utf-8" standalone="yes"?>           <!--this xml generated file-->        <categories>          <category>           <categoryid>1</categoryid>           <categoryname>jitu</categoryname>          </category>          <category>           <categoryid>2</categoryid>           <categoryname>ansul</categoryname>          </category>          <category>           <categoryid>3</categoryid>           <categoryname>satish</categoryname>          </category>          <category>           <categoryid>4</categoryid>           <categoryname>tipu</categoryname>          </category>      </categories> 

my c# code following deleting row datagridview , xml file. code delete first row if select row datagridview , press delete button.

 private void btndelete_click(object sender, eventargs e)      {                    xmldocument xdoc = new xmldocument();         string path = "xmldata.xml";          ds.clear();         dtgvcategory.refresh();         ds.readxml(path);         row = ds.tables[0].rows[0];         int selectedrow = dtgvcategory.selectedrows.count;         if (selectedrow > 0)         {             row.delete();         }          ds.writexml(path);         ds.acceptchanges();     } 

i want code delete 1 selected row on button click event enter image description here

your current code select row @ index 0 row, that's why delete first row in datagridview.

you want get row index of selected cell instead , can try currentcell.rowindex property. @ point you'll able delete row @ index :

int selectedrow = dtgvcategory.selectedrows.count; if (selectedrow > 0) {     selectedrowindex = dtgvcategory.currentcell.rowindex;     row = ds.tables[0].rows[selectedrowindex];     row.delete(); } 

Popular posts from this blog

c# - ODP.NET Oracle.ManagedDataAccess causes ORA-12537 network session end of file -

utf 8 - split utf-8 string into bytes in python -

matlab - Compression and Decompression of ECG Signal using HUFFMAN ALGORITHM -