Solved

Datagrid SelectedIndex

Posted on 2004-04-02
11
3,245 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
Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

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

Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

Question has a verified solution.

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

Suggested Solutions

It seems a simple enough task, yet I see repeated questions asking how to do it: how to pass data between two forms. In this article, I will show you the different mechanisms available for you to do just that. This article is directed towards the .N…
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…
Although Jacob Bernoulli (1654-1705) has been credited as the creator of "Binomial Distribution Table", Gottfried Leibniz (1646-1716) did his dissertation on the subject in 1666; Leibniz you may recall is the co-inventor of "Calculus" and beat Isaac…
The Email Laundry PDF encryption service allows companies to send confidential encrypted  emails to anybody. The PDF document can also contain attachments that are embedded in the encrypted PDF. The password is randomly generated by The Email Laundr…

777 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