0

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" 
4
  • I had to do this recently, and I just applied the new index to the collection that the GridView was databound to. Jan 24, 2012 at 19:49
  • If I understand correctly, this could work, if I add a 'referenceIndex' field to the db for the selected object, and then bind it from within the aspx page based on the selected index..do you know what code to use to do this binding?
    – SHeinema
    Jan 24, 2012 at 20:02
  • I'd expect your eventargs object that you pass into the event handler to have a property representing the new index. no changes to the db should be required. Can you post the code of your existing method handler? Jan 24, 2012 at 20:19
  • Sure, posted above. Perhaps I am still a little unclear about what you meant by 'applying the new index to the collection'.
    – SHeinema
    Jan 24, 2012 at 20:55

1 Answer 1

1

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

3
  • Ok, I think I'm starting to get somewhere, but it gives me a compilation error (posted above) when I try to use GridViewSelectEventArgs instead of EventArgs, and it doesn't let me cast either. I posted more code above. Also, do I correctly understand that "VehiclesGridView.Rows[e.NewSelectedIndex].Cells[i]" will allow me to access the "carNumber" associated with the particular row selected? This is the value I need. Thanks for the help ( :
    – SHeinema
    Jan 25, 2012 at 1:40
  • If you carNumber is displayed in your gridview, then it should. Jan 25, 2012 at 15:06
  • @SHeinema okay, you were right, GridViewSelectEventArgs is for a different event handler, (OnSelectedIndexChanging) You should still be able to use VehiclesGridView.SelectedRow.Cells[i] Jan 25, 2012 at 15:13

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.