2010-09-01 74 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