Question

I have an asp.net page with c# code-behind. I have an event that is triggered in c# when the selected index on a gridview changes... This gridview is bound to an entites-data-source, and I need to find a way to tell my code-behind the id of the object that was selected when it calls the selected_index_changed() method. Any thoughts on how best to do this?

Current event handler code:

protected void VehiclesGridView_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (ChangeAttemptedId && !IsSavedId)
            {
                Alert.Show("Dispatch assignment saved... (But you forgot to click Confirm or Cancel!)");
            }
            IsSavedId = false;
            ChangeAttemptedId = true;
            int selectedIndex = VehiclesGridView.SelectedIndex + 1;
            getNextRide(selectedIndex); //TODO: FIX 
        }

ASP.NET Code:

<asp:EntityDataSource ID="VehiclesEDS" runat="server" EnableDelete="True" 
        EnableFlattening="False" EnableInsert="True" EnableUpdate="True" 
        EntitySetName="Vehicles" ContextTypeName="RamRideOps.RamRideOpsEntities" >
    </asp:EntityDataSource>

<asp:UpdatePanel ID="SelectCarUP" runat="server" UpdateMode="Conditional">
        <ContentTemplate>
            <asp:GridView ID="VehiclesGridView" runat="server" AllowPaging="True" 
                AllowSorting="True" DataSourceID="VehiclesEDS" AutoGenerateColumns="False" 
                onselectedindexchanged="VehiclesGridView_SelectedIndexChanged" 
                BackColor="White" BorderColor="#999999" BorderStyle="None" BorderWidth="1px" 
                CellPadding="3" GridLines="Vertical" ShowHeaderWhenEmpty="True" AutoPostBack="True">
                <AlternatingRowStyle BackColor="#DCDCDC" />
                <Columns>
                    <asp:TemplateField ShowHeader="False">
                        <ItemTemplate>
                            <asp:LinkButton ID="GVSelectButton" runat="server" CausesValidation="False" 
                                CommandName="Select" Text="Select"></asp:LinkButton>
                        </ItemTemplate>
                    </asp:TemplateField>
                    <asp:BoundField DataField="CarNum" HeaderText="Car" ReadOnly="True" 
                        SortExpression="CarNum" />
                    <asp:BoundField DataField="CurrPassengers" HeaderText="Passengers" 
                        ReadOnly="True" SortExpression="CurrPassengers" />
                    <asp:BoundField DataField="MaxPassengers" HeaderText="Capacity" ReadOnly="True" 
                        SortExpression="MaxPassengers" />
                    <asp:BoundField DataField="Status" HeaderText="Status" ReadOnly="True" 
                        SortExpression="Status" />
                    <asp:BoundField DataField="StartAdd" HeaderText="Pick-Up Address" 
                        ReadOnly="True" SortExpression="StartAdd" />
                    <asp:BoundField DataField="EndAdd" HeaderText="Drop-Off Address" 
                        ReadOnly="True" SortExpression="EndAdd" />
                    <asp:BoundField DataField="AvgRideTime" HeaderText="Avg. Ride Time" 
                        ReadOnly="True" SortExpression="AvgRideTime" />
                </Columns>
                <FooterStyle BackColor="#CCCCCC" ForeColor="Black" />
                <HeaderStyle BackColor="#004812" Font-Bold="True" ForeColor="White" />
                <PagerStyle BackColor="#999999" ForeColor="Black" HorizontalAlign="Center" />
                <RowStyle BackColor="#EEEEEE" ForeColor="Black" />
                <SelectedRowStyle BackColor="#C6940D" Font-Bold="True" ForeColor="White" />
                <SortedAscendingCellStyle BackColor="#F1F1F1" />
                <SortedAscendingHeaderStyle BackColor="#C6940D" />
                <SortedDescendingCellStyle BackColor="#CAC9C9" />
                <SortedDescendingHeaderStyle BackColor="#9F770B" />
            </asp:GridView>
        </ContentTemplate>
    </asp:UpdatePanel>

Error when changing EventArgs e to GridViewSelectEventArgs:

Compilation Error

Description: An error occurred during the compilation of a resource required to service this request. Please review the following specific error details and modify your source code appropriately. 

Compiler Error Message: CS0123: No overload for 'VehiclesGridView_SelectedIndexChanged' matches delegate 'System.EventHandler'

Source Error:


Line 90:     <asp:UpdatePanel ID="SelectCarUP" runat="server" UpdateMode="Conditional">
Line 91:         <ContentTemplate>
Line 92:             <asp:GridView ID="VehiclesGridView" runat="server" AllowPaging="True" 
Line 93:                 AllowSorting="True" DataSourceID="VehiclesEDS" AutoGenerateColumns="False" 
Line 94:                 onselectedindexchanged="VehiclesGridView_SelectedIndexChanged" 
Was it helpful?

Solution

If the parameter that you want to pass to getNextRide is indeed the same as selected index, then i'd make an event handler like this

    protected void VehiclesGridView_SelectedIndexChanged(object sender, GridViewSelectEventArgs e)
    {
        if (ChangeAttemptedId && !IsSavedId)
        {
            Alert.Show("Dispatch assignment saved... (But you forgot to click Confirm or Cancel!)");
        }
        IsSavedId = false;
        ChangeAttemptedId = true;

        int selectedIndex = e.NewSelectedIndex;
        getNextRide(selectedIndex); //TODO: FIX             
    }

also, inside your event handler, you can access the individual members of your grid view like so: VehiclesGridView.Rows[e.NewSelectedIndex].Cells[i] where i is the index of your cell.

Also, can you post the line where you set the datasource of VehiclesGridView, so that i might be able to come up with a cleaner answer

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top