The first variant removes the row from the collection but does not mark it as "deleted" (to be deleted on the next update on the tableadapter), while the second one works fine.
Regards,
B.ShivaKarthik
Project Manager
Mumbai , India
-------------------------------------------------
Don't Forget to Mark as Answer for the post that helped you.
-------------------------------------------------
AhmadRock51
Member
359 Points
276 Posts
how to remove rows from a datatable ???
Aug 05, 2008 07:06 AM|LINK
Hi i m trying to remove rows from datatable using
for (int i = 0; i < 9; i++){
dt.Rows.RemoveAt(i);
}
but it only remove the 1st row
whats wrong wid it???
mohi88
Contributor
3752 Points
734 Posts
Re: how to remove rows from a datatable ???
Aug 05, 2008 09:06 AM|LINK
do it this way;
for(int i=9; i=>0; i--)
{ // your remove code here
}
after that you mak ethe dt.acceptchanges();
that is all.
hope this helps
MCSD.NET
My Blog ||My Tweets ||My Photos ||LinkedIn
kcube
Member
279 Points
128 Posts
Re: how to remove rows from a datatable ???
Aug 05, 2008 09:26 AM|LINK
check this snippet!
can i know your requirement plz?
DataTable dt = new DataTable(); DataColumn dcID = new DataColumn("user_id", typeof(System.Int32)); DataColumn dcName = new DataColumn("user_name", typeof(System.String));dt.Columns.Add(dcID);
dt.Columns.Add(dcName);
for (int i = 0; i < 6; i++){
System.Data.DataRow dr = dt.NewRow();dr[
"user_id"] = i; dr["user_name"] = "user name " + i.ToString();dt.Rows.Add(dr);
}
int deletecount = 0;for (int i = 1; i < 4; i++){
dt.Rows.RemoveAt(i-deletecount);
deletecount += 1;
}
dt.AcceptChanges();
foreach (System.Data.DataRow drValue in dt.Rows){
lblDetails.Text += drValue["user_name"].ToString() + " -- ";}
lblDetails.Text +=
"<br />";KCube
dharnendra
Contributor
2955 Points
551 Posts
Re: how to remove rows from a datatable ???
Aug 05, 2008 09:33 AM|LINK
Hi,
Do as per below,
foreach (System.Data.DataRow drValue in dt.Rows)
{
drValue.delete();}
dt.acceptchanges();
Technical Leader
GTL-Ahmedabad
kcube
Member
279 Points
128 Posts
Re: how to remove rows from a datatable ???
Aug 05, 2008 11:17 AM|LINK
1. dataTable.Rows.RemoveAt(0)
2. dataTable.Rows[0].Delete()
The first variant removes the row from the collection but does not mark it as "deleted" (to be deleted on the next update on the tableadapter), while the second one works fine.
KCube
ShivaKarthik
Participant
1675 Points
277 Posts
Re: how to remove rows from a datatable ???
Aug 05, 2008 01:26 PM|LINK
Hi Buddy,
try the following way
for(int i=0;i<9;i++)
{
dt.Rows[i].Delete();
}
dt.AcceptChanges();
Happy Coding!!!!!!!!!
Mark As Answer if this helps you
Further Queries Recommended
B.ShivaKarthik
Project Manager
Mumbai , India
-------------------------------------------------
Don't Forget to Mark as Answer for the post that helped you.
-------------------------------------------------
Eswar.Palach...
Member
46 Points
90 Posts
Re: how to remove rows from a datatable ???
Apr 20, 2010 12:30 PM|LINK
Thank u,
you are saved me alot
Hyderabad India.