2011-07-26 15 views
0

私の.aspxファイルにこのデータグリッドを作成しました。更新機能でエラーが発生しました

<asp:datagrid id="Computerchange" runat="server" AllowPaging="True" PageSize="2" AutoGenerateColumns="False" BorderColor="Gainsboro" Height="500px"> 
    <Columns> 
     <asp:EditCommandColumn ButtonType="LinkButton" UpdateText="Update" HeaderText="Admin Functions" CancelText="Cancel" EditText="Edit"></asp:EditCommandColumn> 
     <asp:ButtonColumn Text="Delete" HeaderText="Delete" CommandName="Delete"></asp:ButtonColumn> 
     <asp:BoundColumn DataField="com_id" ReadOnly="True" HeaderText="Computer Number"></asp:BoundColumn> 
     <asp:BoundColumn DataField="company" HeaderText="Company"></asp:BoundColumn> 
     <asp:BoundColumn DataField="price" HeaderText="Price"></asp:BoundColumn> 
     <asp:BoundColumn DataField="model" HeaderText="Model"></asp:BoundColumn> 
     <asp:BoundColumn DataField="description" HeaderText="Description"></asp:BoundColumn> 
     <asp:BoundColumn DataField="id" ReadOnly="True" HeaderText="Category"></asp:BoundColumn> 
     <asp:BoundColumn DataField="quantity" ReadOnly="False" HeaderText="Quantity"></asp:BoundColumn> 
     <asp:BoundColumn DataField="imgSrc" HeaderText="Image"></asp:BoundColumn> 
    </Columns> 
</asp:datagrid> 

このデータグリッドでは、データベースのこれらの列を編集 - 削除 - 更新したいと考えています。数量の列にReadOnly="False"があるとき、それは正常に動作します。編集 - 削除 - すべてを更新できます。 QUANTITYを除くすべての列を編集したいことがあります。しかし、Trueに変換すると、更新ボタンをクリックすると、次のエラーが発生します。

指定された引数が有効な値の範囲外です。パラメータ名:インデックス

スタックトレース:

[例外ArgumentOutOfRangeException:指定された引数が有効な値の範囲外でした。パラメータ名:インデックス]

System.Web.UI.ControlCollection.get_Item(のInt32インデックス)8750274 AdminMainPage.Computerchange_UpdateCommand(オブジェクトソース、DataGridCommandEventArgs E)626 System.Web.UI.WebControls.DataGrid.OnUpdateCommand( DataGridCommandEventArgs)+115 System.Web.UI.WebControls.DataGrid.OnBubbleEvent(オブジェクトソース、EventArgs e)+498 System.Web.UI.Control.RaiseBubbleEvent(Object source、EventArgs args)+37 System.Web。 (Object source、EventArgs args)+37 System.Web.UI.WebControls.LinkBut​​ton.OnCommand(CommandEventArgs e )+125 System.Web.UI.WebControls.LinkBut​​ton.RaisePostBackEvent(文字列eventArgument)169

System.Web.UI.WebControls.LinkBut​​ton.System.Web.UI.IPostBackEventHandler.RaisePostBackEvent(文字列eventArgument)+9 システム。 + 17 System.Web.UI.Page.RaisePostBackEvent(NameValueCollection postData)+176 System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint、Boolean includeStagesAfterAsyncPoint)+ WebControlBaseから継承されます。 5563

このエラーが発生した理由とその解決方法を知っていますか?私はUPDATEコマンド

private void Computerchange_UpdateCommand(object source, 
System.Web.UI.WebControls.DataGridCommandEventArgs e) 
{ 
    //Store updated column values in local variables: 

    //string updateCOM_ID = e.Item.Cells["com_id"].Text; 

    string updateCOM_ID = e.Item.Cells[2].Text; 

    string updateCompany = ((TextBox)e.Item.Cells[3].Controls[0]).Text; 

    double updatePrice = double.Parse(((TextBox)e.Item.Cells[4].Controls[0]).Text); 

    string updateModel = ((TextBox)e.Item.Cells[5].Controls[0]).Text; 

    string updateDescription = ((TextBox)e.Item.Cells[6].Controls[0]).Text; 

    int updateCategoryId = int.Parse(e.Item.Cells[7].Text); 

    string updateImage = ((TextBox)e.Item.Cells[9].Controls[0]).Text; 

    string updateQuantity = ((TextBox)e.Item.Cells[8].Controls[0]).Text; 

    newView.RowFilter = "com_id='" + updateCOM_ID + "'"; 

    if (newView.Count > 0) 
    { 
     //Delete the row that is being updated 

     newView.Delete(0); 
    } 

    newView.RowFilter = ""; 

    //Create a new DataRow and populate it with the new data. 

    DataRow Row = Table.NewRow(); 

    Row["com_id"] = updateCOM_ID; 

    Row["company"] = updateCompany; 

    Row["price"] = updatePrice; 

    Row["model"] = updateModel; 

    Row["description"] = updateDescription; 

    Row["id"] = updateCategoryId; 

    Row["imgSrc"] = updateImage; 

    //Row["quantity"] = updateQuantity; 

    //Insert the new DataRow: 

    Table.Rows.Add(Row); 

    Computerchange.EditItemIndex = -1; 

    Computerchange.DataSource = newView; 

    Computerchange.DataBind(); 

    // Now update the database with the new data 

    adminCentral1.adminCentral newData = new adminCentral1.adminCentral(); 

    string results; 

    results = newData.updateItem(updateCOM_ID, updateCompany, updatePrice, updateModel, updateDescription, updateImage, updateCategoryId, updateQuantity); 

    if (results == "Success") 
    { 
     errorLabel.Text = "Computer Updated to database!"; 
    } 
    else 
    { 
     errorLabel.Text = results; 
    } 
+1

グリッドビューではなくDataGridを使用している理由が不思議ですか? – TheGeekYouNeed

答えて

0

ため、この機能を書いたので、コントロールは[0]例外がスローされますので、ReadOnly="True"は、それは、テキストだけではなく、コントロールをレンダリングするとき。代わりにTemplateColumnを使用してください:

<asp:TemplateColumn HeaderText="Quantity"> 
<ItemTemplate> 
    <%# Eval("quantity") %> 
</ItemTemplate> 
<EditItemTemplate> 
    <asp:TextBox runat="server" ID="tbQuantity" Text='<%# Eval("quantity") %>' ReadOnly="False" /> 
</EditItemTemplate> 
</asp:TemplateColumn> 

あなたが編集を無効にするにはtrueにtbQuantity'sReadOnlyプロパティを設定することができます。

関連する問題