attempt to find cell id using control

  • Thread starter Thread starter WebBuilder451
  • Start date Start date
W

WebBuilder451

I'm attempting find the cell id, i (e.row.cells ) given a control in that
cell.
using this function:
private int GetcellPosition(GridViewRow gvr, string ctrlID)
{
var iCell = -1;
for (int i = 0; i < gvr.Cells.Count; i++ )
{
if ( gvr.Cells.FindControl(ctrlID) != null )
{
iCell = i;
break;
}
}
return iCell;
}
CALL: e.Row.Cells[GetcellPosition(e.Row, "LabelPerBS")].CssClass= "gvir2";

this does not work. It appears to be doing a find on the entire row so the
first position always gets returned. Is there a way to do this or am i
hitting a limitation?

thanks

kes
 
all the cell have the same NamingContainer (which is what FindControl uses).
you will have to search each cells Control collection (maybe recursivly) for
a control with the desired ID.

-- bruce (sqlwork.com)
 
bruce, i appreciate your response,

I think that is what i was trying to do, but i don't know how to isolate
each cell so that i can get to the collection.
i tried var Tablecell td = e.row.cells;
and for reasons you noted it does not work. I don't think a recursive call
will change this(?)


--
(i''ll be asking a lot of these, but I find C# totally way cooler than vb
and there''s no go''n back!!!)
thanks (as always)

kes


bruce barker said:
all the cell have the same NamingContainer (which is what FindControl uses).
you will have to search each cells Control collection (maybe recursivly) for
a control with the desired ID.

-- bruce (sqlwork.com)


WebBuilder451 said:
I'm attempting find the cell id, i (e.row.cells ) given a control in that
cell.
using this function:
private int GetcellPosition(GridViewRow gvr, string ctrlID)
{
var iCell = -1;
for (int i = 0; i < gvr.Cells.Count; i++ )
{
if ( gvr.Cells.FindControl(ctrlID) != null )
{
iCell = i;
break;
}
}
return iCell;
}
CALL: e.Row.Cells[GetcellPosition(e.Row, "LabelPerBS")].CssClass= "gvir2";

this does not work. It appears to be doing a find on the entire row so the
first position always gets returned. Is there a way to do this or am i
hitting a limitation?

thanks

kes
 
assuming your control is not nested (which would require recursion):

for (int i = 0; i < gvr.Cells.Count; i++ )
{
foreach (Control c in gvr.Cells.Controls)
{
if (c.ID == ctrlID)
return i;
}
}


-- bruce (sqlwork.com)


WebBuilder451 said:
bruce, i appreciate your response,

I think that is what i was trying to do, but i don't know how to isolate
each cell so that i can get to the collection.
i tried var Tablecell td = e.row.cells;
and for reasons you noted it does not work. I don't think a recursive call
will change this(?)


--
(i''ll be asking a lot of these, but I find C# totally way cooler than vb
and there''s no go''n back!!!)
thanks (as always)

kes


bruce barker said:
all the cell have the same NamingContainer (which is what FindControl uses).
you will have to search each cells Control collection (maybe recursivly) for
a control with the desired ID.

-- bruce (sqlwork.com)


WebBuilder451 said:
I'm attempting find the cell id, i (e.row.cells ) given a control in that
cell.
using this function:
private int GetcellPosition(GridViewRow gvr, string ctrlID)
{
var iCell = -1;
for (int i = 0; i < gvr.Cells.Count; i++ )
{
if ( gvr.Cells.FindControl(ctrlID) != null )
{
iCell = i;
break;
}
}
return iCell;
}
CALL: e.Row.Cells[GetcellPosition(e.Row, "LabelPerBS")].CssClass= "gvir2";

this does not work. It appears to be doing a find on the entire row so the
first position always gets returned. Is there a way to do this or am i
hitting a limitation?

thanks

kes
 
Thank you, i understand why mine did not work. Youer help was greatly
appreciated.

Thanks
KES

bruce barker said:
assuming your control is not nested (which would require recursion):

for (int i = 0; i < gvr.Cells.Count; i++ )
{
foreach (Control c in gvr.Cells.Controls)
{
if (c.ID == ctrlID)
return i;
}
}


-- bruce (sqlwork.com)


WebBuilder451 said:
bruce, i appreciate your response,

I think that is what i was trying to do, but i don't know how to isolate
each cell so that i can get to the collection.
i tried var Tablecell td = e.row.cells;
and for reasons you noted it does not work. I don't think a recursive call
will change this(?)


--
(i''ll be asking a lot of these, but I find C# totally way cooler than vb
and there''s no go''n back!!!)
thanks (as always)

kes


bruce barker said:
all the cell have the same NamingContainer (which is what FindControl uses).
you will have to search each cells Control collection (maybe recursivly) for
a control with the desired ID.

-- bruce (sqlwork.com)


:

I'm attempting find the cell id, i (e.row.cells ) given a control in that
cell.
using this function:
private int GetcellPosition(GridViewRow gvr, string ctrlID)
{
var iCell = -1;
for (int i = 0; i < gvr.Cells.Count; i++ )
{
if ( gvr.Cells.FindControl(ctrlID) != null )
{
iCell = i;
break;
}
}
return iCell;
}
CALL: e.Row.Cells[GetcellPosition(e.Row, "LabelPerBS")].CssClass= "gvir2";

this does not work. It appears to be doing a find on the entire row so the
first position always gets returned. Is there a way to do this or am i
hitting a limitation?

thanks

kes
 
Back
Top