在动态生成列时在Datagrid中进行验证。 [英] Validation in Datagrid when columns are dynamically generated.

查看:78
本文介绍了在动态生成列时在Datagrid中进行验证。的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下格式的数据。这些年是动态的,这意味着在1994年之后可以有更多的专栏。



I have data in the following format. The years are dynamic which means after 1994 there can be n more columns.

Block	Block Name	Area Sown	1989	1990	1991	1992	1993	1994
1	          5 losess	               10.1   	1000.1	702	        1003	1002	650	        1000
2	          4 losess	               100	           !!!	        1002	1003	1002	500	        1000
3	           2 losess	      1000	        1000	1002	1003	1002	1000	1000
4	           One loss           10000	        1000	1002	1003	1002	1000	1000



此数据必须在wpf中的数据网格上显示。此数据通常位于csv文件中,我将导入并显示在网格上。



这是一个名为Block的类,用于定义一行数据。


This data has to be shown on a datagrid in wpf. This data would generally be in a csv file which i would import and show on the grid.

The is a class called Block which defines one row of data.

public class Block
    {
        public ObservableCollection<DataCell> Yields
        {
            get;
            set;
        }

        public Block() { }

        public Block(ObservableCollection<DataCell> values)
        {
            Yields = values;
        }       
    }







数据中心定义为






The datacell is defined as

public class DataCell
    {
        string name;
        object _value;

        public string Name
        {
            get { return name; }
            private set { name = value; }
        }

        public object Value
        {
            get { return _value; }

            set
            {
                if (value.ToString().Contains('!'))
                    throw new ArgumentException("Special Characters not allowed");
                else if (String.IsNullOrEmpty(value.ToString()))
                    throw new ArgumentException("Cannot contain blanks.");
                else
                    _value = value;
            }
        }

        public DataCell() { }

        public DataCell(string name, object value)
        {
            Name = name;
            Value = value;
        }
    }







var records = new ObservableCollection<Block>();







var columns = records.First()
                            .Yields
                            .Select((x, i) => new { Name = x.Name, Index = i })
                            .ToArray();

            foreach (var column in columns)
            {
                var binding = new Binding(string.Format("Yields[{0}]", column.Index));
                dataGrid.Columns.Add(new CustomBoundColumn()
                {
                    Header = column.Name,
                    Binding = binding,
                    TemplateName = "CustomTemplate"
                });
            }





CustomTemplate用于在文本框工具提示上显示错误。



问题是,当我导入数据文件时,在DataCell类中抛出ApplicationException,这会使应用程序在此时停止,我无法继续。



如果我使用调试模式并手动运行程序并使用run to cursor继续,那么错误不会显示在datagrid中,但是在导入完成后然后如果我编辑一个错误显示的元素。



请建议我做错了什么或者更好的方法导入上面的数据并在datagrid中显示。



CustomTemplate is used to display the error on the textbox tooltip.

The issue is that when i import the data file, in the DataCell class the ApplicationException is thrown which makes the application stop at that point and i cannot proceed.

IF i use debug mode and manually run the program and use run to cursor to proceed then the error does not show in the datagrid, but after import is done then if i edit an element the error gets displayed.

Please suggest what i am doing wrong or any better way of importing the above data and showing in the datagrid.

推荐答案

这篇关于在动态生成列时在Datagrid中进行验证。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆