GridView与CheckBox

          < asp:GridView  ID ="gridFlow"  DataKeyNames ="id"  runat ="server"  AutoGenerateColumns ="False"  CellPadding ="4"
             ForeColor
="Black"  GridLines ="Vertical"  Width ="100%"  BackColor ="White"  BorderColor ="#DEDFDE"  BorderStyle ="None"  BorderWidth ="1px"  CaptionAlign ="Left"  ShowFooter ="True"  AllowPaging ="True"  OnPageIndexChanging ="gridFlow_PageIndexChanging"  OnRowDataBound ="gridFlow_RowDataBound"  PageSize ="20"  OnDataBinding ="gridFlow_DataBinding" >
             
< FooterStyle  BackColor ="#CCCC99"   />
             
< Columns >
                 
< asp:TemplateField >
                     
< ItemTemplate >
                         
< asp:CheckBox  ID ="CheckBox1"  runat ="server"   />
                     
</ ItemTemplate >
                     
< HeaderTemplate >
                         
< input  id ="CheckAll"  type ="checkbox"  onclick ="selectAll(this);"   />
                     
</ HeaderTemplate >
                 
</ asp:TemplateField >
                 
< asp:BoundField  DataField ="Flow_Main_Date"  HeaderText ="录入日期"   />
                 
< asp:BoundField  DataField ="Flow_Account_Name"  HeaderText ="帐户"   />
                 
< asp:BoundField  DataField ="Flow_Class_Name"  HeaderText ="类别" />
                 
< asp:BoundField  DataField ="Flow_Main_Income"  HeaderText ="增加"   />
                 
< asp:BoundField  DataField ="Flow_Main_Expense"  HeaderText ="减少"   />
                 
< asp:BoundField  DataField ="Flow_Family_Name"  HeaderText ="人员"   />
                 
< asp:BoundField  DataField ="Flow_Main_Info"  HeaderText ="摘要"   />
             
</ Columns >
             
< RowStyle  BackColor ="#F7F7DE"  HorizontalAlign ="Center"   />
             
< SelectedRowStyle  BackColor ="#CE5D5A"  Font-Bold ="True"  ForeColor ="White"   />
             
< PagerStyle  BackColor ="#F7F7DE"  ForeColor ="Black"  HorizontalAlign ="Right"   />
             
< HeaderStyle  BackColor ="#6B696B"  Font-Bold ="True"  ForeColor ="White"   />
             
< AlternatingRowStyle  BackColor ="White"   />
             
< PagerSettings  Mode ="NumericFirstLast"  NextPageText ="[下一页]"  PreviousPageText ="[上一页]"   />
         
</ asp:GridView >

 

 

     /// <summary>
    
/// 获取或设置选中项的集合
    
/// </summary>

     protected  ArrayList SelectedItems
    
{
        
get
        
{
            
return (ViewState["mySelectedItems"!= null? (ArrayList)ViewState["mySelectedItems"] : null;
        }

        
set
        
{
            ViewState[
"mySelectedItems"= value;
        }

    }


 
/// <summary>
    
/// 从当前页收集选中项的情况
    
/// </summary>

     protected   void  CollectSelected()
    
{
        ArrayList selectedItems 
= null;
        
if (this.SelectedItems == null)
            selectedItems 
= new ArrayList();
        
else
            selectedItems 
= this.SelectedItems;

        
for (int i = 0; i < this.gridFlow.Rows.Count; i++)
        
{
           
// string id = this.gridFlow.Rows[i].Cells[1].Text;
            string id = gridFlow.DataKeys[i].Value.ToString();
            CheckBox cb 
= this.gridFlow.Rows[i].FindControl("CheckBox1"as CheckBox;
            
if (selectedItems.Contains(id) && !cb.Checked)
                selectedItems.Remove(id);
            
if (!selectedItems.Contains(id) && cb.Checked)
                selectedItems.Add(id);
        }

        
this.SelectedItems = selectedItems;
    }


    
protected   void  gridFlow_DataBinding( object  sender, EventArgs e)
    
{
        
//在每一次重新绑定之前,需要调用CollectSelected方法从当前页收集选中项的情况
        CollectSelected();
    }


    
protected   void  gridFlow_RowDataBound( object  sender, GridViewRowEventArgs e)
    
{
        
//这里的处理是为了回显之前选中的情况
        if (e.Row.RowIndex > -1 && this.SelectedItems != null)
        
{
            DataRowView row 
= e.Row.DataItem as DataRowView;
            CheckBox cb 
= e.Row.FindControl("CheckBox1"as CheckBox;
            
if (this.SelectedItems.Contains(row["id"].ToString()))
                cb.Checked 
= true;
            
else
                cb.Checked 
= false;
        }

   }


   
/// <summary>
    
/// 删除选中项的记录
    
/// </summary>

     protected   void  btnDel_Click( object  sender, EventArgs e)
    
{
        
//最后,需要对选中项进行操作之前,不能忘了还要最后一次收集当前页的选中情况
        CollectSelected();
        
string tempSelected = string.Empty;
        
foreach (object tmp in this.SelectedItems)
        tempSelected 
+= tmp.ToString() + ",";
        tempSelected 
= tempSelected.Remove((tempSelected.Length - 1), 1);

        
int result;
        
string errmsg = string.Empty;
        result 
= ModuleSql.SqlDelete("Flow_Main""id in (" + tempSelected + ")"ref errmsg);
        
if (result == 0x0)
        
{
            
// 成功
            ShowMessage(UrlCurrentPage, app.MessageDefaultStayTime, "删除成功");
        }

        
else
        
{
            ShowMessage(errmsg, app.MessageDefaultStayTime, errmsg);
//错误提示
        }
 
    }
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值