C#GridView On编辑错误

时间:2010-09-01 20:24:23

标签: c# asp.net gridview

这是我的GridView:

<div>
<asp:GridView ID="MainGridView" runat="server" AllowPaging="True" DataSourceID="GridViewDataSource" EnableModelValidation="True" CssClass="mGrid" PagerStyle-CssClass="pgr" 
        AlternatingRowStyle-CssClass="alt" onpageindexchanging="MainGridView_PageIndexChanging">
<Columns>
    <asp:CommandField ButtonType="Image" CancelImageUrl="~/images/icon_cancel.jpg" EditImageUrl="~/images/icon_edit.jpg" ShowEditButton="True" UpdateImageUrl="~/images/icon_update.jpg" />
</Columns>
</asp:GridView>
    <asp:ObjectDataSource ID="GridViewDataSource" runat="server" 
        OldValuesParameterFormatString="original_{0}" SelectMethod="GetDataByCategory" 
    TypeName="SEPTA_DSTableAdapters.AgencyTBLTableAdapter">
        <SelectParameters>
            <asp:SessionParameter DefaultValue="" Name="Category" SessionField="Cat" Type="String" />
        </SelectParameters>
    </asp:ObjectDataSource>
</div>

这是我背后的代码:

protected void CategoryDDL_SelectedIndexChanged(object sender, EventArgs e)
{
    Session["Cat"] = CategoryDDL.SelectedValue;
    FileTypeDDL_SelectedIndexChanged(sender,e);
}
protected void FileTypeDDL_SelectedIndexChanged(object sender, EventArgs e)
{
    //Agency Value
    if (FileTypeDDL.SelectedValue == "Agency") { AgencyGrid(); }
    else if (FileTypeDDL.SelectedValue == "Stops") { StopsGrid(); }
}
public void AgencyGrid ()
{
    SEPTA_DS.AgencyTBLDataTable GetAgency = (SEPTA_DS.AgencyTBLDataTable)ata.GetDataByCategory(Session["Cat"].ToString());
    string[] arrayOfKeys = new string[] { "AgencyID" };
    MainGridView.DataKeyNames = arrayOfKeys;
    GridViewDataSource.TypeName = "SEPTA_DSTableAdapters.AgencyTBLTableAdapter";
    MainGridView.AllowSorting = true;
}
public void StopsGrid()
{
    SEPTA_DS.StopsTBLDataTable GetStops = (SEPTA_DS.StopsTBLDataTable)stota.GetDataByCategory(Session["Cat"].ToString());
    string[] arrayOfKeys = new string[] { "StopsID" };
    MainGridView.DataKeyNames = arrayOfKeys;
    GridViewDataSource.TypeName = "SEPTA_DSTableAdapters.StopsTBLTableAdapter";
    MainGridView.AllowSorting = true;
}
protected void MainGridView_RowEditing(object sender, GridViewEditEventArgs e)
{

}

当我在两个单独的DropDownLists

之间进行选择时,我的GridView会更改属性
<tr><td>File Name<br /><asp:DropDownList ID="FileTypeDDL" runat="server" 
        Width="136" onselectedindexchanged="FileTypeDDL_SelectedIndexChanged" AutoPostBack="true">
<asp:ListItem Text="Agency" Value="Agency" />
<asp:ListItem Text="Calendar" Value="Calendar" />
<asp:ListItem Text="Calendar Dates" Value="Calendar Dates" />
<asp:ListItem Text="Routes" Value="Routes" />
<asp:ListItem Text="Stop Times" Value="Stop Times" />
<asp:ListItem Text="Stops" Value="Stops" />
<asp:ListItem Text="Transfers" Value="Transfers" />
<asp:ListItem Text="Trips" Value="Trips" />
</asp:DropDownList></td></tr>

<tr><td>Category<br /><asp:DropDownList ID="CategoryDDL" runat="server" Width="136" onselectedindexchanged="CategoryDDL_SelectedIndexChanged" AutoPostBack="true">
<asp:ListItem Text="Select" Value="Select" />
<asp:ListItem Text="Regional Rail" Value="Regional Rail" />
<asp:ListItem Text="Transit" Value="Transit" />
</asp:DropDownList></td></tr>

错误在Stops的{​​{1}}下。

FileTypeDDL下,我可以点击编辑按钮并成功取消按钮。

Agency下,我收到以下错误:

Stops

问题

为什么它不在 DataBinding: 'System.Data.DataRowView' does not contain a property with the name 'StopsID'. Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code. Exception Details: System.Web.HttpException: DataBinding: 'System.Data.DataRowView' does not contain a property with the name 'StopsID'. Source Error: An unhandled exception was generated during the execution of the current web request. Information regarding the origin and location of the exception can be identified using the exception stack trace below. Stack Trace: [HttpException (0x80004005): DataBinding: 'System.Data.DataRowView' does not contain a property with the name 'StopsID'.] System.Web.UI.DataBinder.GetPropertyValue(Object container, String propName) +8660309 System.Web.UI.WebControls.GridView.CreateChildControls(IEnumerable dataSource, Boolean dataBinding) +2178 System.Web.UI.WebControls.CompositeDataBoundControl.PerformDataBinding(IEnumerable data) +57 System.Web.UI.WebControls.GridView.PerformDataBinding(IEnumerable data) +14 System.Web.UI.WebControls.DataBoundControl.OnDataSourceViewSelectCallback(IEnumerable data) +114 System.Web.UI.DataSourceView.Select(DataSourceSelectArguments arguments, DataSourceViewSelectCallback callback) +31 System.Web.UI.WebControls.DataBoundControl.PerformSelect() +142 System.Web.UI.WebControls.BaseDataBoundControl.DataBind() +73 System.Web.UI.WebControls.GridView.DataBind() +4 System.Web.UI.WebControls.BaseDataBoundControl.EnsureDataBound() +82 System.Web.UI.WebControls.BaseDataBoundControl.OnPreRender(EventArgs e) +22 System.Web.UI.WebControls.GridView.OnPreRender(EventArgs e) +17 System.Web.UI.Control.PreRenderRecursiveInternal() +80 System.Web.UI.Control.PreRenderRecursiveInternal() +171 System.Web.UI.Control.PreRenderRecursiveInternal() +171 System.Web.UI.Control.PreRenderRecursiveInternal() +171 System.Web.UI.Control.PreRenderRecursiveInternal() +171 System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) +842 下工作,但在Stops下呢? 代码隐藏中是否缺少组件?

以下是我的DataTables:

alt text

2 个答案:

答案 0 :(得分:0)

我相信如果你看一下你的数据源(SQL数据库?我不确定......在AgencyTBLTableAdapter后面的某个地方),你会看到该表有一个名为AgencyID的列。

然而,当您查看Stops的类似表格时,我猜您将找不到StopsID

需要修改后备数据源以使其具有StopsID。

如果可以,请提供有关TableAdapters背后的数据表的更多信息。

答案 1 :(得分:0)

似乎在使用带有GridView的标准编辑按钮时,必须将功能添加到Page_Load,如此

protected void Page_Load(object sender, EventArgs e)
    {
        if (FileTypeDDL.SelectedValue == "Agency") { AgencyGrid(); }
        else if (FileTypeDDL.SelectedValue == "Stops") { StopsGrid(); }
    }

如果有人知道一个更好的解决方案,而不是把这些线放到任何地方,我都会听到。