2010-09-01 78 views
0

這裏是我的GridView:C#的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) 
{ 

} 

我的GridView改變屬性,當我兩個單獨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> 
之間進行選擇

錯誤在FileTypeDDLStops下。

根據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下工作,但它Agency下呢? 代碼隱藏中是否存在缺失的組件?

這裏是我的數據表:

alt text

回答

0

看來,使用與GridView控件的標準編輯按鈕時的功能不得不被添加到Page_Load像這樣

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

如果有人知道更好的修復不是把這些線無處不在,我都耳朵。

0

,如果你看看你的數據源,我相信(?SQL數據庫,我不知道......後面的某處AgencyTBLTableAdapter),你會看到表有一個名爲AgencyID的列。

但是,當您查看停靠點的類似表格時,我猜你找不到StopsID

支持數據源將需要修改爲具有StopsID。

如果可以,請提供有關TableAdapters後面的數據表的更多信息。

+0

我在SQL Server上的DataSet中有幾個表。原始的GridView加載代理表,但如果從DropDownList中選擇了不同的項目,我特別在代碼隱藏中放置了不同的DataSource。 – balexander 2010-09-02 13:10:59

+0

我在上面的DataSet中發佈了DataTables的圖像。 – balexander 2010-09-02 13:16:43