Monday, January 10, 2011

GridView Row Highlighting on Mouseover

spgv.RowCreated += new GridViewRowEventHandler(spgv_RowCreated);


void spgv_RowCreated(object sender, GridViewRowEventArgs e)
        {
            // only apply changes if its DataRow
            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                // when mouse is over the row, save original color to new attribute, and change it to highlight yellow color
                e.Row.Attributes.Add("onmouseover",
              "this.originalstyle=this.style.backgroundColor;this.style.backgroundColor='#EEEEEE';this.style.cursor='pointer'");


                // when mouse leaves the row, change the bg color to its original value   
                e.Row.Attributes.Add("onmouseout",
                "this.style.backgroundColor=this.originalstyle;this.style.cursor='default'");
                 
                // on click row, go to item detail
                e.Row.Attributes.Add("onclick", "javascript:location.href='" + ((DataRowView)e.Row.DataItem).Row.ItemArray.GetValue(6) + "'");
            }
        }

No comments:

Post a Comment