Link to home
Start Free TrialLog in
Avatar of rawilken
rawilkenFlag for United States of America

asked on

asp.net Gridview

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.
Avatar of Miguel Oz
Miguel Oz
Flag of Australia image

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.
Avatar of rawilken

ASKER

Thanks, but can I get it in VB? I am not familiar with C# yet.
Is there an event that fires before the selectedindexchanged event that I need to include?
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
mas_oz, thanks, but I am getting an error because the GridView control is contained in an ajaxtoolkit:tabcontainer...
User generated image

Also, I did see MSDN page, but using Dim row As GridViewRow = GridViewAll.SelectedRow failed. Nothing seemed to work.
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
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.
I was on vacation. I am now back and looking to resolve this. Here are the error message, code behind, and markup
Markup.JPG
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>
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>
Clicking select button does not display any values of the row selected.
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.
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
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.
Here is a screenshot of the GridView control
GridView.JPG
ASKER CERTIFIED SOLUTION
Avatar of Miguel Oz
Miguel Oz
Flag of Australia image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial