Changing checkbox color in gridview-VBForums
Results 1 to 3 of 3

Thread: Changing checkbox color in gridview

  1. #1

    Thread Starter
    Member
    Join Date
    Apr 2007
    Posts
    43

    Changing checkbox color in gridview

    Hi everyone,

    I have a ASP.Net gridview that is bound to a datasource. One of the fields in the datasource is a SQL bit value called Online which is linked to the gridview as a readonly checkbox.

    I am hoping to change the color of the checkbox depending on the Online value.

    Ie: Online = 1/true - Color checkbox green
    Online = 0/false - Color checkbox red

    Could someone guide me in the right direction with this?
    I am guessing I would have to do this around the datasource selected event?

    Thanks in advance.

  2. #2
    Fanatic Member
    Join Date
    May 2005
    Posts
    608

    Re: Changing checkbox color in gridview

    Hello,

    Create a handler for the RowDataBound event of the GridView.
    Inside that handler, find the checkbox and depending if it's checked or not, add either the CSS attribute to the control, or force the color change with the control's properties.

    The code should be something like this:

    Code:
    Protected Sub GridView1_RowDataBound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewRowEventArgs) Handles GridView1.RowDataBound
    
        If e.Row.RowType = DataControlRowType.DataRow Then
    
            'Replace "CheckboxName" for the generic name of your checkbox
            Dim ChkBox as CheckBox = CType(e.Row.FindControl("CheckboxName"), CheckBox)
    
            If ChkBox.Checked = True Then
                ChkBox.CssClass = "chkbox1"
            Else
                ChkBox.CssClass = "chkbox2"
            End If
        End If
    
    End Sub
    I have not tested if the change of color works properly, but it looks about right.

    HTH,
    HoraShadow
    I do like the reward system. If you find that my post was useful, rate it.

  3. #3

    Thread Starter
    Member
    Join Date
    Apr 2007
    Posts
    43

    Re: Changing checkbox color in gridview

    Thank you very much for your help.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Featured


Click Here to Expand Forum to Full Width

Survey posted by VBForums.