zoukankan      html  css  js  c++  java
  • Dev+Grid复选框(全选,反选)

    DevExpress+Grid 全选反选记录

    一、先建一个辅助类

        public class GridControlHelp
        {
            /// <summary>
            /// draw checkbox
            /// </summary>
            /// <param name="e"></param>
            /// <param name="chk"></param>
            public static void DrawCheckBox(DevExpress.XtraGrid.Views.Grid.ColumnHeaderCustomDrawEventArgs e, bool chk)
            {
                DevExpress.XtraEditors.Repository.RepositoryItemCheckEdit repositoryCheck = e.Column.ColumnEdit as DevExpress.XtraEditors.Repository.RepositoryItemCheckEdit;
                if (repositoryCheck != null)
                {
                    System.Drawing.Graphics g = e.Graphics;
                    System.Drawing.Rectangle r = e.Bounds;
    
                    DevExpress.XtraEditors.ViewInfo.CheckEditViewInfo info;
                    DevExpress.XtraEditors.Drawing.CheckEditPainter painter;
                    DevExpress.XtraEditors.Drawing.ControlGraphicsInfoArgs args;
                    info = repositoryCheck.CreateViewInfo() as DevExpress.XtraEditors.ViewInfo.CheckEditViewInfo;
    
                    painter = repositoryCheck.CreatePainter() as DevExpress.XtraEditors.Drawing.CheckEditPainter;
    
                    info.EditValue = chk;
                    info.Bounds = r;
                    info.CalcViewInfo(g);
                    args = new DevExpress.XtraEditors.Drawing.ControlGraphicsInfoArgs(info, new DevExpress.Utils.Drawing.GraphicsCache(g), r);
                    painter.Draw(args);
                    args.Cache.Dispose();
                }
            }
    
            /// <summary>
            /// click checkbox event
            /// </summary>
            /// <param name="gridView"></param>
            /// <param name="fieldName"></param>
            /// <param name="currentStatus"></param>
            /// <returns></returns>
            public static bool ClickGridCheckBox(DevExpress.XtraGrid.Views.Grid.GridView gridView, string fieldName, bool currentStatus)
            {
                bool result = false;
                if (gridView != null)
                {
                    //禁止排序 
                    gridView.ClearSorting();
    
                    gridView.PostEditor();
                    DevExpress.XtraGrid.Views.Grid.ViewInfo.GridHitInfo info;
                    System.Drawing.Point pt = gridView.GridControl.PointToClient(Control.MousePosition);
                    info = gridView.CalcHitInfo(pt);
                    if (info.InColumn && info.Column != null && info.Column.FieldName == fieldName)
                    {
                        for (int i = 0; i < gridView.RowCount; i++)
                        {
                            gridView.SetRowCellValue(i, fieldName, !currentStatus);
                        }
                        return true;
                    }
                }
                return result;
            }
    
    
    
        }

    二、需要用到Grid控件的几个事件

    gvBk_Click控件click事件

    gvBk_CustomDrawColumnHeader表头重绘事件

    gvBk_DataSourceChanged数据源改变事件

    gvBk_RowCellClick单元格点击事件

    bool checkStatus = true;默认是否选中状态

    1.数据源中添加选中状态字段Bool类型,True选中,false取消选中

    DataTable table = EdiBookingBiz.GetBookingBySchid(ssd, user);//用vessel和voyage去找booking
     if (table.Rows.Count > 0)
    {
        table.Columns.Add("Chk", System.Type.GetType("System.Boolean"));
        table.Columns["Chk"].DefaultValue = Boolean.FalseString;
    }
    
    bool checkStatus = true;
    private void gvBk_Click(object sender, EventArgs e)
    {
        if (GridControlHelp.ClickGridCheckBox(gvBk, "Chk", checkStatus))
        {
           checkStatus = !checkStatus;
         }
    }
    
    private void gvBk_CustomDrawColumnHeader(object sender, ColumnHeaderCustomDrawEventArgs e)
    {
         if (e.Column != null && e.Column.FieldName == "Chk")
          {
             e.Info.InnerElements.Clear();
             e.Painter.DrawObject(e.Info);
             GridControlHelp.DrawCheckBox(e, checkStatus);
             e.Handled = true;
          }
    }
    
    private void gvBk_DataSourceChanged(object sender, EventArgs e)
    {
       GridColumn column = this.gvBk.Columns.ColumnByFieldName("Chk");
       if (column != null)
       {
          column.Width = 80;
          column.OptionsColumn.ShowCaption = false;
          column.ColumnEdit = new RepositoryItemCheckEdit();
        }
    }
    
     private void gvBk_RowCellClick(object sender, DevExpress.XtraGrid.Views.Grid.RowCellClickEventArgs e)
    {
      string columnName = gvBk.FocusedColumn.FieldName.ToString();
      if (columnName == "Chk")
      {
         DevExpress.XtraEditors.Repository.RepositoryItemCheckEdit repositoryCheck = e.Column.ColumnEdit as DevExpress.XtraEditors.Repository.RepositoryItemCheckEdit;
         string value = gvBk.GetFocusedRowCellValue(columnName).ToString();
         int hand = e.RowHandle;
         if (value == "True")
         {
           gvBk.SetRowCellValue(hand, "Chk", false);
          }
          else
          {
            gvBk.SetRowCellValue(hand, "Chk", true);
           }
          //修改后的值更新到数据源
          gvBk.CloseEditor();
          gvBk.UpdateCurrentRow();
      }
    }
    //默认全选的方法
    private  void SelectAll(DataTable tableSource)
    {
      if (!checkStatus) checkStatus = true;
      if (tableSource != null && tableSource.Rows.Count > 0)
      {
        foreach (DataRow item in tableSource.Rows)
        {
          item["Chk"] = true;
        }
        gridBk.DataSource = tableSource;
      }
    }
    

      

      

  • 相关阅读:
    AspNet WebApi 中应用fo-dicom抛出异常:No codec registered for tranfer syntax:
    Codeforces Round #261 (Div. 2)459A. Pashmak and Garden(数学题)
    Android开发之使用Web Service进行网络编程
    线段树 + 扫描线加深具体解释
    对Shell几个冷知识的总结(IFS,数组,替换,分割,查找)
    lscript.ld 链接器脚本
    iOS 系统地图实现及定位
    Perl怎样过滤html标签
    hdu1213 How Many Tables(并查集)
    数据结构——链表
  • 原文地址:https://www.cnblogs.com/stoneWl/p/10905924.html
Copyright © 2011-2022 走看看