有一个datagrid模板列,每条记录第一列有一个checkbox,可以多选,点击button可以将选择的记录删除,如何实现
难点:1。如何保存checkde那行的索引
      2。知了索引如何一起删除欢迎大家讨论,UP有分

解决方案 »

  1.   

    循环一个存有索引值的数组,里面加入一个遍历datarow的循环,如果值相等就删除,这样可以吧?
      

  2.   

    For each循环 判断属性 然后结果 保存成一个字符串 例如:"1|2|3|4" 发给一个页面处理 就可以了
      

  3.   

    <asp:boundcolumn visible="False" datafield="FileDescID" readonly="True"></asp:boundcolumn>
    加一列记录信息的ID
    还有一行是Checkbox用来选中的
    然后在删除处理方法中获取
    int count=FileListDG.Items.Count;
    for(int i=0;i<count;i++)
    {
    if(((CheckBox)(FileListDG.Items[i].FindControl("CheckBox1"))).Checked)
    {
    int selectFileID=Int32.Parse(FileListDG.Items[i].Cells[0].Text.ToString().Trim());
    //处理代码
    }
    }
      

  4.   

    1:给你的Grid隐藏一列,绑定ID字段
    比如说隐藏在第一列
    2:string id = "";
    foreach(DataGridItem dgi in yourGrid.Items)
    {
        CheckBox cb = (CheckBox)dgi.FindControl("yourCheckBoxId");
         if(cb.Checked)
         {
             id = dgi.Items.Cell[0].Text + ",";
         }
    }if(id.Length > 0)
    {
        id = "(" + id.Substring(0,id.Length - 1) + ")";
        string sql = "delete from yourtable where id in " + id;
        //执行这个sql就可以拉
    }
      

  5.   

    //思路如下
    private void DataGrid1_ItemCreated(object sender, System.Web.UI.WebControls.DataGridItemEventArgs e)
    {
    if(e.Item.ItemIndex ==-1)
    return;
        
    //控制选择的情况
    HtmlInputCheckBox chk = (HtmlInputCheckBox)e.Item.Cells[0].FindControl("CheckBox2");
    if(chk!=null)
    {
    chk.ID="chk_"+DataGrid1.DataKeys[e.Item.ItemIndex].ToString();
    chk.Attributes["onclick"]=" return changeit(this);";
    }
    }//然后脚本中判断选择的信息
    function checkAll(){
    var select  = document.getElementById("chk_All");
    var IsSelected = select.checked;
    var all = new Array();
    all= document.getElementsByTagName("input");
    for(i = 0;i<all.length;i++){
    if(all[i].type.toLowerCase() =="checkbox"){
    all[i].checked = IsSelected;
    if((all[i].id!=select.id) && all[i].checked){
    SaveInfo(all[i].id);
    }

    if(!all[i].checked){
    ChangeSaveInfo(all[i].id);
    }
    }
    }
    }
    //保存信息
    function SaveInfo(obj){
    var txt=document.getElementById("Text_Save");
    if(txt.value .indexOf(obj)==-1)
    txt.value =obj+";"+txt.value;     
    }
    function ChangeSaveInfo(obj){
        var txt=document.getElementById("Text_Save");
        var saved =txt.value;
    if(saved.indexOf(obj)!=-1){
    saved =saved.replace(obj+";","");
    }
    txt.value =saved;     
    }
    function changeit(e){
    if(e.checked){
        SaveInfo(e.id);
    }else{
    ChangeSaveInfo(e.id);
    }
    }//然后接收Text_Save.Text中的值就是选择的控件的索引了
    //最后 在服务器端进行分析Text_Save.Text的值,实现删除功能
      

  6.   

    做一个 记录ID 的隐藏列   遍厉  DATAGRID 后 如何checkbox.CHECK=true  就取出 ID 的直  然后 删除 再绑定
      

  7.   

    For i = 0 To dgReceive.Items.Count - 1‘遍历datagrid
                    Dim dgItem As DataGridItem = dgReceive.Items(i)
                    Dim chkBox As CheckBox = dgItem.FindControl("chkSubmit")'定义摸板列中的textbox                If chkBox.Checked = True Then'如果被选中
                       调用删除该行
                    End If
                Next注意:::page_load  中 datagrid一定要包含在 If Not Page.IsPostBack = True then ........end 中
      

  8.   

    一起删除,我想可以这样实现。
    dim str as string
    For i = 0 To dgReceive.Items.Count - 1‘遍历datagrid
                    Dim dgItem As DataGridItem = dgReceive.Items(i)
                    Dim chkBox As CheckBox = dgItem.FindControl("chkSubmit")'定义摸板列中的textbox                If chkBox.Checked = True Then'如果被选中
                    str=该行的ID,’一定要加逗号!
             调用方法以 str为参数  
                    End If
    Next
    存储过程中可以这样写。
    if id 中包含逗号 
      delete table where  id='id'
    else       
      delete from table where id in (str)
      

  9.   

    给你个例子,这个例子主要用了ArrayList保存选择的行,并且翻页后,还是保存选择的状态。
    保存状态的方法
    /// <summary>
            /// 方法编号:10
            /// 方法名称:GetSelectID
            /// 内容摘要:获取选中的记录集合
            /// </summary>
            /// <param name="dg">当前操作的DataGrid控件</param>
            private void GetSelectID(DataGrid dg)
            {
                //创建集合对象
                ArrayList aryLstID = null;
                if (!object.Equals(ViewState["aryLstID"],null))
                {
                    aryLstID = ViewState["aryLstID"] as ArrayList;
                }
                else
                {
                    aryLstID = new ArrayList();
                }
                
                //创建Hashtable对象,用于存放所选毕业生的姓名和学历
                Hashtable htXueLi = null;
                if (!object.Equals(ViewState["htXueLi"],null))
                {
                    htXueLi = ViewState["htXueLi"] as Hashtable;
                }
                else
                {
                    htXueLi = new Hashtable();
                }            foreach(DataGridItem dgItem in dg.Items)
                {
                    CheckBox chk = dgItem.FindControl("chkSelect") as CheckBox;                if (chk != null)
                    {
                        //如果此CheckBox选中,并且集合中没有此项,就将此项添加到集合中
                        if (chk.Checked && !aryLstID.Contains(dg.DataKeys[dgItem.ItemIndex]))
                        {
                            aryLstID.Add(dg.DataKeys[dgItem.ItemIndex]);
                        }
                            //如果没有选中,但集合中有此项,就将此项删除
                        else if(!chk.Checked && aryLstID.Contains(dg.DataKeys[dgItem.ItemIndex]))
                        {
                            aryLstID.Remove(dg.DataKeys[dgItem.ItemIndex]);
                        }
                        //将毕业生学历,姓名,ID号添加到Hashtable中
                        if (chk.Checked && !htXueLi.ContainsKey(dgItem.Cells[1].Text + "-" + dgItem.Cells[2].Text))
                        {
                            htXueLi.Add(dgItem.Cells[1].Text + "-" + dgItem.Cells[2].Text,dgItem.Cells[5].Text);
                        }
                            //将毕业生学历,姓名,ID号从Hashtable中删除
                        else if (!chk.Checked && htXueLi.ContainsKey(dgItem.Cells[1].Text + "-" + dgItem.Cells[2].Text))
                        {
                            htXueLi.Remove(dgItem.Cells[1].Text + "-" + dgItem.Cells[2].Text);
                        }
                    }
                }            ViewState["aryLstID"] = aryLstID;
                ViewState["htXueLi"]  = htXueLi;        }
      

  10.   

    翻页时:
    /// <summary>
            /// 方法编号:09
            /// 方法名称:dgList_PageIndexChanged
            /// 内容摘要:DataGrid分页事件
            /// </summary>
            /// <param name="source"></param>
            /// <param name="e"></param>
            private void dgList_PageIndexChanged(object source, System.Web.UI.WebControls.DataGridPageChangedEventArgs e)
            {
                //添加选中的项到集合对象中
                this.GetSelectID((DataGrid)source);            //分页
                ((DataGrid)source).CurrentPageIndex = e.NewPageIndex;
                this.btnQuery_ServerClick(null,null);
            }
      

  11.   

    DataBound时重新选中之前的状态
    /// <summary>
            /// 方法编号:08
            /// 方法名称:dgList_ItemDataBound
            /// 内容摘要:DataGrid绑定事件,用于产生序列号
            /// </summary>
            /// <param name="sender"></param>
            /// <param name="e"></param>
            private void dgList_ItemDataBound(object sender, System.Web.UI.WebControls.DataGridItemEventArgs e)
            {
                if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
                {
                    //在第一列中增加序列号列
                    e.Item.Cells[0].Text = Convert.ToString(e.Item.ItemIndex + 1);                //如果已经存在集合对象
                    if (!object.Equals(ViewState["aryLstID"],null))
                    {
                        ArrayList aryLstID = ViewState["aryLstID"] as ArrayList;
                    
                        //判断集合对象中是否有此记录项,如果有就选中
                        if (aryLstID != null && aryLstID.Contains(this.dgList.DataKeys[e.Item.ItemIndex]))
                        {
                            CheckBox chk = e.Item.FindControl("chkSelect") as CheckBox;                        if (chk != null)
                            {
                                chk.Checked = true;
                                intCount += 1;
                            }
                        }
                    }
    }
    }