简介

DataPropertyGrid,正如它的名字,是一个继承于PropertyGrid的扩展控件,它可以浏览和编辑数据。

开发背景

我们以前这样写:

绝对适合您的DataPropertyGrid!public class myClass


很不方便。

理解DataPropertyGrid控件 

DataPropertyGrid dynamically creates a class from the columns of the DataRow that passed as parameter in the MuestraDatos(System.Data.DataRow row) method. The generated class code is compiled in memory, and from it an instance or object is created and is shown by the SelectedObject property of the control.

In order to generate the class code that will represent the DataRow, I have preferred to use template code (string) instead of CodeDom, since it makes me easier and controlled to work with.

绝对适合您的DataPropertyGrid!string codigo = @"
绝对适合您的DataPropertyGrid!using System;
绝对适合您的DataPropertyGrid!using System.Data;
绝对适合您的DataPropertyGrid!using System.ComponentModel;
绝对适合您的DataPropertyGrid!using System.Reflection;
绝对适合您的DataPropertyGrid!
绝对适合您的DataPropertyGrid!namespace miNamespace {
绝对适合您的DataPropertyGrid![TypeConverter(typeof(PropertySorter))]
绝对适合您的DataPropertyGrid!public class miClase {
绝对适合您的DataPropertyGrid!            
绝对适合您的DataPropertyGrid!   private System.Data.DataRow m_Row = null;
绝对适合您的DataPropertyGrid!
";
绝对适合您的DataPropertyGrid!   
foreach(System.Data.DataTable tabla in this.DS.Tables)
}

I make iterations with the columns and tables associated of the DataRow in order to generate the fields, properties, attributes and enumerations. When the properties' values in the component are changed, immediately the respective fields in the DataRow are also changed.

绝对适合您的DataPropertyGrid!foreach(System.Data.DataColumn col in this.Tabla.Columns)
 codigo;

If the DataRow has related tables, an enumeration is generated that allows to show the data in combobox way with understandable values for the end user, instead of key values used in database normalization. For example, the Customer_ID field of the DataRow would generate a Customer property of type enumeration with the names of the different clients so that the user chooses names instead of the ID codes, in the case that there is a Dataset with a table of clients with columns [Customer_Id] representing the ID and [Customer] the name of the client. This allows, in addition, to validate the input data.

If the DataRow has an associated DataSet, an enumeration by each one of the additional tables will be created that it has and whose columns are the code of a column of the DataRow and another column with the description for this code. The name of the property no longer will be the name of the column of the DataRow, but the name of the column description of the associate table.

 绝对适合您的DataPropertyGrid!

使用DataPropertyGrid 

现在您可以把DataPropertyGrid加入到toolbox中,您可以在设计状态中修改它的属性。为了显示一条记录,您必须加入MuestraDatos方法,示例如下:
 

绝对适合您的DataPropertyGrid!this.dataPropertyGrid1.MuestraDatos
绝对适合您的DataPropertyGrid!        (
绝对适合您的DataPropertyGrid!        Row, 
//DataRow
绝对适合您的DataPropertyGrid!
        "OrderID,Active,CustomerID,EmployeeID,OrderDate," + 
绝对适合您的DataPropertyGrid!         
"ShippedDate,Freight,ShipCountry,ShipCity"//显示别名
绝对适合您的DataPropertyGrid!
        "Código,Activa,Cliente,Empleado,Fecha,Entrega," +
绝对适合您的DataPropertyGrid!         
"Precio,Pais,Ciudad"//Nombre de campos
绝对适合您的DataPropertyGrid!
        "General,General,General,General,Tiempo,Tiempo," + 
绝对适合您的DataPropertyGrid!         
"General,Localizacion,Localizacion"//Categorías
绝对适合您的DataPropertyGrid!
        "el Código de la orden,Define si la orden está activa o no," +
绝对适合您的DataPropertyGrid!         
"Cliente de la orden,Empleado que registró la orden,
绝对适合您的DataPropertyGrid!
        Fecha de la orden,Fecha de entrega,Precio Total,País de" +
绝对适合您的DataPropertyGrid!
         " recepcion,Ciudad de recepción"//Descripciones
绝对适合您的DataPropertyGrid!
        "1,2,3,4,5,6,7,8,9"//Orden
绝对适合您的DataPropertyGrid!
        "1,0,0,0,0,0,0,0,0" //ReadOnly 1-true. 0-false
绝对适合您的DataPropertyGrid!
        );

这个控件使用很简单,具体的请参阅
http://www.codeproject.com/cs/miscctrl/DataPropertyGrid.asp#xx1096777xx

相关文章:

  • 2021-05-25
  • 2021-12-21
  • 2021-09-10
  • 2022-12-23
  • 2021-09-25
  • 2021-08-14
  • 2021-12-20
猜你喜欢
  • 2021-09-07
  • 2021-08-28
  • 2021-10-06
  • 2021-10-11
  • 2021-06-12
  • 2021-11-19
  • 2021-08-23
相关资源
相似解决方案