Click here to Skip to main content
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 k4.9K
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

Hi Laiju
 
Try this http://forums.asp.net/t/1280034.aspx?how+to+get+rowindex+of+gridview+in+javascript[^]
 
Hope it will help you.
 
Good Luck
  Permalink  

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

  Print Answers RSS
0 OriginalGriff 584
1 Maciej Los 275
2 DamithSL 233
3 Sergey Alexandrovich Kryukov 209
4 BillWoodruff 200
0 OriginalGriff 6,803
1 Sergey Alexandrovich Kryukov 6,377
2 DamithSL 5,421
3 Manas Bhardwaj 4,841
4 Maciej Los 4,330


Advertise | Privacy | Mobile
Web01 | 2.8.1411023.1 | Last Updated 25 Apr 2014
Copyright © CodeProject, 1999-2014
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