Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Removing DataRow From array but not the DataTable 1

Status
Not open for further replies.

KoRUPT

Programmer
Jul 25, 2002
25
0
0
CA
Here is the code I am talking about:
Code:
			MySQL con = new MySQL();
			DataTable dt = new DataTable( "TEST" );

			con.FillDataTable( "SELECT 1 AS ID, \"Name1\" AS Name;", ref dt, true );
			con.FillDataTable( "SELECT 2 AS ID, \"Name2\" AS Name;", ref dt, false );
			con.FillDataTable( "SELECT 3 AS ID, \"Name3\" AS Name;", ref dt, false );
			con.FillDataTable( "SELECT 4 AS ID, \"Name4\" AS Name;", ref dt, false );
			con.FillDataTable( "SELECT 5 AS ID, \"Name5\" AS Name;", ref dt, false );
			con.FillDataTable( "SELECT 6 AS ID, \"Name6\" AS Name;", ref dt, false );
			con.FillDataTable( "SELECT 7 AS ID, \"Name7\" AS Name;", ref dt, false );

			DataRow[] r = dt.Select( "ID=4" );
			r[0]["Name"] = "Test4";
			dt.AcceptChanges();
			r[0].Delete();
			dt.AcceptChanges();

At the line "r[0].Delete()" the row is question is deleted from the datatable "dt". What I want to happen is that the row is removed from the DataRow[] array, but NOT the DataTable. Thank you.
 
(sorry if I am doing this wrong but I dont know how to edit my post)

I noticed that the code is mostly links to my own functions, so let me explain a breif bit. The MySQL class sets up a connection to the server and implements some container functions. The MySQL.FillDataTable function fills a datatable with whatever is selected; the last parameter indicates whether to clear the table of all entries or add a new entry (true = clear).

So the DataTable dt has 7 rows, 2 columns before the select statement.
 
KoRUPT,
Code:
[b]DataRow[] r[/b] = dt.Select( "ID=4" );
r[0]["Name"] = "Test4";
dt.AcceptChanges();
[b]r[0][/b].Delete();
Well, you're invoking the Delete method of the DataRow class and thus, you're deleting the row, not the array element, and since this array row is a reference type, it's deleted from the DataTable where it came from.

I don't know how to manipulate arrays very much as I don't use them frequently because I like the much easier-to-use ArrayList type which is some sort of array on steroids. ArrayList is like a dynamic array to and from which you add/remove elements at will. I would suggest that you use the ArrayList instead of Array to accomplish what you want.

Here's the code:
Code:
ArrayList rows = [blue]new[/blue] ArrayList();

[blue]foreach[/blue] (DataRow row [blue]in[/blue] dt.Select("ID=4"));
{
   [green]// Add row to arraylist object[/green]
   rows.Add(row);
}

[green]// Use the first row[/green]
DataRow r = [b](DataRow)[/b]rows[0];
r["Name"] = "Test4";
dt.AcceptChanges();

[green]// Delete first row from the arraylist[/green]
rows.RemoveAt(0);
ArrayList stores its elements as object and you have to cast them into the appropriate types before you use them. That's what the line [tt]DataRow r = (DataRow)rows[0][/tt] mean.

Hope this helps!

JC

_________________________________________________
To get the best response to a question, read faq222-2244.
 
or you can do:

[copy] DataTable dtTemp = dt.Copy();
DataRow[] r = dtTemp.Select( "ID=4" );
r[0]["Name"] = "Test4";
dtTemp.AcceptChanges();
r[0].Delete();
dtTemp.AcceptChanges();[/copy]

and then use dtTemp for any other modifications or display.


--------------------------
"two wrongs don't make a right, but three lefts do" - the unknown sage
 
DaZZleD,
Your approach is much simpler and cleaner - Have a star!

JC

_________________________________________________
To get the best response to a question, read faq222-2244.
 
Thank you JC and DaZZleD. I am trying both approaches now and trying to work on implementing them. There are still some things in both approaches that I have to work out. Firstly, DaZZleD's approach is clean and good, but the point of trying to remove the DataRow array and NOT delete them, was so I can have references to the original data rather than copies. I did not make that clear in my OP so I appologize. I am now testing JC's method to see if the performance is acceptable.

Thanks once again for your help. Cheers.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top