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.
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
Re: Changing checkbox color in gridview
Thank you very much for your help.