web:
<asp:gridview id="item" runat="server" width="100%"   autogeneratecolumns="False" cellpadding="0"  gridlines="None" allowpaging="True" pagesize="20" DataKeyNames="itemno" HorizontalAlign="Center">
        <columns>
            <asp:boundfield datafield="itemno" headertext="物品编号" ReadOnly="True">
                <ItemStyle Width="50px" />
            </asp:boundfield>
            <asp:boundfield datafield="itemname" headertext="物品名称" >
                <ItemStyle Width="60px" />
            </asp:boundfield>
            <asp:boundfield datafield="itemprice" headertext="物品价格(元)" >
                <ItemStyle Width="60px" />
            </asp:boundfield>
           <asp:commandfield showdeletebutton="True" showeditbutton="True" headertext="操作" >
               <ItemStyle Width="80px" />
           </asp:commandfield>
         
        </columns>
        <footerstyle backcolor="#507CD1" font-bold="True" forecolor="White" />
        <rowstyle backcolor="#EFF3FB" HorizontalAlign="Center" VerticalAlign="Middle" Width="100px" Wrap="False" />
        <editrowstyle backcolor="#2461BF" />
        <selectedrowstyle backcolor="#D1DDF1" font-bold="True" forecolor="#333333" />
        <pagerstyle backcolor="#2461BF" forecolor="#00C0C0" horizontalalign="Center" />
        <headerstyle cssclass="header" height="30px" forecolor="White"  />
        <alternatingrowstyle backcolor="White" />
        <emptydatarowstyle cssclass="header" forecolor="White" height="30px"  />
    </asp:gridview>dal:    /// <summary>
        /// 宿舍物品管理  在girdview中显示数据
        /// </summary>
        /// <returns></returns>
        public static IList<Items> SelectAllItems()
        {
            IList<Items> list = new List<Items>();
            string sql = "select * from Items";
            using (SqlDataReader reader = DBHelper.ExcuteReader(sql))
            {
                if (reader.Read())
                {
                    Items items = new Items();
                    items.ItemNo = reader["ItemNo"].ToString();
                    items.ItemName = reader["ItemName"].ToString();
                    items.ItemPrice = Convert.ToInt32(reader["ItemPrice"]);
                    list.Add(items);
                }
                return list;
            }
        }
        
        /// <summary>
        ///  更新宿舍物品信息 对应girdview的editbutton
        /// </summary>
        /// <param name="items">宿舍物品</param>
        /// <returns></returns>
        public static int UpdateItems(Items items)
        {
            int result = 0;
            string sql = null;
            StringBuilder builder = new StringBuilder();
            builder.Append("update Items set");
            builder.Append("ItemName = '" + items.ItemName + "',");
            builder.Append("ItemPrice ='" + items.ItemPrice + "',");
            builder.Append("ItemNo ='" + items.ItemNo + "',");
            builder.Append("where Id ='" + items.Id + "')select @@ROWCOUNT");
            sql = builder.ToString();
            try
            {
                result = DBHelper.ExcuteCommand(sql);
            }
            catch (Exception e)
            {
                throw e.InnerException;
            }
            finally
            {
                DBHelper.CloseConnction();
            }
            return result;
        }
        /// <summary>
        /// 根据Id删除信息 对应girdview的deletebutton
        /// </summary>
        /// <param name="Id">物品编号</param>
        /// <returns></returns>
        public static int DeleteItems(int id) 
        {
            int result = 0;
            string sql = "delete from Items where Id = " + " " + id + " ";
            try
            {
                result = DBHelper.ExcuteCommand(sql);
            }
            catch (Exception e)
            {
                throw e.InnerException;
            }
            finally
            {
                DBHelper.CloseConnction();
            }
            return result;
        }
bll:    public static int UpdateItems(Items items) 
        {
            return ItemsDAL.UpdateItems(items);
        }
        public static int DeleteItems(int id) 
        {
            return ItemsDAL.DeleteItems(id);
        }
        public static IList<Items> SelectALLItems() 
        {
            return ItemsDAL.SelectAllItems();
        }

解决方案 »

  1.   

      string sql = "delete from Items where Id = " + " " + id + " ";
    =====
      string sql = "delete from Items where Id =  " + id ;
      

  2.   

    写cs文件,因为我不怎么熟悉三层中的girdview要怎么用!
      

  3.   


    item.DataSource=bll.SelectALLItems();
    item.DataBind();
      

  4.   

    protected void Page_Load(object sender, EventArgs e)
            {
                int id = Convert.ToInt32(Request.QueryString["Id"]);
                int itemNo = Convert.ToInt32(Request.QueryString["ItemNo"]);
                if (!Page.IsPostBack) 
                {
                    this.DataBindItems();
                    if (id != 0) 
                    {
                        int result = ItemsBLL.DeleteItems(id);
                        if (result > 0)
                        {
                            DataBindItems();
                        }
                        else 
                        {
                            Response.Write("<script>alter('删除失败!')</script>");
                        }
                    }
                    if (itemNo != 0) 
                    {
                        Items items = new Items();
                        items.ItemNo = itemNo.ToString();
                        int result = ItemsBLL.UpdateItems(items);
                        if (result > 0)
                        {
                            DataBindItems();
                        }
                        else 
                        {
                            Response.Write("<script>alter('编辑失败!')</script>");
                        }
                    }
                }
            }
            public void DataBindItems() 
            {
                this.item.DataSource = ItemsBLL.SelectALLItems();
                this.item.DataBind();        }
            protected void item_PageIndexChanging(object sender, GridViewPageEventArgs e)
            {
                item.PageIndex = e.NewPageIndex;
                this.DataBindItems();
            }
    这是我自己写的,有问题的,绑定数据的没问题,但是添加了之后不能及时显示,而至于我刚说的修改和删除功能就没有了,谁教教我怎么写啊!!!
      

  5.   

    三层还就是绑定数据问题把,只要返回的类型是dataset,datatable还有数据集合类型的好像都可以的。
      

  6.   

    你要在更新数据后重新绑定就可以了。放在 item_PageIndexChanging 是分页事件啦
      

  7.   

    string sql =String.Format("delete from Items where Id ={0}",id );
    gridview自带了编辑和删除
     protected void gv_RowCommand(object sender, GridViewCommandEventArgs e)
            {
                  if (e.CommandName.Equals("Del"))
                  {
                      UnitBLL.Delete(int.Parse(e.CommandArgument.ToString()));
                      BindData();
                  }
            }
    protected void gv_RowUpdating(object sender, GridViewUpdateEventArgs e)
            {
                this.gv.EditIndex = e.RowIndex;
                Unit p = new Unit(); 
                UnitBLL.Edit(p);
                
                this.gv.EditIndex = -1;
                BindData();
            }
      

  8.   


    protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {
                string sql = "";
                sql = @"SELECT contract.amount, 
                               contract.re, 
                               contract.contents, 
                               contract.isApproved, 
                               contract.enddingTime, 
                               contract.beginningTime, 
                               contract.customerID, 
                               contract.contractID, 
                               customer.customerName 
                          FROM contract 
                          LEFT OUTER JOIN customer 
                            ON contract.customerID = customer.customerID";            ViewState["sql_select"] = sql;
                //this.Lable_count.Text = "共显示" + this.gv_contract.Rows.Count.ToString() + "条记录";
            }
            if (ViewState["sql_select"] != null)
            {
                this.sqlds_contract.SelectCommand = ViewState["sql_select"].ToString();
            }    }
        protected void gv_contract_RowCommand(object sender, GridViewCommandEventArgs e)
        {
            if (e.CommandName == "MyModify")
            {
                int index = Convert.ToInt32(e.CommandArgument);
                string contractID = this.gv_contract.DataKeys[index]["contractID"].ToString();
                this.objds_contract.SelectParameters["contractID"].DefaultValue = contractID;
                this.fv_contract.ChangeMode(FormViewMode.Edit);
                this.MultiView1.ActiveViewIndex = 1;
            }
            if (e.CommandName == "MyDetails")
            {
                int index = Convert.ToInt32(e.CommandArgument);
                string contractID = this.gv_contract.DataKeys[index]["contractID"].ToString();
                this.objds_contract.SelectParameters["contractID"].DefaultValue = contractID;
                this.fv_contract.ChangeMode(FormViewMode.ReadOnly);
                this.MultiView1.ActiveViewIndex = 1;
            }
            if (e.CommandName == "MyOrder")
            {
                int index = Convert.ToInt32(e.CommandArgument);
                string contractID = this.gv_contract.DataKeys[index]["contractID"].ToString();
                this.sqlds_order.SelectCommand = @"SELECT  [order].productID, 
                                                           [order].contractID, 
                                                           [order].orderID,   
                                                           product.productName, 
                                                           productType.productTypeName 
                                                      FROM [order] 
                                                      LEFT OUTER JOIN contract 
                                                        ON [order].contractID = contract.contractID 
                                                      LEFT OUTER JOIN product 
                                                        ON [order].productID = product.productID 
                                                      LEFT OUTER JOIN productType 
                                                        ON product.productTypeID = productType.productTypeID 
                                                     WHERE contract.contractID='" + contractID + "'";
                this.MultiView1.ActiveViewIndex = 3;
                this.Label_order.Text = "共显示0条记录";
            }
            if (e.CommandName == "MyDelete")
            {
                int index = Convert.ToInt32(e.CommandArgument);
                string contractID = this.gv_contract.DataKeys[index]["contractID"].ToString();
                contract BLL = new contract();
                try
                {
                    BLL.deletecontract(contractID);
                }
                catch (Exception ex)
                {
                    commTools.alert(this, ex.InnerException.Message);
                }
                commTools.alert(this, "删除成功!");
                string sql = ViewState["sql_select"].ToString();
                this.sqlds_contract.SelectCommand = sql;
                this.gv_contract.DataBind();
                //this.Lable_count.Text = "共显示" + this.gv_contract.Rows.Count.ToString() + "条记录";
            }
        }    protected void fv_contract_ItemInserted(object sender, FormViewInsertedEventArgs e)
        {
            if (e.Exception != null)
            {            commTools.alert(this, e.Exception.InnerException.Message);
                e.ExceptionHandled = true;
              //  this.MultiView1.ActiveViewIndex = 1;
            }
     
        }
        protected void fv_contract_ItemUpdating(object sender, FormViewUpdateEventArgs e)
        {
            foreach (DictionaryEntry entry in e.NewValues)
            {
                if (string.IsNullOrEmpty(entry.Value as string))
                {
                    e.NewValues[entry.Key] = null;
                }
            }    }
        protected void fv_contract_ItemInserting(object sender, FormViewInsertEventArgs e)
        {
            foreach (DictionaryEntry entry in e.Values)
            {
                if (string.IsNullOrEmpty(entry.Value as string))
                {
                    e.Values[entry.Key] = null;
                }
            }
        }protected void ib_AddRecord_Click(object sender, ImageClickEventArgs e)
        {
            this.fv_contract.ChangeMode(FormViewMode.Insert);
            this.MultiView1.ActiveViewIndex = 1;
        }protected void ib_Back_Insert_Click(object sender, ImageClickEventArgs e)
        {
            string sql = "";
            sql = ViewState["sql_select"].ToString();
            this.sqlds_contract.SelectCommand = sql;
            this.MultiView1.ActiveViewIndex = 0;
            this.gv_contract.DataBind();
        }
        protected void ib_Save_Insert_Click(object sender, ImageClickEventArgs e)
        {
            TextBox tb = (TextBox)this.fv_contract.FindControl("contractIDTextBox");
            ViewState["contractID"] = tb.Text.ToString();
            this.gv_contract.DataBind();
            this.MultiView1.ActiveViewIndex = 2;
            this.contractIDLabel.Text = ViewState["contractID"].ToString();
        }凑合看看吧~我不知道对你有没有用 
    这是我以前用2008做的一个界面