Go4Expert

Go4Expert (http://www.go4expert.com/)
-   C# (http://www.go4expert.com/forums/c-sharp/)
-   -   delete a row in dataset (http://www.go4expert.com/forums/delete-row-dataset-t986/)

markyjj 7Jul2006 01:43

delete a row in dataset
 
I was hoping that someone could assist me in the following.

I have created a dataset in c# and have bound this to a datagrid and I want to delete a row in the datatable in the dataset.

I know how to add a row to the dataset but am unsure of how to delete a row. I want to delete a row by using the unique ID number but I am unsure of how to do this.

Can I use the indexof operator to obtain the index of the row in the datatable?

I am not using an adapter as the datasource is based on xml.

I would be grateful of any advice.

shabbir 7Jul2006 10:49

Re: delete a row in dataset
 
Use datarow.delete and then use AcceptChanges to commit the delete.

markyjj 12Jul2006 03:36

Re: delete a row in dataset
 
I am currently trying to use the delete method and then the accept changes method but am having problems.

I am trying to locate a row in the datatable to delete but am not sure how to go about this. I have decided to use a for loop to iterate through the datatable until a match is found with the entered ID number. However, when the matching ID number is found, the code that I use to delete the found row doesnt seem to work. I get a thrown exception saying that the row at position (n)does not exist in the table and the datagrid on the form goes blank.

The code that I am using to try to delete the found row is shown below.
Code: C#

int RowCount = 0;
foreach(DataRow dr in mydataset.Tables[1].Rows)
{     
    RowCount ++;
    if (txtStudID.Text == dr[0].ToString())
    {
        mydataset.Tables[1].Rows[RowCount].Delete();
        mydataset.WriteXml("C://Student1.xml");
        mydataset.WriteXmlSchema("C://Student1.xml");           
    }
} //end of for loop     
mydataset.Tables[1].Rows[RowCount].AcceptChanges();         
f2.dataGrid1.DataSource = mydataset;   
return;

Any help will be very much appreciated.

shabbir 12Jul2006 11:48

Re: delete a row in dataset
 
Please read Before you make a query thread regarding code snippets.
Quote:

7) Use code tags. Use the appropriate code tag (e.g. [ php][/php ] and [ code][/code ]) to make your code easier to read. See BB Code Section for [Code]
Also you need to break the loop where you are deleting so that the AcceptChanges is on the correct RowCount


All times are GMT +5.5. The time now is 07:51.