• 【原创】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 }

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

  • 相关阅读:
    ASP.NET中 LinkButton(链接按钮)的使用
    ASP.NET中 HTML标签总结及使用
    ASP.NET中 TextBox(文本框)的使用
    ASP.NET中 ListView(列表视图)的使用前台绑定
    ASP.NET中 Repeater 的使用前台绑定
    ASP.NET中 FormView(表单视图)的使用前台绑定
    ASP.NET中 Button(按钮)的使用
    ASP.NET中 RadioButtonList(单选按钮组),CheckBoxList(复选框组),DropDownList(下拉框),ListBox(列表框),BulletedList(无序列表)的使用前台绑定
    ASP.NET中 GridView(网格视图)的使用前台绑定
    ASP.NET中 WebForm 窗体控件使用及总结
  • 原文地址:https://www.cnblogs.com/LeeWenjie/p/2548215.html
Copyright © 2020-2023  润新知