?
Solved

Datagrid SelectedIndex

Posted on 2004-04-02
11
Medium Priority
?
3,279 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
Get your Disaster Recovery as a Service basics

Disaster Recovery as a Service is one go-to solution that revolutionizes DR planning. Implementing DRaaS could be an efficient process, easily accessible to non-DR experts. Learn about monitoring, testing, executing failovers and failbacks to ensure a "healthy" DR environment.

 
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

How To Install Bash on Windows 10

Windows’ budding partnership with Canonical has certainly led to some great improvements. One of them being the ability to use Bash on your Windows machine without third party applications! This might be one of the greatest things a cloud engineer in a Windows environment can do!

Question has a verified solution.

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

This article describes relatively difficult and non-obvious issues that are likely to arise when creating COM class in Visual Studio and deploying it by professional MSI-authoring tools. It is assumed that the reader is already familiar with the cla…
In my previous two articles we discussed Binary Serialization (http://www.experts-exchange.com/A_4362.html) and XML Serialization (http://www.experts-exchange.com/A_4425.html). In this article we will try to know more about SOAP (Simple Object Acces…
Add bar graphs to Access queries using Unicode block characters. Graphs appear on every record in the color you want. Give life to numbers. Hopes this gives you ideas on visualizing your data in new ways ~ Create a calculated field in a query: …
In this video, Percona Director of Solution Engineering Jon Tobin discusses the function and features of Percona Server for MongoDB. How Percona can help Percona can help you determine if Percona Server for MongoDB is the right solution for …
Suggested Courses

800 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