change color on row in gridview using event

Last post 11-07-2009 3:09 PM by castfamilydad. 3 replies.

Sort Posts:

  • change color on row in gridview using event

    11-07-2009, 12:48 PM

    I am trying to change the backgound color on a datarow in a gridview while it is being built by using an event in the properties of the gridview.  I found an example for an integer check but the field I want to check is a text field with the words "STANDARD" OR "SIGNIFICANT" stored in the field within the database.  Here is the code I am executing below but I get the error below.  (I am including the code I found that does the integer check at the bottom so you can see it as well).  Anyone got any idea as to my problem?

    Error 5 Overload resolution failed because no accessible '=' can be called with these arguments: 'Public Shared Operator =(a As String, b As String) As Boolean': Value of type '1-dimensional array of Object' cannot be converted to 'String'.

    Sub typeGridView_RowDataBound(ByVal sender As Object, _
      ByVal e As System.Web.UI.WebControls.GridViewRowEventArgs)
            If e.Row.RowType = DataControlRowType.DataRow Then
                'determine the value of the type field
                Dim whattype(DataBinder.Eval(e.Row.DataItem, "type"))
                If whattype = "STANDARD" Then
                    ' color the background of the row yellow
                    e.Row.BackColor = Drawing.Color.Yellow
                End If
            End If
        End Sub
     
    Code example I found that does an interger.....
     
    Sub productsGridView_RowDataBound(ByVal sender As Object, _
      ByVal e As System.Web.UI.WebControls.GridViewRowEventArgs)
        If e.Row.RowType = DataControlRowType.DataRow Then
            'determine the value of the UnitsInStock field
            Dim unitsInStock As Integer =  _
              Convert.ToInt32(DataBinder.Eval(e.Row.DataItem, _
              "UnitsInStock"))
            If unitsInStock = 0 Then
                ' color the background of the row yellow
                e.Row.BackColor = Drawing.Color.Yellow
            End If
        End If
    End Sub
    

     
    
    


     

  • Re: change color on row in gridview using event

    11-07-2009, 12:55 PM
    • Participant
      1,641 point Participant
    • vishwaraj1
    • Member since 11-07-2008, 7:44 PM
    • India
    • Posts 424

    do it on row GridView1_RowCreated event
    
    
    Protected Sub GridView1_RowCreated(ByVal sender As Object, ByVal e As GridViewRowEventArgs)
        'Add CSS class on header row.
        If e.Row.RowType = DataControlRowType.Header Then
            e.Row.CssClass = "header"
        End If
        
        'Add CSS class on normal row.
        If e.Row.RowType = DataControlRowType.DataRow AndAlso e.Row.RowState = DataControlRowState.Normal Then
            e.Row.CssClass = "normal"
        End If
        
        'Add CSS class on alternate row.
        If e.Row.RowType = DataControlRowType.DataRow AndAlso e.Row.RowState = DataControlRowState.Alternate Then
            e.Row.CssClass = "alternate"
        End If
    End Sub
    
    and create CSS like this....
    
    /*GridViewCSS*/
    
    .grid-view
    {
        color:#000;
    	margin: 0 auto;
    	font-family: "Verdana", Arial, Helvetica, sans-serif, Trebuchet MS;
    	font-size: 11px;
    }
    
    .grid-view tr.header
    {
    	color: #ffffff;
    	background:#454545;
    	height: 25px; 
    }
    .grid-view tr.header a
    {
        color:#FFF;
    }
    
    .grid-view tr.normal
    {
    	background-color:#fdfdfd;
    	color: #000;
    }
    
    .grid-view tr a
    {	
    	text-decoration:none;
    }
    .grid-view tr td a
    {	
    	text-decoration:none;
    	padding-right:5px;
    	font-size:12px;
    }
    
    .grid-view tr a:hover
    {
    	text-decoration:underline;
    }
    
    .grid-view tr.alternate
    {
    	background-color:#f3f2f2;
    }
    
    .grid-view tr.normal:hover, .grid-view tr.alternate:hover
    {
    	background-color:#f8f8de;
    	color: #000;
    }
    .GridViewPagerStyle
    {
    	background:#b5babf;
        height:25px;
        font-weight: bold;
        color:#fff;
        text-align:center;
    }
    .GridViewPagerStyle a
    {
    	margin-right:5px;
    	padding:5px;
    	font-family:Geneva, Arial, Helvetica, sans-serif;
    	font-size:12px;
    	letter-spacing:1px;
    	font-weight:bolder;
    	text-decoration:none;
    	color:#264992;
    }
    .GridViewPagerStyle a:hover
    {
    	color:#fff;
    	text-decoration:none;
    }
    



    Regards:

    Vishwaraj Malik

    VB to C# Converter


    Please: Don't forget to click "Mark as Answer" on the post that helped you. That way future readers will know which post solved your issue.
  • Re: change color on row in gridview using event

    11-07-2009, 12:59 PM
    Answer
    • Contributor
      4,173 point Contributor
    • fayaz_3e
    • Member since 09-14-2007, 10:15 AM
    • Hyderabad
    • Posts 875

    You didnt declare 'whattype'. Declare it as string.

     Dim whattype As String = Convert.ToString(DataBinder.Eval(e.Row.DataItem, "type"));

    Fayaz
  • Re: change color on row in gridview using event

    11-07-2009, 3:09 PM

    The declare of the variable with the string statement worked !!!  Thanks.  However, it dit not work until I noticed that VS added the Handles GridView1.RowDataBound  at the end of the protected sub.  When I left that in there, it worked once I declared the string.  I am posting the code that actually worked here for anyone else that might be looking at this.

    Partial Class Eim
        Inherits System.Web.UI.Page
    
        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
                'determine the value of the type field
                Dim whattype As String = Convert.ToString(DataBinder.Eval(e.Row.DataItem, "type"))
                If whattype = "Minor" Then
                    ' color the background of the row yellow
                    e.Row.BackColor = Drawing.Color.Yellow
                End If
            End If
        End Sub
    End Class


     

Page 1 of 1 (4 items)