ASP.NET:从选定的gridview项-linq中获取数据到实体数据源

本文关键字:获取 -linq 数据 数据源 实体 gridview NET ASP | 更新日期: 2023-09-27 18:21:22

我有一个包含c#代码的asp.net页面。我有一个事件,当网格视图上所选的索引发生更改时,它会在c#中触发。。。这个gridview绑定到一个实体数据源,我需要找到一种方法来告诉我的代码在调用selected_index_changed()方法时所选对象的id后面。你对如何最好地做到这一点有什么想法吗?

当前事件处理程序代码:

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代码:

<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>

将EventArgs e更改为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" 

ASP.NET:从选定的gridview项-linq中获取数据到实体数据源

如果要传递给getNextRide的参数确实与所选索引相同,那么我会制作一个类似的事件处理程序

    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             
    }

此外,在事件处理程序中,您可以访问网格视图的各个成员,如下所示:VehiclesGridView.Rows[e.NewSelectedIndex].Cells[i],其中i是单元格的索引。

此外,您是否可以发布设置VehiclesGridView数据源的行,这样我就可以得到一个更清晰的答案