Click here to Skip to main content
15,892,199 members
Please Sign up or sign in to vote.
2.60/5 (3 votes)
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
Updated 25-Apr-14 17:31pm
v2

1.Through javascript:
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
C#
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
C#
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
C#
<asp:gridview id="GridView1" runat="server" autogeneratecolumns="false" onselectedindexchanged="ChangeRowColor" xmlns:asp="#unknown"></asp:gridview>

Hope this help.
Thanks
 
Share this answer
 
 
Share this answer
 

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



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900