编译网格视图以显示一组订单时,出现索引超出范围异常。
当我添加新行时,它不会发生,但是当我尝试删除或更新行时,它会发生。
任何帮助深表感谢。
设计者是:
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" DataKeyNames="" OnRowCancelingEdit="GridView1_RowCancelingEdit" OnRowEditing="GridView1_RowEditing" OnRowUpdating="GridView1_RowUpdating" OnRowCommand="GridView1_RowCommand" ShowFooter="True" OnRowDeleting="GridView1_RowDeleting"> <Columns> <asp:TemplateField ShowHeader="False"> <EditItemTemplate> <asp:Button ID="Button1" runat="server" CausesValidation="True" CommandName="Update" Text="Update" /> <asp:Button ID="Button2" runat="server" CausesValidation="False" CommandName="Cancel" Text="Cancel" /> </EditItemTemplate> <ItemTemplate> <asp:Button ID="editButton" runat="server" CausesValidation="False" CommandName="Edit" Text="Edit" /> </ItemTemplate> <FooterTemplate> <asp:Button ID="LinkButton2" runat="server" CausesValidation="False" CommandName="AddNew" Text="Add New"></asp:Button> </FooterTemplate> </asp:TemplateField> <asp:TemplateField HeaderText="Order ID"> <EditItemTemplate> <asp:TextBox ID="txtOrderID" runat="server" Text='<%# Bind("OrderID") %>'></asp:TextBox> </EditItemTemplate> <FooterTemplate> <asp:TextBox ID="txtNewOrderID" runat="server"></asp:TextBox> </FooterTemplate> <ItemTemplate> <asp:Label ID="Label1" runat="server" Text='<%# Bind("OrderID") %>'></asp:Label> </ItemTemplate> </asp:TemplateField> <asp:TemplateField HeaderText="Date"> <EditItemTemplate> <asp:TextBox ID="txtDate" runat="server" Text='<%# Bind("Date") %>'></asp:TextBox> </EditItemTemplate> <FooterTemplate> <asp:TextBox ID="txtNewDate" runat="server"></asp:TextBox> </FooterTemplate> <ItemTemplate> <asp:Label ID="Label2" runat="server" Text='<%# Bind("Date") %>'></asp:Label> </ItemTemplate> </asp:TemplateField> <asp:TemplateField HeaderText="Order Taken by"> <EditItemTemplate> <asp:TextBox ID="txtOrderTaker" runat="server" Text='<%# Bind("OrderTaker") %>'></asp:TextBox> </EditItemTemplate> <FooterTemplate> <asp:TextBox ID="txtNewOrderTaker" runat="server"></asp:TextBox> </FooterTemplate> <ItemTemplate> <asp:Label ID="Label3" runat="server" Text='<%# Bind("OrderTaker") %>'></asp:Label> </ItemTemplate> </asp:TemplateField> <asp:TemplateField HeaderText="Order By"> <EditItemTemplate> <asp:TextBox ID="txtOrderBy" runat="server" Text='<%# Bind("OrderBy") %>'></asp:TextBox> </EditItemTemplate> <FooterTemplate> <asp:TextBox ID="txtNewOrderBy" runat="server"></asp:TextBox> </FooterTemplate> <ItemTemplate> <asp:Label ID="Label4" runat="server" Text='<%# Bind("OrderBy") %>'></asp:Label> </ItemTemplate> </asp:TemplateField> <asp:TemplateField HeaderText="Lens Material"> <EditItemTemplate> <asp:TextBox ID="txtMaterial" runat="server" Text='<%# Bind("Material") %>'></asp:TextBox> </EditItemTemplate> <FooterTemplate> <asp:TextBox ID="txtNewMaterial" runat="server"></asp:TextBox> </FooterTemplate> <ItemTemplate> <asp:Label ID="Label5" runat="server" Text='<%# Bind("Material") %>'></asp:Label> </ItemTemplate> </asp:TemplateField> <asp:TemplateField HeaderText="Tint"> <EditItemTemplate> <asp:TextBox ID="txtTint" runat="server" Text='<%# Bind("Tint") %>'></asp:TextBox> </EditItemTemplate> <FooterTemplate> <asp:TextBox ID="txtNewTint" runat="server"></asp:TextBox> </FooterTemplate> <ItemTemplate> <asp:Label ID="Label6" runat="server" Text='<%# Bind("Tint") %>'></asp:Label> </ItemTemplate> </asp:TemplateField> <asp:TemplateField HeaderText="BCOR"> <EditItemTemplate> <asp:TextBox ID="txtBCOR" runat="server" Text='<%# Bind("BCOR") %>'></asp:TextBox> </EditItemTemplate> <FooterTemplate> <asp:TextBox ID="txtNewBCOR" runat="server"></asp:TextBox> </FooterTemplate> <ItemTemplate> <asp:Label ID="Label7" runat="server" Text='<%# Bind("BCOR") %>'></asp:Label> </ItemTemplate> </asp:TemplateField> <asp:TemplateField HeaderText="Power"> <EditItemTemplate> <asp:TextBox ID="txtPower" runat="server" Text='<%# Bind("Power") %>'></asp:TextBox> </EditItemTemplate> <FooterTemplate> <asp:TextBox ID="txtNewPower" runat="server"></asp:TextBox> </FooterTemplate> <ItemTemplate> <asp:Label ID="Label8" runat="server" Text='<%# Bind("Power") %>'></asp:Label> </ItemTemplate> </asp:TemplateField> <asp:TemplateField HeaderText="Diameter"> <EditItemTemplate> <asp:TextBox ID="txtDiameter" runat="server" Text='<%# Bind("Diameter") %>'></asp:TextBox> </EditItemTemplate> <FooterTemplate> <asp:TextBox ID="txtNewDiameter" runat="server"></asp:TextBox> </FooterTemplate> <ItemTemplate> <asp:Label ID="LabelDiameter" runat="server" Text='<%# Bind("Diameter") %>'></asp:Label> </ItemTemplate> </asp:TemplateField> <asp:TemplateField HeaderText="Design"> <EditItemTemplate> <asp:TextBox ID="txtDesign" runat="server" Text='<%# Bind("Design") %>'></asp:TextBox> </EditItemTemplate> <FooterTemplate> <asp:TextBox ID="txtNewDesign" runat="server"></asp:TextBox> </FooterTemplate> <ItemTemplate> <asp:Label ID="Label9" runat="server" Text='<%# Bind("Design") %>'></asp:Label> </ItemTemplate> </asp:TemplateField> <asp:TemplateField HeaderText="Notes"> <EditItemTemplate> <asp:TextBox ID="txtNotes" runat="server" Text='<%# Bind("Notes") %>'></asp:TextBox> </EditItemTemplate> <FooterTemplate> <asp:TextBox ID="txtNewNotes" runat="server" TextMode="MultiLine"></asp:TextBox> </FooterTemplate> <ItemTemplate> <asp:Label ID="Label10" runat="server" Text='<%# Bind("Notes") %>'></asp:Label> </ItemTemplate> </asp:TemplateField> <asp:CommandField HeaderText="Delete" ShowDeleteButton="True" ShowHeader="True" /> </Columns> </asp:GridView>
后面的代码是:
public class LensOrdersCls { private string cnstr = ConfigurationManager.ConnectionStrings["PatientConnectionString"].ConnectionString; private string sql; public LensOrdersCls() { // // TODO: Add constructor logic here // } public void Insert(int OrderID, string Date, string OrderTaker, string OrderBy, string Material, string Tint, string BCOR, string Power, string Diameter, string Design, string Notes) { string sql = "Insert Into LensOrders (OrderID, Date, OrderTaker, OrderBy, Material, Tint, BCOR, Power, Diameter, Design, Notes) Values ('" + OrderID + "' , '" + Date + "', '" + OrderTaker + "', '" + OrderBy + "', '" + Material + "' , '" + Tint + "' , '" + BCOR + "' , '" + Power + "', '" + Diameter + "', '" + Design + "', '" + Notes + "')"; SqlConnection conn = new SqlConnection(cnstr); conn.Open(); SqlCommand cmd = new SqlCommand(sql, conn); cmd.ExecuteNonQuery(); conn.Close(); conn.Dispose(); } public DataTable Fetch() { string sql = "Select * From LensOrders"; SqlDataAdapter da = new SqlDataAdapter(sql, cnstr); DataTable dt = new DataTable(); da.Fill(dt); return dt; } public void Update(int OrderID, string Date, string OrderTaker, string OrderBy, string Material, string Tint, string BCOR, string Power, string Diameter, string Design, string Notes) { string sql = "UPDATE LensOrders SET OrderID='" + OrderID + "', Date = '" + Date + "', OrderTaker='" + OrderTaker + "', OrderBy = '" + OrderBy + "' Material=" + Material + "' Tint= '" + Tint + "' BCOR= '" + BCOR + "' Power '" + Power + "' Diameter= '" + Diameter + "' Design= '" + Design + "' Notes= '" + Notes; SqlConnection conn = new SqlConnection(cnstr); conn.Open(); SqlCommand cmd = new SqlCommand(sql, conn); cmd.ExecuteNonQuery(); conn.Close(); conn.Dispose(); } public void Delete(int OrderID) { string sql = "Delete LensOrders Where OrderID=" + OrderID; SqlConnection conn = new SqlConnection(cnstr); conn.Open(); SqlCommand cmd = new SqlCommand(sql, conn); cmd.ExecuteNonQuery(); conn.Close(); conn.Dispose(); } }
后面的页面代码是:
public partial class Default : System.Web.UI.Page { LensOrdersCls LensOrder = new LensOrdersCls(); private void FillOrdersInGrid() { DataTable dtOrder = LensOrder.Fetch(); if (dtOrder.Rows.Count > 0) { GridView1.DataSource = dtOrder; GridView1.DataBind(); } else { dtOrder.Rows.Add(dtOrder.NewRow()); GridView1.DataSource = dtOrder; GridView1.DataBind(); int TotalColumns = GridView1.Rows[0].Cells.Count; GridView1.Rows[0].Cells.Clear(); GridView1.Rows[0].Cells.Add(new TableCell()); GridView1.Rows[0].Cells[0].ColumnSpan = TotalColumns; GridView1.Rows[0].Cells[0].Text = "No Record Found"; } } protected void Page_Load(object sender, EventArgs e) { if (!IsPostBack) { FillOrdersInGrid(); test.Text = GridView1.Rows[0].Cells.Count.ToString(); } } protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e) { if (e.CommandName.Equals("AddNew")) { TextBox txtNewOrderID = (TextBox)GridView1.FooterRow.FindControl("txtNewOrderID"); TextBox txtNewDate = (TextBox)GridView1.FooterRow.FindControl("txtNewDate"); TextBox txtNewOrderTaker = (TextBox)GridView1.FooterRow.FindControl("txtNewOrderTaker"); TextBox txtNewOrderBy = (TextBox)GridView1.FooterRow.FindControl("txtNewOrderBy"); TextBox txtNewMaterial = (TextBox)GridView1.FooterRow.FindControl("txtNewMaterial"); TextBox txtNewTint = (TextBox)GridView1.FooterRow.FindControl("txtNewTint"); TextBox txtNewBCOR = (TextBox)GridView1.FooterRow.FindControl("txtNewBCOR"); TextBox txtNewPower = (TextBox)GridView1.FooterRow.FindControl("txtNewPower"); TextBox txtNewDiameter = (TextBox)GridView1.FooterRow.FindControl("txtNewDiameter"); TextBox txtNewDesign = (TextBox)GridView1.FooterRow.FindControl("txtNewDesign"); TextBox txtNewNotes = (TextBox)GridView1.FooterRow.FindControl("txtNewNotes"); LensOrder.Insert(Convert.ToInt32(txtNewOrderID.Text), txtNewDate.Text, txtNewOrderTaker.Text, txtNewOrderBy.Text, txtNewMaterial.Text, txtNewTint.Text, txtNewBCOR.Text, txtNewPower.Text, txtNewDiameter.Text, txtNewDesign.Text, txtNewNotes.Text); FillOrdersInGrid(); } } // Row Editing protected void GridView1_RowEditing(object sender, GridViewEditEventArgs e) { GridView1.EditIndex = e.NewEditIndex; FillOrdersInGrid(); } // Row Cancel editing protected void GridView1_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e) { GridView1.EditIndex = -1; FillOrdersInGrid(); } //Row updating protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e) { TextBox txtOrderID = (TextBox)GridView1.Rows[e.RowIndex].FindControl("txtOrderID"); TextBox txtDate = (TextBox)GridView1.Rows[e.RowIndex].FindControl("txtDate"); TextBox txtOrderTaker = (TextBox)GridView1.Rows[e.RowIndex].FindControl("txtOrderTaker"); TextBox txtOrderBy = (TextBox)GridView1.Rows[e.RowIndex].FindControl("txtNewOrderBy"); TextBox txtMaterial = (TextBox)GridView1.Rows[e.RowIndex].FindControl("txtMaterial"); TextBox txtTint = (TextBox)GridView1.Rows[e.RowIndex].FindControl("txtTint"); TextBox txtBCOR = (TextBox)GridView1.Rows[e.RowIndex].FindControl("txtBCOR"); TextBox txtPower = (TextBox)GridView1.Rows[e.RowIndex].FindControl("txtPower"); TextBox txtDiameter = (TextBox)GridView1.Rows[e.RowIndex].FindControl("txtDiameter"); TextBox txtDesign = (TextBox)GridView1.Rows[e.RowIndex].FindControl("txtDesign"); TextBox txtNotes = (TextBox)GridView1.Rows[e.RowIndex].FindControl("txtNotes"); LensOrder.Update(Convert.ToInt32(GridView1.DataKeys[e.RowIndex].Values[0].ToString()), txtDate.Text, txtOrderTaker.Text, txtOrderBy.Text, txtMaterial.Text, txtTint.Text, txtBCOR.Text, txtPower.Text, txtDiameter.Text, txtDesign.Text, txtNotes.Text); GridView1.EditIndex = -1; FillOrdersInGrid(); } //Deleting row protected void GridView1_RowDeleting(object sender, GridViewDeleteEventArgs e) { LensOrder.Delete(Convert.ToInt32(GridView1.DataKeys[e.RowIndex].Values[0].ToString())); FillOrdersInGrid(); } }
您没有为Grid设置数据键的名称,但是您的删除方法引用了DataKeys [e.RowIndex]。我认为这就是引发异常的地方。
在标记中设置DataKeyNames =“ OrderId”。