zoukankan      html  css  js  c++  java
  • 【原创】PDA 实现DataGrid可编辑

    PDA 实现DataGrid可编辑

    通过继承 DataGrid 扩展实现

    对ISupportInitialize 空实现,如没有,会出现异常。

    在PDA设备上不能直接对DataGrid的单元格进行编辑,那么,如何实现单元格可编辑呢?我们可以用TextBox来模拟单元格,让这个TextBox一开始隐藏起来,当点击DataGrid的单元格的时候,在当前单元格的位置显示TextBox.因此我们必须要先获得当前单元格的坐标,然后显示TextBox在该坐标,并且将当前单元格的内容赋给TextBox,当用户修改了TextBox的内容并且离开该单元格时,TextBox将再次被隐藏,同时,单元格的内容被赋为TextBox的最新内容。按照以上的思路我们可以写如下代码:

     1 using System;
     2 using System.Drawing;
     3 using System.Windows.Forms;
     4 using System.ComponentModel;
     5 
     6 namespace SmartDeviceProject1
     7 {
     8     public class datagridEx :DataGrid,ISupportInitialize
     9     {
    10         DataGridCell editcell = new DataGridCell();
    11         Rectangle cellpos = new Rectangle();
    12         TextBox txtedit = new TextBox();
    13         bool isUpdateMode = false;
    14         bool isEditMode = false;
    15 
    16         public datagridEx()
    17         {
    18             this.Controls.Add(txtedit);
    19             txtedit.Visible = false;
    20             txtedit.BringToFront();
    21             txtedit.Text = "grn";
    22         }
    23         protected override void OnCurrentCellChanged(EventArgs e)
    24         {
    25             if (isUpdateMode)
    26             {
    27                 base.OnCurrentCellChanged(e);
    28                 return;
    29             }
    30             DataGridCell currentcell = this.CurrentCell;
    31             if (isEditMode && !this.CurrentCell.Equals(editcell))
    32             {
    33                 isUpdateMode = true;
    34                 this[editcell.RowNumber, editcell.ColumnNumber] = txtedit.Text;
    35                 this.CurrentCell = currentcell;
    36                 this.Visible = true;
    37                 txtedit.Visible = false;
    38                 isUpdateMode = false;
    39                 isEditMode = false;
    40             }
    41             editcell = this.CurrentCell;
    42             cellpos = this.GetCellBounds(editcell.RowNumber, editcell.ColumnNumber);
    43             txtedit.Left = cellpos.Left  - 1;
    44             txtedit.Top = cellpos.Top  - 1;
    45             txtedit.Width = cellpos.Width + 2;
    46             txtedit.Height = cellpos.Height + 2;
    47 
    48             txtedit.Visible = true;            
    49             txtedit.Text = this[editcell.RowNumber, editcell.ColumnNumber].ToString();
    50 
    51             txtedit.BringToFront();
    52             txtedit.Focus();
    53             txtedit.SelectAll();
    54             isEditMode = true;
    55 
    56             base.OnCurrentCellChanged(e);
    57         }
    58 
    59         #region ISupportInitialize接口实现
    60         public void BeginInit()
    61         {            
    62         }
    63         public void EndInit()
    64         {
    65         }
    66         #endregion
    67 
    68     }   
    69 }

    当然,还可以增加回车跳转到下一单元格。

  • 相关阅读:
    HttpClient访问的策略Policy
    Fiddler 工具使用技巧
    设置Chrome浏览器User-Agent
    anaconda Script file Scriptspip-script.py is not present
    anaconda3+ paddleOCR安装使用
    anaconda 创建虚环境 必须指定python版本
    pyqt 启动GUI前启动子进程,退出GUI后退出子进程
    pyqt 扩展QsciScintilla disable mouse select
    pyqt designer下添加QsciScintilla 控件
    rdkit 读mol时保留H原子
  • 原文地址:https://www.cnblogs.com/LeeWenjie/p/2548215.html
Copyright © 2011-2022 走看看