zoukankan      html  css  js  c++  java
  • WPF DataGrid显示表格中编辑Check数据

    WPF DataGrid显示中编辑数据Check

    1、DataGrid显示数据选中后编辑

    //xaml 在DataGrid中加入事件
    BeginningEdit = "dataGrid_BeginningEdit" CellEditEnding = "dataGrid_CellEditEnding" selectionUnit = "Cell"
    //cs文件中加code 
        string collumnName = "";
    private void dataGrid_BeginningEdit(object sender, DataGridBeginningEditEventArgs e)
    {
        collumnName = dataGrid.CurrentColumn.Header.Tostring();
    }
    
    private void dataGrid_CellEditEnding(object sender, DataGridCellEditEndingEventArgs e)
    {
        string newValue = (e.EditingElement as TextBox).Text;
        int rowCount = this.getCellRow(dataGrid);
        if(collumnName == "LOCATION"){
            DataGridList[rowCount].LOCATION = newValue;
        }
        if(collumnName == "LOTTYPE"){
            DataGridList[rowCount].LOTTYPE = newValue;
        }
        if(collumnName == "QTY"){ //如果输入的数字需要Check
            if(checkIsNum(newValue) == false){ //验证newValue 如果不通过给初始值“0”
                newValue = "";
                DataGridList[rowCount].QTY = "0";
                this.DataGrid.DataContext = null;
                this.DataGrid.DataContext = DataGridList;
                return;
            }
            
            int nValue = 0;
            int waferQty = 0;
            int.TryParse(newValue, out nValue);
            int.TryParse(DataGridList[rowCount].oldQTY, out waferQty)
                if(nValue > waferQty){ //输入的数字大于当前行的oldQTY值,则报错
                    ErrorBox errBox = new ErrorBox(this,"Invalid Data", false);//自定义公共控件
                    errBox.Owner = this;
                    errBox.ShowDialog();
                    newValue = "0";
                }
            DataGridList[rowCount].QTY = newValue;
        }
        this.DataGrid.DataContext = null;
        this.DataGrid.DataContext = DataGridList;
    }
    private int getCellRow(DataGrid dg){
        int RowNum = 99;
        var cells = dg.SelectedCells;
        if(cells.Any()){ //集合中有任何一元素满足条件,返回就true 不带任何参数,表示集合中有元素
            			//如加参数委托,集合中有任何一个元素满足该条件就返回true
            RowNum = dg.Items.IndexOf(cells.First().Item);
        }
        return RowNum;
    }
    private bool checkIsNum(string str){
        Regex obj = new Regex(@"^[0-9]d*$");
        return obj.IsMatch(str);
    }
    

    2、文本框限制只输入数字

    //xaml定义事件
    PreviewTextInput = "qty_textInput"
    //cs Code
    private void qty_textInput(){
        Regex re = new Regex(@"^[0-9]*$");
        e.Handled = !re.IsMatch(e.Text);
    }    
    
  • 相关阅读:
    初拾Java(问题一:404错误,页面找不到)
    新年新气象--总结过去,展望未来
    接口测试[整理]
    [转]SVN-版本控制软件
    浅谈黑盒测试和白盒测试
    Bug管理工具的使用介绍(Bugger 2016)
    P2805/BZOJ1565 [NOI2009]植物大战僵尸
    近期学习目标
    P3643/BZOJ4584 [APIO2016]划艇
    P5344 【XR-1】逛森林
  • 原文地址:https://www.cnblogs.com/DingGuo/p/14303476.html
Copyright © 2011-2022 走看看