?
Solved

asp.net Gridview

Posted on 2012-08-31
18
Medium Priority
?
708 Views
Last Modified: 2013-12-04
I want to do one of two things...
A: Use the select button from Gridview control, populate a text box with the DataKeyName of the selected it. Use the text box to control what appears on four other DetailsView controls.

B: Use two text boxes, either of which may be populated or not, to filter the records in the Gridview control. Use the Select button to select a record. The DataKeyName for that selected item will then control what appears on four other DetailsView controls.


I attempted A and got to this...

 Protected Sub GridViewAllSelectedIndexChanged(sender As Object, e As EventArgs) Handles GridViewAll.SelectedIndexChanged
            Dim row As GridViewRow = GridViewAll.SelectedRow
            Dim strfirst As String = GridViewAll.SelectedRow.Cells(2).Text
            Dim strmiddle As String = GridViewAll.SelectedRow.Cells(3).Text
            Dim strlast As String = GridViewAll.SelectedRow.Cells(1).Text
            txtSelectedPerson.Text = strlast & ", " & strfirst & " " & strmiddle
            strItemSelected.Text = GridViewAll.SelectedRow.Cells(0).Text
        End Sub

But nothing happens when I click the Select button in the GridView control.
0
Comment
Question by:rawilken
  • 11
  • 5
16 Comments
 
LVL 36

Expert Comment

by:Miguel Oz
ID: 38356351
You need to get row from sender as follows:
C# -(GridViewRow)(((Control)sender).NamingContainer);
Dim row As GridViewRow = CType(CType(sender, Control).NamingContainer, GridViewRow)

Open in new window

Note: I use CType but you can use DirectCast too.
0
 

Author Comment

by:rawilken
ID: 38357556
Thanks, but can I get it in VB? I am not familiar with C# yet.
0
 

Author Comment

by:rawilken
ID: 38357558
Is there an event that fires before the selectedindexchanged event that I need to include?
0
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
LVL 36

Expert Comment

by:Miguel Oz
ID: 38359569
VB.NET code to get the row:
Protected Sub GridViewAllSelectedIndexChanged(sender As Object, e As EventArgs) Handles GridViewAll.SelectedIndexChanged
            Dim row As GridViewRow = CType(CType(sender, Control).NamingContainer, GridViewRow)
            Dim strfirst As String = row.Cells(2).Text
            Dim strmiddle As String = row.Cells(3).Text
            Dim strlast As String = row.Cells(1).Text
            txtSelectedPerson.Text = strlast & ", " & strfirst & " " & strmiddle
            strItemSelected.Text = row.Cells(0).Text
        End Sub

Open in new window

Note: I assume your cells exist.

Regarding post question about selectedindexchanged, have a look at MSDN:
http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.gridview.selectedindexchanged.aspx#Y0

If you need more post your markup and code in a separate question. I thought your issue was only the method in the original question
0
 

Author Comment

by:rawilken
ID: 38361272
mas_oz, thanks, but I am getting an error because the GridView control is contained in an ajaxtoolkit:tabcontainer...
error message

Also, I did see MSDN page, but using Dim row As GridViewRow = GridViewAll.SelectedRow failed. Nothing seemed to work.
0
 

Author Comment

by:rawilken
ID: 38361284
This is what I have tried..

Tried both Dim row statements, one at a time...

Protected Sub GridViewAllSelectedIndexChanged(sender As Object, e As EventArgs) Handles GridViewAll.SelectedIndexChanged
            Dim row As GridViewRow = TabContainer1.tabEmployeeList.GridViewAll.SelectedRow
            'Dim row As GridViewRow = CType(CType(sender, Control).NamingContainer, GridViewRow)
            Dim strfirst As String = row.Cells(2).Text
            Dim strmiddle As String = row.Cells(3).Text
            Dim strlast As String = row.Cells(1).Text
            txtSelectedPerson.Text = strlast & ", " & strfirst & " " & strmiddle
            txtSelectedPerson.Visible = True
            lblSelectedPerson.Visible = True
            tabEmployeeData.Focus()
        End Sub
0
 
LVL 36

Expert Comment

by:Miguel Oz
ID: 38366092
Please post:
-  your markup
- code behind.
- Quick watch screenshots of sender object contents and CType(sender, Control).NamingContainer.

Note: Sender should contain a reference to the gridview row if the event is associated with the gridview.
0
 

Author Comment

by:rawilken
ID: 38396877
I was on vacation. I am now back and looking to resolve this. Here are the error message, code behind, and markup
Markup.JPG
0
 

Author Comment

by:rawilken
ID: 38396879
markup for html to display cell values...


<asp:Label ID="lblSelectedPerson" runat="server" Text="Details for " 
        Visible="False" BackColor="Silver"></asp:Label>
    <asp:TextBox ID="txtSelectedPerson" runat="server" BorderStyle="None"
        Visible="False" Width="338px" BackColor="Silver"></asp:TextBox>
0
 

Author Comment

by:rawilken
ID: 38396979
Grid

<asp:GridView ID="GridViewAll" runat="server" AllowSorting="True"
                    AutoGenerateColumns="False" DataSourceID="EmployeesDataSource1"
                    AllowPaging="True" DataKeyNames="SSNO" OnRowDataBound="GridViewDataBoundEventHandler" CellPadding="5" Height="160px">
                    <PagerSettings Mode="NextPreviousFirstLast" FirstPageText="First" LastPageText="Last" NextPageText="Next" PreviousPageText="Previous"></PagerSettings>
                    <Columns>
                        <asp:CommandField ShowSelectButton="True"  ButtonType="Button"/>
                        <asp:BoundField DataField="[SSNO]" HeaderText="SSNO" SortExpression="[SSNO]" Visible="False" />
                        <asp:BoundField DataField="LastName" HeaderText="Last Name" SortExpression="LastName" />
                        <asp:BoundField DataField="FirstName" HeaderText="First Name" SortExpression="FirstName" />
                        <asp:BoundField DataField="MName" HeaderText="Middle Name" SortExpression="MName" />
                        <asp:BoundField DataField="PHONE1" HeaderText="Phone 1" SortExpression="PHONE1" />
                        <asp:BoundField DataField="PHONE2" HeaderText="Phone 2" SortExpression="PHONE2" />
                        <asp:BoundField DataField="Pager" HeaderText="Cell/Pager" SortExpression="Pager" />
                        <asp:BoundField DataField="Shift" HeaderText="Shift" SortExpression="Shift" />
                        <asp:BoundField DataField="Super" HeaderText="Supervisor" SortExpression="Supervisor" />
                    </Columns>
                    <SelectedRowStyle BackColor="Gainsboro" />
                </asp:GridView>
0
 

Author Comment

by:rawilken
ID: 38396982
Clicking select button does not display any values of the row selected.
0
 

Author Comment

by:rawilken
ID: 38396992
What I want to do is that when a record is selected, I want the values for the fields FirstName, LastName and MName to be displayed above the AjaxTabControl so that the user know which record has been selected when they go to the other tabs. The select button is on the first tab.
0
 
LVL 36

Expert Comment

by:Miguel Oz
ID: 38397052
Notice that the gridview markup does not define the event.
You should have the following attribute:
onselectedindexchanged="GridViewAllSelectedIndexChanged"

Let us know where the event is defined. For further info you can check:
http://stackoverflow.com/questions/9166568/gridview-onselectedindexchanged-event-not-firing
0
 

Author Comment

by:rawilken
ID: 38399057
It has been defined from the beginning...here is the full function...

Protected Sub GridViewAllSelectedIndexChanged(sender As Object, e As EventArgs) Handles GridViewAll.SelectedIndexChanged
            'Dim row As GridViewRow = GridViewAll.SelectedRow
            'Dim row As GridViewRow = CType(CType(sender, Control).NamingContainer, GridViewRow)
            'Dim strfirst As String = row.Cells(2).Text
            'Dim strmiddle As String = row.Cells(3).Text
            'Dim strlast As String = row.Cells(1).Text
            'txtSelectedPerson.Text = strlast & ", " & strfirst & " " & strmiddle
            'txtSelectedPerson.Visible = True
            'lblSelectedPerson.Visible = True
            'tabEmployeeData.Focus()
        End Sub

I have commented out the code for now, but it just does nothing. I have tried two ways of getting the row number from the selected row, both to no avail.
0
 

Author Comment

by:rawilken
ID: 38400167
Here is a screenshot of the GridView control
GridView.JPG
0
 
LVL 36

Accepted Solution

by:
Miguel Oz earned 750 total points
ID: 38400851
Ok, you need to define in the markup
Add the following attribute to the grideview: (You can locate after OnRowDataBound attribute)
onselectedindexchanged="GridViewAllSelectedIndexChanged"

and change method declaration in code behind as: (Notice that I do not use Handles)
Protected Sub GridViewAllSelectedIndexChanged(sender As Object, e As EventArgs)

If more help required please include the following info:
- Complete markup  and code behind as text file.
- Quick watch screenshots of sender object contents and CType(sender, Control).NamingContainer.
0

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

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

A theme is a collection of property settings that allow you to define the look of pages and controls, and then apply the look consistently across pages in an application. Themes can be made up of a set of elements: skins, style sheets, images, and o…
Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
This is Part 3 in a 3-part series on Experts Exchange to discuss error handling in VBA code written for Excel. Part 1 of this series discussed basic error handling code using VBA. http://www.experts-exchange.com/videos/1478/Excel-Error-Handlin…
Whether it be Exchange Server Crash Issues, Dirty Shutdown Errors or Failed to mount error, Stellar Phoenix Mailbox Exchange Recovery has always got your back. With the help of its easy to understand user interface and 3 simple steps recovery proced…

850 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