0

I have a database in xml my xml file is:

      <?xml version="1.0" encoding="UTF-8" standalone="yes"?>
          <!--This is an 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 is following for deleting a row from DataGridView and xml file. But my code always delete first row if I select any row from DataGridView and press the 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 that delete only one selected row on button click event enter image description here

user2342574
  • 109
  • 2
  • 8
  • 17

2 Answers2

1

Your current code always select row at index 0 as row, that's why it always delete the first row in the DataGridView.

You want to get row index of currently selected cell instead and you can try to get it from CurrentCell.RowIndex property. At this point you'll be able to delete row at that index :

int selectedRow = dtgvCategory.SelectedRows.Count;
if (selectedRow > 0)
{
    selectedRowIndex = dtgvCategory.CurrentCell.RowIndex;
    row = ds.Tables[0].Rows[selectedRowIndex];
    row.Delete();
}
Community
  • 1
  • 1
har07
  • 88,338
  • 12
  • 84
  • 137
  • thanks sir your code work perfectly..... now i want code to update record in xml file using c# code.please provide some solution or idea – user2342574 Apr 24 '15 at 16:18
0

you could add an eventhandler for when the row is selected using RowStateChanged:

public int SelectedRow = 0;

private void dtgvCategory_RowStateChanged(object sender, DataGridViewRowStateChangedEventArgs e)
    {
        // return if not StateChanged
        if (e.StateChanged != DataGridViewElementStates.Selected) return;

        // then you could put that row in a public variable
        SelectedRow = e.Row.Index;
    }

Now in your delete handler you know which row to delete.

https://msdn.microsoft.com/en-us/library/system.windows.forms.datagridviewrowstatechangedeventargs%28v=vs.110%29.aspx

Dominic Scanlan
  • 1,009
  • 1
  • 6
  • 12
  • thanks for Answer but it is not working properly..it give error. Error 1 Cannot implicitly convert type 'System.Windows.Forms.DataGridViewRow' to 'int' D:\IMS\IMS\Form1.cs 163 27 IMS – user2342574 Apr 24 '15 at 10:55
  • thanks for your reply but again when we click on delete button it delete first row either we select second row or any other row .we want to delete selected row only – user2342574 Apr 24 '15 at 11:23
  • What does e.Row.Index return? What is e.Row.State? – Dominic Scanlan Apr 24 '15 at 11:27