Highlight selected row and unhighligh unselected row in gridview

I wanted the selected row to be highlighted when the user click

on select link button on a gridview, so the code I have below works

exactly that. However, when I click on a different row, the new row is now

highlighed, but the hilighted stays with the previous row as well. Shouldn't the highligh be only on the seleted row? what am I doing wrong or not doing? any help would be really appreciated.

protected void gridview1_RowCommand(object sender, GridViewCommandEventArgs e)

{

if (e.CommandName.Equals("Select"))

{

index =
Convert.ToInt32(e.CommandArgument);

GridViewRow selectedRow = gridview1.Rows[index];

selectedRow.BackColor = Color.Yello;

}

}

 

 

0
ASPBlue
3/18/2009 2:59:51 PM
asp.net.presentation-controls 72751 articles. 3 followers. Follow

12 Replies
910 Views

Similar Articles

[PageSpeed] 1

I do it in the SelectedIndexChanged event and it works for me.  Something like:

GridView1.Rows[GridView1.SelectedIndex].BackColor = Color.Yellow;


C# <---> VB.Net Translator
0
MetalAsp
3/18/2009 3:34:12 PM

 Use this....

 GridViewRow row = gridview1.SelectedRow;

row.BackColor = Color.Yellow;

You can actually use this in the SelectedIndexChanged of the gridview so it fires when the selected row changes. 

 


KAMMIE
Don't forget to Mark as Answer on the post that helped you. It encourages them to share their knowledge, and it helps others to easily identify the solution.
0
kammie
3/18/2009 3:36:14 PM

Actually since you are doing postback by using a linkbutton it redraws the Grid and previous changes are lost

I have here an example using javascript see if it helps

http://www.aspsnippets.com/post/2009/03/13/Using-JavaScript-with-ASPNet-GridView-Control.aspx


MAKMark as Answer if this reply helps you
MVP ASP/ASP.Net
MVP ASP/ASP.Net
ASP.Net Hosting : Host DepotMy Site : ASPSnippets
0
mudassarkhan
3/18/2009 3:37:49 PM

MetalAsp.net, Kammie: I tried your both method, and the higligh still stuck in the previous row. How come it works for you? below is my code. The comment out portion is Kammie's method I think. the portion that is not comment out is MetalAsp.net method. I'm not sure why they are all green color when I pasted here, but you get the idea.  I'm going to try mudassarkhan's method next to see if it would work for me.

 protected void gridview1_SelectedIndexChanged(Object sender, EventArgs e)

{

//GridViewRow row = gridview1.SelectedRow;

//row.BackColor = Color.Yellow;

gridview1.Rows[gridview1.SelectedIndex].BackColor = Color.Yellow;

}

0
ASPBlue
3/18/2009 3:52:16 PM

MAK, your methods are all for check box, so how do i replace that with the command select?

0
ASPBlue
3/18/2009 4:04:20 PM

 Worst case, loop through your gridview rows, set them all to default backcolor, before you actually set the color to the selected row.


KAMMIE
Don't forget to Mark as Answer on the post that helped you. It encourages them to share their knowledge, and it helps others to easily identify the solution.
0
kammie
3/18/2009 4:06:19 PM

ASPBlue:
MAK, your methods are all for check box, so how do i replace that with the command select?

 

yeas as i said its didferent from u did

only option in ur way is to preserve some identifier that will udentify that row in session variable


MAKMark as Answer if this reply helps you
MVP ASP/ASP.Net
MVP ASP/ASP.Net
ASP.Net Hosting : Host DepotMy Site : ASPSnippets
0
mudassarkhan
3/18/2009 4:34:00 PM

Kammie, so do i put this looping code in the rowdatabound event for default color? and then leave the color highlight code in selectedindexchanged when it is clicked on? 

0
ASPBlue
3/18/2009 4:40:43 PM

Yeah, you can put the looping in the RowDataBound event and set the color in the SelectedIndexChanged event or you can put it in the same event where you actually change the color, just before it.


KAMMIE
Don't forget to Mark as Answer on the post that helped you. It encourages them to share their knowledge, and it helps others to easily identify the solution.
0
kammie
3/18/2009 4:46:51 PM

Okay, I used Kammie suggestion to loop before the changes so it works now.

Thanks a lot Kammie.

Tthe final code is below in case someone has the same question. The code in green are two ways to have it hilight it in yellow. Either one of it would work.

protected void gridview1_SelectedIndexChanged(Object sender, EventArgs e)

{

foreach (GridViewRow row in gridview1.Rows)

{

row.BackColor =
Color.Transparent;

}

/* GridViewRow row = gridview1.SelectedRow;

row.BackColor = Color.Yellow;

*/

//gridview1.Rows[gridview1.SelectedIndex].BackColor = Color.Yellow;

 

}

0
ASPBlue
3/18/2009 5:00:01 PM

I'm trying to mark Kammie answer as the answer but the status just hang and not update. This happens to me a few times now, i'm going to report to ASP site. Not sure anybody else has the same problem as I do.

0
ASPBlue
3/18/2009 5:07:56 PM

ASPBlue:
I'm trying to mark Kammie answer as the answer but the status just hang and not update. This happens to me a few times now, i'm going to report to ASP site. Not sure anybody else has the same problem as I do.

Simply create an arraylist

ArrayList arr = new ArrayList();

And keep on adding the ids for the selected rows afer changing color to arraylist

 

finally save it in Session variable

Session["rows"] = arr;

Next time when another row is seleced

Get the arraylist back

ArrayList arr = (ArrayList)Session["rows"];

And loop and recolor the old rows with the new row and add the new row to arraylist and save it back to session

 

Thats the only way you can achieve it

 


MAKMark as Answer if this reply helps you
MVP ASP/ASP.Net
MVP ASP/ASP.Net
ASP.Net Hosting : Host DepotMy Site : ASPSnippets
0
mudassarkhan
3/18/2009 7:27:15 PM
Reply:

Similar Artilces:

Selecting a gridview row and keeping the last selected row in a gridview highlighted after mouse moves over a new row.
What is the best way to show a gridview row highlighted once selected and keep it highlighted once the mouse moves over a different row? Thanks, Steve hi,you can use <SelectedRowStyle BackColor="#008A8C" Font-Bold="True" ForeColor="#FFFFFF" /> plz let me know if i misunderstood you Thanks for your help. One problem; however, is that after the row has been selected and I check the "grid.SelectedIndex" it has a value of -1 and not the index of the row that was selected on the grid. What do you recommend? Thanks, Steve hi, sorry i din...

Add new controls to a gridview row; when editing that row using the edit row feature in the gridview control.
Hi all  I have a questions that looks hard / complex to me, and i know you can help me in this. Q {  I have row in a Gridview and i have an edit option to it. I press the edit button, to edit the values in that row and update data all the way back to the database. Now when i press the edit button, i want a drop down list to be displayed that contain values for a particular field in my grid view. (for now only plain text boxes appear.) } I hope you understand my question, all i need to do is replace the text box controls with a dropdown list when i am editing a row in ...

select multiple rows with its row highlighted in GridView
Hello everyone, I'm not using any checkbox or select command to select the row in the GridView, I use the "RowDataBound" event in the GridView for the "onmouseover" click and row select.  That works fine for me but I want to have the functionality to select a multiple rows with its row highlighted and have a row selected count. For example, If I select a row, it hightlights that row and if I select another row, it will do the same thing and have the previous row still highlighted.  I have been looking for a way to implement this but can't find a ...

Gridview paging enabled. Has checkbox to select the row . If user select the checkbox on one page and move to 2nd page to select the rows there also to export the selected rows to excel. But doing so
HiI am using Gridview and that has paging enabled. Using checkbox to select the particular row of the gridview to export the selected rows to the excel . Everything is working fine except for one case.Let user selected the "check" box for, five rows which user would like to export to excel (two of rows on page 1 of results and 3 of them on page 2 of results), however it only exports the 3 row details from page 2, because it seems to de-select the 2 rows from page 1 when I move to page 2 and vice versa. The code i am using is as :protected void BtnExcel_Click(object sender, EventArg...

Selecting the multiple rows in GridView and inserting the selected rows to another Gridview & Performing some operations on it
 Dear All, I have become mad for solving this problem. Please Help me. I have a Gridview bound to a datasource. I inserted the CheckBox Control in Templet to select certain rows. Gridview1 contains some products id & name.I want to insert the selected rows in to second Gridview  and in the second Gridview I need to add two more colums AdvicedQty & AcceptedQty(TextBox Cotrol).The user will select few products in Gridview1 & clicks add which will be added another gridview then in second Gridview he enters the AdviceQty & AcceptedQty.After entering the data he cli...

Problem in Using DropDownList Control to select a selected row of SqlDataSource that is under GridView Control
Hi all,In my SQL Server Management Studio Express, I have a database "ChemDatabase" that has a table "LabData"  formatted in the following way: SampleID int PRIMARY KEY, SampleName nvarchar(25), ChemicalName, nvarchar(25), Concentration decimal(6.2) with 5 rows of records in this .dbo table. In my Visual Web Developer Express, I created a website "RPD-TCE" for extracting a row of record from the LabData table by using a DropDownList, a GridView and a SqlDataSource.  I want to click DropDownList to list one number of S...

new row below selected row in gridview to display child gridview
Hello,   I have a gridview that up selected..shows the nested child grivdview. The problem I currently have is that it stuffs all the data into a column whcih blows the whole tbale out of whack. What I need to do is create a new row below the sleected row to house the new child grivdview. I have seen third party controls do this such as teleriks radgrid but I wont to do this with the gridview control.  Any articles on how to do this? Does AJAX support this? Any help is greatly appreciated.    ...

I want to select top 3 rows except the first from 4 rows of a Table. It should select from top 2 row,top3 row and top4 row.
 Hi, Let say I've 4 rows in a table. I want to select top 3 rows except the first from 4 rows of a Table. It should select from top 2 row,top3 row and top4 row. Thanks in advance Asghar Ali Mohammed http://www.aliwebdev.com(Web Designer and Developer)Do not forget to "MARK AS ANSWER" on the post that helped you. If you can use Linq, you can use Skip. http://msdn.microsoft.com/en-us/vcsharp/aa336757.aspxThanks, EdMicrosoft MVP - ASP/ASP.NET Hii dear..U can fire the Query select Top(3) from Table1 order by id desc.. "A conclusion is whe...

Using a row selection in a gridview to create database rows for another gridview
Hi there, I have 2 gridviews on a form, the first displays some template rows from a database table.  The second displays some user specific rows (with additional data added to the template info) from the same table. They both work fine for displaying the info.  Now what I want to do, is allow users to 'select' a template row on the first gridview, and have that action automatically: create a new user-specific row, with added fields from profile data etc. insert it into the table then refresh the second gridview in order to show the new added row. C...

Insert row above selected row in GridView
Hi, I've been asked to create a list with an insert button at the end of each row that, when clicked, will insert a new line above the selected row and allow the user to enter in information.  It won't be an edit, but a new entry - however it needs to be displayed above the row with the "Insert" button.  I'm not sure if this is possible or not, but any help would be most appreciated. Thanks! Check this articlehttp://fredrik.nsquared2.com/viewpost.aspx?PostID=201Hope my suggestion helps Regards,Vinz"Code, Beer and Music" that's my way of ...

Data Controls insert rows into gridview then insert rows from gridview into SQL Table
Hello there, I'm still new at performing miracles with code . Currently I am having trouble with my latest coding adventure I have a web application which records jobs and labour assigned to jobs. The issue in particular I'm having is the way in which labour is to be assigned. I want to use standard controls (Textbox, Dropdownlist) to record the values into a gridview (ie,put multiple rows into the gridview using the controls). Then, when all the inserting is complete, the user clicks a submit button to insert all the rows from the gridview into a database table. The reason for...

selecting row in gridview by clicking anywhere in row (not just on 'select' link)
Hi there- I would like my gridview to behave such that a click anywhere in the (whole length of the) row would act just like clicking the "select" column link. Anyone have any code or suggestions?   You could use: ( but you would also need to disable event validation ) <%@ Page Language="VB" EnableEventValidation="false"%> Protected Sub GridView1_RowCreated(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewRowEventArgs) If e.Row.RowType = DataControlRowType.DataRow Then e.Row.Attributes.Add("onclick", "javascript:__doPostBack('GridView1','select$" &...

Iwant to select a row and column in gridview with select radiobutton. but No checked change event.below put a button .if click that button to seleced row values will select
I want to select a row and column in gridview using radiobutton in gridview . if which radio button is select that row will select and i want that row column values in outside individual textboxes of next page.  but in that only single radiobutton will select and no post back="true" bcz iam not using checked change property. i inserted button below gridview. if i click the that selected row and columns values are stored into textboxes of next page im using this code .but im getting one column in that row. but i want all columns in that selected row so plz repla...

Highlighting rows of gridview selection
I have a gridview that I would like to be able to highlight a row's background yellow with a single click and have access to one of the currently selected row's fields.  How can I do this? Use the selectedindextemplate to define the style of the selected row in the grid. Also when the button is clikced make the row selected http://www.netomatix.com/development/gridviewrowselectedstyle.aspxVikram www.vikramlakhotia.comPlease mark the answer if it helped you HiUse this code in your gridview tag and you can set any colour font etc.,<SelectedRowStyle ForeColor="AliceBlu...

Web resources about - Highlight selected row and unhighligh unselected row in gridview - asp.net.presentation-controls

Resources last updated: 11/26/2015 4:46:32 AM