Click here to Skip to main content
11,640,445 members (64,299 online)
Rate this: bad
good
Please Sign up or sign in to vote.
I have to change the color of the selected index row of the grid.
I am binding the grid dynamically from the database using SP.
Posted 24-Apr-14 23:36pm
Laiju k5.6K
Edited 25-Apr-14 17:31pm
v2
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

1.Through javascript:
function ChangeRowColor(rowID)
{
var color = document.getElementById(rowID).style.backgroundColor;
if(color != '#6aafaf')
oldColor = color;
if(color == '#6aafaf')
	document.getElementById(rowID).style.backgroundColor = oldColor;
else 
	document.getElementById(rowID).style.backgroundColor = '#6aafaf';
}
Call this function in GridView Row create event
protected void GridView1_RowCreated(object sender, GridViewRowEventArgs e)
{
string rowID = String.Empty;
if (e.Row.RowType == DataControlRowType.DataRow)
{
rowID = "row"+e.Row.RowIndex;
e.Row.Attributes.Add("id","row"+e.Row.RowIndex);
e.Row.Attributes.Add("onclick","ChangeRowColor(" +"'" + rowID + "'" + ")");
}
}
2.Through coding
make a function in code behind
protected void ChangeRowColor(object sender, EventArgs e)
{
    foreach (GridViewRow row in GridView1.Rows)
    {
        if (row.RowIndex == GridView1.SelectedIndex)
        {
            row.BackColor = ColorTranslator.FromHtml("#6aafaf");
        }
        else
        {
            row.BackColor = ColorTranslator.FromHtml("#93acae");
        }
    }
}
call this function on Gridview event
<asp:gridview id="GridView1" runat="server" autogeneratecolumns="false" onselectedindexchanged="ChangeRowColor" xmlns:asp="#unknown"></asp:gridview>
Hope this help.
Thanks
  Permalink  
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 2

  Permalink  

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
0 CPallini 239
1 Suvendu Shekhar Giri 215
2 Mika Wendelius 210
3 OriginalGriff 165
4 Sergey Alexandrovich Kryukov 139


Advertise | Privacy | Mobile
Web01 | 2.8.150731.1 | Last Updated 25 Apr 2014
Copyright © CodeProject, 1999-2015
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100