index loop in a dataset

  • Thread starter Thread starter nologo
  • Start date Start date
N

nologo

hi all, having a few problems with a index loop on a dataset im
using. basically i have a datagrid displaying rows of data. i select
1+ rows, it deletes them but misses the last row.

for (int iIndex=0; iIndex<this.ultraGrid1.ActiveRow.Index; iIndex++)
{
// Store the schemeID to int
int iSchemeID = (int)this.cmbx_basetest.SelectedItem.DataValue;
// Store the Item Code to Delete
string strNumber = this.ultraDataSource1.Rows[iIndex]
["ItemCode"].ToString();

// Pass the ItemCode to the store procedure to delete
clsTestScheme test = new clsTestScheme();
test.TestDeleteProduct(iSchemeID, strNumber);
}

I cant see what im missing here..

Cheers in advance for any assistance
 
hi all, having a few problems with a index loop on a dataset im
using. basically i have a datagrid displaying rows of data. i select
1+ rows, it deletes them but misses the last row.

Do you have things bound such that deleting the row changes the grid?
If so, that's almost certainly the problem. The easiest way to get
round that is to create a collection of clsTestScheme items to delete
(without actually deleting them) and *then* delete them all. That way
your loop index and boundary for the first part won't be affected by
the second part.

Jon
 
Do you have things bound such that deleting the row changes the grid?
If so, that's almost certainly the problem. The easiest way to get
round that is to create a collection of clsTestScheme items to delete
(without actually deleting them) and *then* delete them all. That way
your loop index and boundary for the first part won't be affected by
the second part.

Jon

ah, yes, when i click "delete" it appears all the selected rows are
deleted..but when i reload the dataset..the item re-appears.
However, when i wish to delete only 1 product - the loop skips the
item, so its almost the first item is skipped.
i had considered using
string strNumber = this.ultraDataSource1.Rows[iIndex -1]
["ItemCode"].ToString();

but the boundary of the array fails
 
ok, resolved the issue with a simply method:
for (int iIndex=0; iIndex<=this.ultraGrid1.ActiveRow.Index; iIndex++)

Because the first row of the array is 0 but the first row of the
datagrid ActiveRow 1. so the problem was, if only 1 row is selected,
the loop only works if its less than 'iIndex'. this change says if
its equal to or less than, complete actions in loop.
 
Jon,

In my idea is for this kind of non strongly typed datasets, the easiest way
to work with most grid, to use a dataview in between, that saves you as well
the trouble with all kind of sorts.

Cor
 
Because the first row of the array is 0 but the first row of the
datagrid ActiveRow 1. so the problem was, if only 1 row
is selected, the loop only works if its less than 'iIndex'.
this change says if
its equal to or less than, complete actions in loop.

That seems like a really weird API - so the ActiveRow.Index is 1-
based, but actually fetching a row is 0-based? Sounds like a recipe
for disaster to me. (To be clear, I'm not criticising your code - just
the grid's API.)

Jon
 
In my idea is for this kind of non strongly typed datasets, the easiest way
to work with most grid, to use a dataview in between, that saves you as well
the trouble with all kind of sorts.

Certainly views are a good idea in general, but I'm not sure that I
see how it applies here. The fundamental problem (leaving aside the 0-
based/1-based issue) is that deleting from a collection while looping
through it is a tricky business to get right. It's simpler to create a
totally separate collection to loop through, and a plain List or
something similar should be fine for that.

Jon
 
Certainly views are a good idea in general, but I'm not sure that I
see how it applies here. The fundamental problem (leaving aside the 0-
based/1-based issue) is that deleting from a collection while looping
through it is a tricky business to get right. It's simpler to create a
totally separate collection to loop through, and a plain List or
something similar should be fine for that.

Jon

i'm using ultragrid, so i'm not a 100% sure but it seems to think
activerow =1 instead of 0.
seems to work, been testing it for some time today because yes..it
sounds like something could go very wrong lol. yeah i'll look into
methods you guys have provided me with.
cheers once again
 
Certainly views are a good idea in general, but I'm not sure that I
see how it applies here. The fundamental problem (leaving aside the 0-
based/1-based issue) is that deleting from a collection while looping
through it is a tricky business to get right. It's simpler to create a
totally separate collection to loop through, and a plain List or
something similar should be fine for that.

Jon

I agree with you.
Usually I prefer to usee something like:
List<string> delList = new .....
for (int iIndex=0; iIndex<this.ultraGrid1.ActiveRow.Index; iIndex++)
{
// Store the schemeID to int
int iSchemeID = (int)this.cmbx_basetest.SelectedItem.DataValue;
// Store the Item Code to Delete
string strNumber = this.ultraDataSource1.Rows[iIndex]
["ItemCode"].ToString();

delList.add( strNumber);

}
foreach(strging s in delList)
{
// Pass the ItemCode to the store procedure to delete
clsTestScheme test = new clsTestScheme();
test.TestDeleteProduct(iSchemeID, s);

}

It involve two loops but clearly separate the deletion. In the case of
the OP it can delete while looping cause he is really deleting the DB
info, nt the binded collection
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top