Solved

Datagrid SelectedIndex

Posted on 2004-04-02
11
3,226 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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Enabling OSINT in Activity Based Intelligence

Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

 
LVL 23

Author Comment

by:adathelad
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
Cool,

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

James :-)
0
 
LVL 23

Author Comment

by:adathelad
Comment Utility
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
Comment Utility
Thanks for your help. You moved me onto the right track so the points are yours
0

Featured Post

Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

Join & Write a Comment

For those of you who don't follow the news, or just happen to live under rocks, Microsoft Research released a beta SDK (http://www.microsoft.com/en-us/download/details.aspx?id=27876) for the Xbox 360 Kinect. If you don't know what a Kinect is (http:…
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…
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…
This tutorial demonstrates a quick way of adding group price to multiple Magento products.

762 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

15 Experts available now in Live!

Get 1:1 Help Now