hide/show GridCheckBoxColumn in ItemDataBound

9 posts, 1 answers
  1. DJ
    DJ avatar
    14 posts
    Member since:
    Jan 2008

    Posted 23 Apr 2008 Link to this post

    I'm trying to determine weather to show or hide the checkbox in a GridCheckBoxColumn inside the grid's ItemDataBound event.  I thought this would be a simple task.

    Inside my ItemDataBound method:

    if (e.Item is GridDataItem)
                {
                    GridDataItem dataItem = e.Item as GridDataItem;

                    if (dataItem["ItemTypeName"].Text.Equals("Sponsorship"))
                    {
                        CheckBox cb = (CheckBox)dataItem["column"].Controls[0];
                        //cb.Enabled = false;
                        cb.Visible = false;
                        //cb.Checked = true;
                        lbCancelEntireOrder.Visible = false;
                    }
                }

    the lbCancelEntireOrder.Visible line works - so I know the problem is not in the ItemDataBound itself.  Am I getting the CheckBox out of the column properly?

    Thanks

  2. Shinu
    Shinu avatar
    17004 posts
    Member since:
    Mar 2007

    Posted 24 Apr 2008 Link to this post

    Hi DJ,

    Try the following code snippet to achieve the desired scenario.

    CS:

      protected void RadGrid1_ItemDataBound(object sender, GridItemEventArgs e) 
        { 
            if (e.Item is GridDataItem) 
            { 
                GridDataItem item = (GridDataItem)e.Item; 
                CheckBox chkbx = (CheckBox)item["column"].Controls[0]; 
                string strtxt = item["ItemTypeName"].Text.ToString(); 
                if (strtxt == "Sponsorship") 
                { 
                    chkbx.Visible = false
                } 
            } 
      } 


    Thanks
    Shinu.

  3. DJ
    DJ avatar
    14 posts
    Member since:
    Jan 2008

    Posted 24 Apr 2008 Link to this post

    This code is pretty close to what I had - but I did copy it all and paste it into my page with the same results.   The label's visibility is properly set to false, but the checkbox just will not go away.

  4. Shinu
    Shinu avatar
    17004 posts
    Member since:
    Mar 2007

    Posted 24 Apr 2008 Link to this post

    Hi Dj,

    Can you check whether you have set the correct ColumnUniqueName for the CheckBox Column? Sending your aspx code will be more good.

    Thanks
    Shinu.

  5. DJ
    DJ avatar
    14 posts
    Member since:
    Jan 2008

    Posted 25 Apr 2008 Link to this post

    Ok, I removed everything except the UniqueName from the column:

    <radG:GridCheckBoxColumn UniqueName="cbCol"
    </radG:GridCheckBoxColumn> 

    And I've modified my aspx.cs page a little bit:

    protected void rdgOrderItems_OnItemDataBind(object sender, Telerik.WebControls.GridItemEventArgs e) 
            { 
                if (e.Item is GridDataItem) 
                { 
                    GridDataItem item = (GridDataItem)e.Item; 
                    CheckBox chkbx = (CheckBox)item["cbCol"].Controls[0]; 
                    string strtxt = item["ItemTypeName"].Text.ToString(); 
                    if (strtxt == "Sponsorship") 
                    { 
                        chkbx.Visible = false
                        lbCancelEntireOrder.Visible = false
                    } 
                } 

    The lines that hides lbCancelEntireOrder works - while the line that hides chkbx does nothing. 

    What am I doing wrong here, any idea?  Thanks.

  6. Shinu
    Shinu avatar
    17004 posts
    Member since:
    Mar 2007

    Posted 28 Apr 2008 Link to this post

    Hi DJ,

    Could try the same code in the ItemCreated event handler? Try it out  and let me know if this helped.

    Thanks,
    Shinu

  7. DJ
    DJ avatar
    14 posts
    Member since:
    Jan 2008

    Posted 28 Apr 2008 Link to this post

    I put the code into an OnItemCreated method and get the same results.

    I also tried to loop through all rows in the PreRender of the grid, but that didn't work either.

    I'm starting to get really frustrated with this.  Telerik has typically been good to me, but not the GridCheckBoxColumn.

  8. Answer
    Yavor
    Admin
    Yavor avatar
    11 posts
    Member since:
    Dec 2013

    Posted 29 Apr 2008 Link to this post

    Hello DJ,

    Attached to this message is a small application, which handles a functionality close to the one that you mentioned. Take a look at it and let me know if this is the expected behavior.

    Greetings,
    Yavor
    the Telerik team


    Instantly find answers to your questions at the new Telerik Support Center

  9. DJ
    DJ avatar
    14 posts
    Member since:
    Jan 2008

    Posted 29 Apr 2008 Link to this post

    Thanks for the example.  That is pretty much what I was trying to accomplish, but 2 minor differences.

    1. My form was put into edit mode so the checkboxes were enabled.

    2. My column didn't have a DataType.

    I've just removed the whole column instead of tyring to hide individual checkboxes - that is going to work just fine for me.

    Thanks

Back to Top