attempt to find cell id using control

Discussion in 'ASP .Net' started by WebBuilder451, Dec 10, 2008.

  1. 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
    WebBuilder451, Dec 10, 2008
    #1
    1. Advertising

  2. WebBuilder451

    bruce barker Guest

    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" wrote:

    > 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
    bruce barker, Dec 10, 2008
    #2
    1. Advertising

  3. 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" wrote:

    > 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" wrote:
    >
    > > 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
    WebBuilder451, Dec 10, 2008
    #3
  4. WebBuilder451

    bruce barker Guest

    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" wrote:

    > 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" wrote:
    >
    > > 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" wrote:
    > >
    > > > 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
    bruce barker, Dec 11, 2008
    #4
  5. Thank you, i understand why mine did not work. Youer help was greatly
    appreciated.

    Thanks
    KES

    "bruce barker" wrote:

    > 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" wrote:
    >
    > > 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" wrote:
    > >
    > > > 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" wrote:
    > > >
    > > > > 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
    Yankee Imperialist Dog!, Dec 11, 2008
    #5
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Lloyd Dupont
    Replies:
    0
    Views:
    394
    Lloyd Dupont
    Jul 27, 2003
  2. Jon   Paal

    2nd attempt - post back event in server control

    Jon Paal, Sep 24, 2006, in forum: ASP .Net Building Controls
    Replies:
    0
    Views:
    109
    Jon Paal
    Sep 24, 2006
  3. Empire City
    Replies:
    3
    Views:
    389
    Empire City
    Apr 30, 2004
  4. Mrinalini Pande

    Find which Cell is clicked by user in GridView control of Asp.net

    Mrinalini Pande, Nov 23, 2006, in forum: ASP .Net Datagrid Control
    Replies:
    0
    Views:
    751
    Mrinalini Pande
    Nov 23, 2006
  5. GMI
    Replies:
    3
    Views:
    481
    Tad McClellan
    Jun 19, 2005
Loading...

Share This Page