One more problem with DataGrid

G

Guest

I am trying to iterate through a datagrid using the following code:

private void updateFCA(object sender,
System.Web.UI.WebControls.DataGridItemEventArgs e)
{
for (int i = 0; i < e.Item.Cells.Count - 1; i ++)
{
if (e.Item.Cells[3].Text == "FCA")
{
e.Item.Cells[3].Text = "FCA";
}
else if (e.Item.Cells[3].Text == "")
{
e.Item.Cells[3].Text = "N";
}
else
{
e.Item.Cells[3].Text = "Y";
}
}
}

Thie iteration does not work as the int i value is not used. How do I alter
the code to fix this problem?

Thanks,

Dave
 
G

Guest

I corrected that problem but the program still does not iterate through the
column.

Thanks,

XPhaktor said:
There is a space between the "i" and the "++" (i.e., "i ++" should be "i++")

kscdavefl said:
I am trying to iterate through a datagrid using the following code:

private void updateFCA(object sender,
System.Web.UI.WebControls.DataGridItemEventArgs e)
{
for (int i = 0; i < e.Item.Cells.Count - 1; i ++)
{
if (e.Item.Cells[3].Text == "FCA")
{
e.Item.Cells[3].Text = "FCA";
}
else if (e.Item.Cells[3].Text == "")
{
e.Item.Cells[3].Text = "N";
}
else
{
e.Item.Cells[3].Text = "Y";
}
}
}

Thie iteration does not work as the int i value is not used. How do I alter
the code to fix this problem?

Thanks,

Dave
 
D

Daniel Billingsley

kscdavefl said:
Thie iteration does not work as the int i value is not used. How do I alter
the code to fix this problem?

Why are using the for loop in the first place when all you apparently want
to do is manipulate cell #3?
 
D

DalePres

You have several problems with your concept here:

i<e.Item.Cells.Count-1 would mean, if your loop did anything at all, that
you would never process the last cell. You should use i<e.Item.Cells.Count.
If you have 4 cells, you will iterate cells 0, 1, 2, 3. With the way you
have it, you would iterate cells 0, 1, 2.

Secondly, you don't ever reference i within your loop so why a for loop? It
is not clear how you are determining that i is not used. Your code should
loop through the columns, and therefore use i at least as the loop counter,
with the exception that I outlined in the first paragraph above: you will
never execute a count on the last column.

You call for Cells[3] in every iteration of your loop so you do exactly the
same thing each iteration. When your loop completes, Cells[3] is always
going to be FCA or Y since your last else will replace all the N values with
Y on the second iteration.

Next, why iterate the columns at all? Unless your entire data structure is
dynamic, then FCA should be in the same cell position, apparently in
Cells[3].

Perhaps you really mean to iterate through the rows using e.Items?

Also, you may want to replace your long series of if else statements with a
switch statement.

HTH

DalePres
MCAD, MCDBA, MCSE
 

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

Top