Solved

Datagrid SelectedIndex

Posted on 2004-04-02
11
3,237 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
  • 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
 
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
3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

 
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 125 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

3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

Question has a verified solution.

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

Suggested Solutions

Wouldn’t it be nice if you could test whether an element is contained in an array by using a Contains method just like the one available on List objects? Wouldn’t it be good if you could write code like this? (CODE) In .NET 3.5, this is possible…
Today I had a very interesting conundrum that had to get solved quickly. Needless to say, it wasn't resolved quickly because when we needed it we were very rushed, but as soon as the conference call was over and I took a step back I saw the correct …
Along with being a a promotional video for my three-day Annielytics Dashboard Seminor, this Micro Tutorial is an intro to Google Analytics API data.
Video by: Mark
This lesson goes over how to construct ordered and unordered lists and how to create hyperlinks.

861 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

Need Help in Real-Time?

Connect with top rated Experts

23 Experts available now in Live!

Get 1:1 Help Now