我在.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 E)115 System.Web.UI.WebControls.DataGrid.OnBubbleEvent(對象源,EventArgs的)498 System.Web.UI.Control.RaiseBubbleEvent(對象源,EventArgs的參數)37 的System.Web。 UI.WebControls.DataGridItem.OnBubbleEvent(Object source,EventArgs e)+121 System.Web.UI.Control.RaiseBubbleEvent(Object source,EventArgs args)+37 System.Web.UI.WebControls.LinkButton.OnCommand(CommandEventArgs e )+125 System.Web.UI.WebControls.LinkButton.RaisePostBackEvent(字符串eventArgument)169
System.Web.UI.WebControls.LinkButton.System.Web.UI.IPostBackEventHandler.RaisePostBackEvent(字符串eventArgument)9 系統。 Web.UI.Page.RaisePostBackEvent(IPostBackEventHandler sourceControl,String eventArgument)+13 System.Web.UI.Page.RaisePostBackEvent(NameValueCollection postData)+176 System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint,Boolean includeStagesAfterAsyncPoint)+ 5563
你知道爲什麼我有這個錯誤嗎?我能做些什麼來解決它?我寫此功能更新命令
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;
}
好奇你爲什麼使用DataGrid而不是GridView? – TheGeekYouNeed