Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations wOOdy-Soft on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Make Gridview column selectable 1

Status
Not open for further replies.

ksbigfoot

Programmer
Apr 15, 2002
856
CA
I am using VS 2005 Standard edition.
I have a Gridview in which I set the "Enable Selection".
An extra column shows up with "Select" as the word to select, which brings up my detailsview grid.

How would I make one of my columns to be the selectable link instead of the word "Select"?
 
Howdy ca8msm,
I am not sure what you mean.

Here is some of the code I have in my ASP.NET page
Code:
    <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" DataSourceID="AccessDataSource1" AllowPaging="True" AllowSorting="True" DataKeyNames="ProductID">
        <Columns>
            <asp:CommandField ShowSelectButton="True" />
            <asp:BoundField DataField="ProductID" HeaderText="ProductID" InsertVisible="False"
                SortExpression="ProductID" />
            <asp:BoundField DataField="ProductName" HeaderText="ProductName" SortExpression="ProductName" />
            <asp:BoundField DataField="CompanyName" HeaderText="CompanyName" SortExpression="CompanyName" />
            <asp:BoundField DataField="CategoryName" HeaderText="CategoryName" SortExpression="CategoryName" />
            <asp:BoundField DataField="UnitPrice" HeaderText="UnitPrice" SortExpression="UnitPrice" />
            <asp:BoundField DataField="UnitsInStock" HeaderText="UnitsInStock" SortExpression="UnitsInStock" />
        </Columns>
    </asp:GridView>
 
Instead of this:
Code:
<asp:CommandField ShowSelectButton="True" />
try:
Code:
<asp:ButtonField ButtonType="Link" Text="Hello!" />
Or have I misundertood? Do you actually want the whole row to be selectable (i.e. the user can click anywhere on a particular row and it will be selected).?


____________________________________________________________
Mark,
[URL unfurl="true"]http://aspnetlibrary.com[/url]

Need help finding an answer? Try the Search Facility or read FAQ222-2244.
 
Not the whole row, but just the ProductID to be selectable.
This way if I am pressed for space I don't have to add an extra column.
 
Code:
<asp:ButtonField ButtonType="Link" DataTextField="ProductId" DataTextFormatString="{0:n}" />
of course formatting is optional

Jason Meckley
Programmer
Specialty Bakers, Inc.
 
Howdy jmeckley,
Your way has the field as underlined, but when I click on it, my detailsview grid is not showing up.
Do I have to add in extra code to make it show up?
 
yes, that would happend in the codebehind on the SelectedIndexChanged event for the gridview. If you don't use a datasource control you will need to define the SelectedIndexChanging event as well. this is very simple
set [tt]MyGridView.SelectedIndex = e.NewIndex;[/tt] there is also a cancel property to cancel the changing of the index if you want to.

the text is underlined because it's a link. you can use CSS to adjust as necessary.

Jason Meckley
Programmer
Specialty Bakers, Inc.
 
I am getting the following error message in this code:
'NewIndex' is not a member of 'System.EventArgs'
Code:
Protected Sub GridView1_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles GridView1.SelectedIndexChanged
        GridView1.SelectedIndex = e.NewIndex
    End Sub
 
this is the Changed event. the Changing event has the NewSelectedIndex property.

each event on the Grid/Details...View has a before and after event.
the before event gives you a chance to cancel the action, while the after event will acutally do the action if the action was not canceled.

Jason Meckley
Programmer
Specialty Bakers, Inc.
 
Sorry that I am not getting this, but I changed the event to the following code:

Code:
    Protected Sub GridView1_SelectedIndexChanging(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewSelectEventArgs) Handles GridView1.SelectedIndexChanging
        GridView1.SelectedIndex = e.NewSelectedIndex
    End Sub

When I click on the link I still don't see the DetailsView.

I added back in the 'Select' option and it works, so I was checking the difference and when I move my mouse over each link I get this

Over the 'Select' option: javascript: __doPostBack('ctl00$ContentPlaceHolder1$GridView1', 'Select$0')
And then over the ProductID link: javascript: __doPostBack('ctl00$ContentPlaceHolder1$GridView1', '$0')

So the only difference is the 'Select$0' instead of '$0'.

Is there a way I can add in that word to my link?
 
now that you have changed the selected index you still need to bind the data to details view. this should happen in the SelectedIndexChanged event.

Jason Meckley
Programmer
Specialty Bakers, Inc.
 
I put a break in both the SelectedIndexChanged and SelectedIndexChanging events so that I can trace through what is happening.
When I click on my link, it never stops at those events.

Here is the line of code I was using:
Code:
<asp:ButtonField ButtonType="Link" DataTextField="ProductID" />

 
if there is a command name property try setting it
Code:
<asp:ButtonField ButtonType="Link" DataTextField="ProductID" CommandName="Select" />
the gridview will automatically hook up events if the command name i set. Edit, Delete, Update, and Cancel also work this way.

Jason Meckley
Programmer
Specialty Bakers, Inc.
 
Howdy Jason,

If I could give you two stars I would. I took out the events and added the CommandName="Select" to the ButtonField like you suggested. It works just like I want now.

Thanks again.
ksbigfoot
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top