Click here to Skip to main content
11,417,390 members (49,524 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 25-Apr-14 0:36am
Laiju k5.1K
Edited 25-Apr-14 18: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 Sascha Lefévre 340
1 Abhinav S 248
2 Sergey Alexandrovich Kryukov 245
3 OriginalGriff 235
4 Richard Deeming 160
0 Sergey Alexandrovich Kryukov 9,345
1 OriginalGriff 7,712
2 Maciej Los 3,758
3 Abhinav S 3,601
4 Sascha Lefévre 3,198


Advertise | Privacy | Mobile
Web04 | 2.8.150427.4 | 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