开发者

C# GridView On Edit Error

Here is my 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>

Here is my code behind:

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

}

My GridView changes properties when I select between two seperate DropDownLists

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

The error lies when under Stops in the FileTypeDDL.

When under Agency I can click the edit button and cancel button successfully.

When under Stops I get the below error:

 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.DataBind开发者_如何学编程er.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

QUESTION

Why doesn't it work under Stops but it does under Agency? Is there a missing component in the code-behind?

Here are my DataTables:

C# GridView On Edit Error


I believe if you look at your data source (SQL database? I'm not sure... somewhere behind AgencyTBLTableAdapter), you'll see that the table has a column called AgencyID.

When you look at the similar table for Stops, however, I'm guessing you will not find StopsID.

The backing data source will need to be modified to have StopsID.

If you can, please provide more info about the data-tables behind the TableAdapters.


It seems that when using the standard editing buttons with the GridView the functions had to be added to the Page_Load like so

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

If anyone knows a better fix than putting these lines everywhere, I'm all ears.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜