Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

Datagrid SelectedIndex

Posted on 2004-04-02
11
Medium Priority
?
3,285 Views
Last Modified: 2011-09-20
Hi,

I have a bound datagrid. I need to be able to select a row by clicking any of the cells and have it fire of the SelectedIndexChanged event.

How can I do this?

So far, all I've found is that I need a button column (Select), but that means I have to click that specific cell to trigger of an event.

Thanks
0
Comment
Question by:adathelad
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 6
  • 5
11 Comments
 
LVL 7

Expert Comment

by:culshaja
ID: 10740014
I don't think that there is a way as such. You could use a repeater control or a datalist and wrap everything in a row in a span or div tag and assign a javascript onclick event to it.

James :-)
0
 
LVL 7

Expert Comment

by:culshaja
ID: 10740025
Hmm,

Having just read the last bit of your question relating to capturing which cell was clicked, intead of using autogenrated columns in your DataGRid you could use template columns, and you could make very column contain a link button rather than a textbox. You can the capture exactly which link button was clicked (an therefor which cell) in the ItemCommand event of the datagrid.

James :-)
0
 
LVL 7

Expert Comment

by:culshaja
ID: 10740044
Example code for a tenmplate column with a LinkButton:

<asp:TemplateColumn>
                                                            <ItemTemplate>
                                                                  <asp:LinkButton id="lbView" runat="server" CommandArgument="V">View</asp:LinkButton>
                                                            </ItemTemplate>
                                                            <FooterTemplate>
                                                                  <asp:Button id="cmdAddApplication" runat="server" Text="Add" CommandArgument="A"></asp:Button>
                                                            </FooterTemplate>
                                                      </asp:TemplateColumn>

You can then set the CommandArgument or the CommandValue to be the column that the button is in so you can work out which column was clicked in. The get the row you just use e.item.itemindex in the ItemCommand event of the datagrid (e is an event parameter). e.Item is the actual linkbutton that was clicked.

James :-)
0
NFR key for Veeam Backup for Microsoft Office 365

Veeam is happy to provide a free NFR license (for 1 year, up to 10 users). This license allows for the non‑production use of Veeam Backup for Microsoft Office 365 in your home lab without any feature limitations.

 
LVL 23

Author Comment

by:adathelad
ID: 10740053
But then all the data will be links and you have to click the actual text rather than anywhere in the cell.

Ok, I'm going down the route of having an extra column "select" at the end of each row which allows me to select a row.
This is fine, and highlights the selected row in Yellow.

What want to do now is to be able to unselect a row once it's been selected (i.e. revert it back to a white background). Is this possible? Ideally I don't want another new column "Unselect"....I just want to use the one "Select" column to select/deselect a row.

Thanks for your help.
0
 
LVL 7

Expert Comment

by:culshaja
ID: 10740061
Why not just add a checkbox to the row, and then if they click the checkbox it is selected. You can set autopostback to true on the checkbox to allow you to change the row colour.

James :-)
0
 
LVL 23

Author Comment

by:adathelad
ID: 10740088
I'd rather not use a checkbox.....and I only want one row to be selected at a time.

I really need an ONCLICK event for the row, that would be ideal.

I know I can set the CommandName property and catch that in the ItemCommand event. Is it possible to do something like:

Select Case e.CommandName
     Case "select" Then
          '-- Code here to highlight the row + other stuff
          SET THE COMMAND NAME OF THE LINK BUTTON TO "unselect"

     Case "unselect"
          '-- Code here to unhighlight the row + other stuff
          SET THE COMMAND NAME OF THE LINK BUTTON TO "select"
End Select
0
 
LVL 7

Accepted Solution

by:
culshaja earned 500 total points
ID: 10740106
Yes,

you can do that by using the FindControl method on e.Item. You pass in the control name and cast it to a linkbitton and you can then change any of the properties on that control that are not read-only.

James :-)
0
 
LVL 23

Author Comment

by:adathelad
ID: 10740114
I've found I can do it:

If e.CommandName.ToLower = "select" Then
     CType(e.CommandSource, LinkButton).CommandName = "unselect"
Else
     CType(e.CommandSource, LinkButton).CommandName = "select"
End If
0
 
LVL 7

Expert Comment

by:culshaja
ID: 10740131
Cool,

never knew about CommandSource. That'll save some coding.

James :-)
0
 
LVL 23

Author Comment

by:adathelad
ID: 10740212
This is what I ended up with and is now working:

If e.CommandName.ToLower = "select" Then
     CType(e.CommandSource, LinkButton).CommandName = "unselect"
     
     If Not (CType(source, DataGrid).SelectedItem) Is Nothing Then
          '-- There is already a row selected.
          '-- Unhighlight it
          CType(source, DataGrid).SelectedItem.BackColor = Color.White
          '-- Reset that row's link button action to "select"
          CType(CType(source, DataGrid).SelectedItem.Cells(15).Controls(0), LinkButton).CommandName = "select"
     End If

     '-- SELECTEDINDEXCHANGED event then fires
     '-- This event handler then highlights the new selected row
Else
     '-- Unhighlight selected row
     CType(e.CommandSource, LinkButton).CommandName = "select"
     CType(source, DataGrid).SelectedItem.BackColor = Color.White
End If
0
 
LVL 23

Author Comment

by:adathelad
ID: 10740219
Thanks for your help. You moved me onto the right track so the points are yours
0

Featured Post

New feature and membership benefit!

New feature! Upgrade and increase expert visibility of your issues with Priority Questions.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In my previous article (http://www.experts-exchange.com/Programming/Languages/.NET/.NET_Framework_3.x/A_4362-Serialization-in-NET-1.html) we saw the basics of serialization and how types/objects can be serialized to Binary format. In this blog we wi…
Many of us here at EE write code. Many of us write exceptional code; just as many of us write exception-prone code. As we all should know, exceptions are a mechanism for handling errors which are typically out of our control. From database errors, t…
In response to a need for security and privacy, and to continue fostering an environment members can turn to for support, solutions, and education, Experts Exchange has created anonymous question capabilities. This new feature is available to our Pr…
Please read the paragraph below before following the instructions in the video — there are important caveats in the paragraph that I did not mention in the video. If your PaperPort 12 or PaperPort 14 is failing to start, or crashing, or hanging, …

610 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question