假设GridView中某行的index为1
怎么才能把这行删除掉呢。
我用GridView1.DeleteRow(index);可是不行,那个大虾帮帮忙

解决方案 »

  1.   

    GridView1.Rows[1].Style.Add(HtmlTextWriterStyle.Display, "none");
      

  2.   

    Gridview生成的是Table,只能隐藏,或者就把对应数据删除然后再重新绑定Gridview
      

  3.   

    简单点的· 在gridview后面加一删除列
      然后如下 
    <ItemTemplate>  
                    <asp:LinkButton ID="LinkButton1" runat="server" CausesValidation="False" CommandName="Delete"  
                        Text="删除" OnClientClick='<%#  "if (!confirm(\"你确定要删除" + Eval("字段名称").ToString() + "吗?\")) return false;"%>'></asp:LinkButton>  
                </ItemTemplate>  
      

  4.   

    只是想删除Web页面上GridView表中的某行
      

  5.   

    想在数据库里删这条记录的话,通过DataKeyField,然后拼接sql去delete
    如果仅仅是在gridview中删除,用gridview1.rows.Remove
      

  6.   

    能否不这样,因为有可能一次删除很多行,所以我在GridView中加了一个CheckBox控件,我判断这个控件选中的时候就删除掉,所以有没有办法不在后面加删除列的
      

  7.   

    不是数据库里,只是在Web页面的GridView表中
      

  8.   


    ==在Gridview 添加一列
     <asp:TemplateColumn HeaderText="操作">
                                                <itemstyle horizontalalign="Center" />
                                                <itemtemplate>
    <cc1:LinkButton id="lbActivate" runat="server" CommandName="delete" onclientclick="return window.confirm('如果删除该人员将无法恢复,是否要删除该人员?');"></cc1:LinkButton>
    </itemtemplate>
                                    <headerstyle horizontalalign="Center" width="15%" />
                                            </asp:TemplateColumn>然后 通过,Gridview 的ItemCommand事件       protected void DataGridUserList_ItemCommand(object source, DataGridCommandEventArgs e)
            {
                string id = this.DataGridUserList.DataKeys[e.Item.ItemIndex].ToString();
                if (e.CommandName == "delete")
                {                    if ()//写自己调用 底层的方法,或 sql 语句
                        {
                            Popwin.ShowMessage("删除成功!", this.Page);                    }
                        else
                        {
                            Popwin.ShowMessage("删除失败!", this.Page);
                        }
                    }                //删除完后在把数据绑定一便
                    //this.DataGridUserListDataBind();            }        }
      

  9.   

      string id = this.DataGridUserList.DataKeys[e.Item.ItemIndex].ToString();
      

  10.   


    ==在Gridview 添加一列
     <asp:TemplateColumn HeaderText="操作">
                                                <itemstyle horizontalalign="Center" />
                                                <itemtemplate>
    <cc1:LinkButton id="lbActivate" runat="server" CommandName="delete" onclientclick="return window.confirm('如果删除该人员将无法恢复,是否要删除该人员?');"></cc1:LinkButton>
    </itemtemplate>
                                    <headerstyle horizontalalign="Center" width="15%" />
                                            </asp:TemplateColumn>//然后 通过,Gridview 的ItemCommand事件       protected void DataGridUserList_ItemCommand(object source, DataGridCommandEventArgs e)
            {
                string id = this.DataGridUserList.DataKeys[e.Item.ItemIndex].ToString();
                if (e.CommandName == "delete")
                {                    if ()//写自己调用 底层的方法,或 sql 语句
                        {
                            Popwin.ShowMessage("删除成功!", this.Page);                    }
                        else
                        {
                            Popwin.ShowMessage("删除失败!", this.Page);
                        }
                    }                //删除完后在把数据绑定一便
                    //this.DataGridUserListDataBind();            }        }
      

  11.   

    设置DataKeys...后台就可以取了删了~
      

  12.   

    protected void DataGridUserList_ItemCommand(object source, DataGridCommandEventArgs e)
            {
                string id = this.DataGridUserList.DataKeys[e.Item.ItemIndex].ToString();
                if (e.CommandName == "delete")
                {                    if ()//写自己调用 底层的方法,或 sql 语句
                        {
                            Popwin.ShowMessage("删除成功!", this.Page);                    }
                        else
                        {
                            Popwin.ShowMessage("删除失败!", this.Page);
                        }
                    }                //删除完后在把数据绑定一便
                    //this.DataGridUserListDataBind();            }        }
      

  13.   

    那就如下   我用到的。首先在页面增加一个删除按钮,然后在按钮的单击事件里写
    DataTable dtwhere=new DataTable()
    CheckBox chk;
      //循环遍历GridView行数据
     foreach (GridViewRow row in this.grv_OutInter.Rows)
    {
             //判断是否为数据行
              if (row.RowType == DataControlRowType.DataRow)
             {
                DataRow dr;
                        dr = dtwhere.NewRow();                    chk = (CheckBox)row.FindControl("你页面的CheckBoxID");//获取控件ID
                        dr[“字段名称”] = 值;//用户ID
                        .....
                       dr[“字段名称”] = Convert.ToString(chk.Checked);//是否选中
                       dtwhere.Rows.Add(dr);//添加到dtwhere
              }
    }
    链接数据源
    DbConnection conn = null;//数据库链接
    .....
    后面的不用我写了吧!·
      

  14.   

    链接数据源后 把dtwhere放入你执行删除的方法里就行了  (执行删除的方法需接受dtwhere)
    列如(UpdateTempBySelect(dtwhere);//删除方法)
      

  15.   

    先把取出来的数据取出来存到VIEWSTATE["XX"]中,或者是LIST<T>中,删除的时候遍历数据,然后在Remove()掉,再重新绑定。
      

  16.   

    string courseid = gvClassCourse.DataKeys[e.RowIndex].Value.ToString();
    List<CourseInfo> list = ViewState[""+hfClassId.Value+""] as List<CourseInfo>;
    CourseInfo course = list.Find(ee=>ee.Code==courseid);
    if(course!=null)
            list.Remove(course);
    gvClassCourse.DataSource = list;
    gvClassCourse.DataBind();
      

  17.   

    http://topic.csdn.net/u/20091119/12/e10d4ad4-efaa-401b-96a2-eaf77e307aec.html?4125
    下载这个文件  里面有一个 用checkbox 和gridview控件实现删除的例子...
      

  18.   

    web页
            <asp:GridView ID="GridView1" runat="server">
                <Columns>
                    <asp:TemplateField>
                        <ItemTemplate>
                            <asp:LinkButton ID="LinkButton1" runat="server" OnClick="LinkButton1_Click">LinkButton</asp:LinkButton>
                        </ItemTemplate>
                    </asp:TemplateField>
                </Columns>
            </asp:GridView>cs页
    protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                string sConnectionString;            // Modify the following string to correctly connect to your SQL Server.
                sConnectionString = "Password=sa;User ID=sa;"
                    + "Initial Catalog=wzdb;"
                    + "Data Source=(local)";            SqlConnection conn = new SqlConnection(sConnectionString);
                conn.Open();            DataSet ds = new DataSet();
                SqlCommand cmd = new SqlCommand();
                cmd.Connection = conn;
                cmd.CommandText = "select * from gc_table";
                cmd.ExecuteNonQuery();
                SqlDataAdapter sda = new SqlDataAdapter(cmd);
                sda.Fill(ds);
                conn.Close();
                conn.Dispose();            GridView1.DataSource = ds;
                GridView1.DataBind();
            
            }
        }    protected void LinkButton1_Click(object sender, EventArgs e)
        {
            Control col = sender as Control;
            int index = ((GridViewRow)col.Parent.Parent).RowIndex;
            GridView1.Rows[index].Visible = false;
        }
      

  19.   

    如果是隐藏的话,用JS来实现,很方面比如jQuery等等
    要是要从数据库中删除,上面有答案。