凝霜若雪

博客园 首页 新随笔 联系 订阅 管理
  11 Posts :: 0 Stories :: 10 Comments :: 0 Trackbacks

2010年1月4日 #

C#反射属性例子
 

程序集包含模块,而模块包含类型,类型又包含成员。反射则提供了封装程序集、模块和类型的对象。您可以使用反射动态地创建类型的实例,将类型绑定到现有对象,或从现有对象中获取类型。然后,可以调用类型的方法或访问其字段和属性。反射通常具有以下用途:

使用 Assembly 定义和加载程序集,加载在程序集清单中列出的模块,以及从此程序集中查找类型并创建该类型的实例。

使用 Module 了解如下的类似信息:包含模块的程序集以及模块中的类等。您还可以获取在模块上定义的所有全局方法或其他特定的非全局方法。

使用 ConstructorInfo 了解以下信息:构造函数的名称、参数、访问修饰符(如 public 或 private)和实现详细信息(如 abstract 或 virtual)等。使用 Type 的 GetConstructors 或 GetConstructor 方法来调用特定的构造函数。

使用 MethodInfo 了解以下信息:方法的名称、返回类型、参数、访问修饰符(如 public 或 private)和实现详细信息(如 abstract 或 virtual)等。使用 Type 的 GetMethods 或 GetMethod 方法来调用特定的方法。

使用 FieldInfo 了解以下信息:字段的名称、访问修饰符(如 public 或 private)和实现详细信息(如 static)等;并获取或设置字段值。

使用 EventInfo 来了解如下的类似信息:事件的名称、事件处理程序数据类型、自定义属性、声明类型和反射类型等;并添加或移除事件处理程序。

使用 PropertyInfo 来了解如下的类似信息:属性的名称、数据类型、声明类型、反射类型和只读或可写状态等;并获取或设置属性值。

使用 ParameterInfo 来了解如下的类似信息:参数的名称、数据类型、参数是输入参数还是输出参数,以及参数在方法签名中的位置等。

当您在一个应用程序域的仅反射上下文中工作时,请使用 CustomAttributeData 来了解有关自定义属性的信息。使用 CustomAttributeData,您不必创建属性的实例就可以检查它们。[FROM:MSDN]

通过一个示例来简要说明:

using System;
using System.Reflection;

namespace testReflection
{
    public delegate void TestDelegate(string info);
    class Program
    {
        static void Main(string[] args)
        {
             //从Dll中加载
             //Assembly ass = Assembly.LoadFile(@"TestReflect.dll");
             //Type myType = ass.GetType("testReflection.Person");
             //object aPerson = ass.CreateInstance("Person");

             //取得类型
            Type myType = Type.GetType ("testReflection.Person");
             //构造函数要用到的参数
            object [] constuctParms = new object[] { "Brad Pitt" };
            //创建实例
            //object TestName = Assembly.GetAssembly(myType).CreateInstance("Person");
            object aPerson = Activator.CreateInstance (myType, constuctParms);

             //使用MethodInfo 和Invoke 调用方法
            MethodInfo displayInfoMethod = myType.GetMethod ("DisplayInfo");
            displayInfoMethod .Invoke(aPerson, new object[] { "Using Invoke to call Method DisplayInfo()" });

            //使用InvokeMember 调用方法
            //调用方法的一些标志位
             BindingFlags flag = BindingFlags .InvokeMethod | BindingFlags.Public | BindingFlags.Instance;
            myType.InvokeMember("DisplayInfo", flag, null, aPerson, new object[] { "Using InvokeMethod to call DisplayInfo()" });
             //如果方法有返回值
             string name = (string)myType.InvokeMember("getName" , flag, null , aPerson, null );
            System.Console.WriteLine( "call getName(), return: " + name );

            //设置属性值
            myType.InvokeMember("Age" , BindingFlags.SetProperty, null, aPerson, new object[] { 30 });
            //得到属性值
             int age = (int)myType.InvokeMember( "Age", BindingFlags.GetProperty, null, aPerson, null);
            System.Console .WriteLine("Get the property of Age : " + Convert.ToString(age));

             //设置字段值
            myType.InvokeMember ("Name", BindingFlags.SetField , null, aPerson , new object[] { "Michal Jodn" });
             //获取字段值
            string fname = ( string)myType.InvokeMember("Name", BindingFlags.GetField, null , aPerson, null );
            System.Console.WriteLine("Get the Field Value of Name : " + fname);

            myType.InvokeMember("DisplayName", flag, null, aPerson, null);

            //获得方法集
             MethodInfo[] methods = myType.GetMethods();
            foreach (MethodInfo m in methods) {
                System.Console.WriteLine(m.Name);
            }
            //同样还有:GetFiedls()、GetProperties()、GetEvents()等方法

            //使用Delegate
            //此方法是静态的,所以必须提供委托类型。
            TestDelegate dg = (TestDelegate)Delegate.CreateDelegate( typeof(testReflection.TestDelegate), aPerson, "DisplayInfo");
            dg ("Test Delegate by call DisplayInfo()");
            
           
            //获得解决方案的所有Assembly
            Assembly[] AX = AppDomain.CurrentDomain.GetAssemblies();
            //遍历显示每个Assembly的名字
            foreach (object var in AX ) {
                Console.WriteLine ("Assembly的名字:"+var.ToString());               
            }
             //使用一个已知的Assembly名称,来创建一个Assembly
             //通过CodeBase属性显示最初指定的程序集的位置
            Console.WriteLine ("最初指定的程序集TestReflection的位置:" + Assembly.Load("TestReflection").CodeBase);

            System.Console.ReadLine();

         }
    }   

    public class Person
    {
        public string Name;
        private int _Age;
        public int Age
        {
            get
            {
                 return _Age;
             }
            set
            {
                _Age = value;
            }
        }

        public Person (string Name)
         {
            this.Name = Name;
        }
        public void DisplayInfo(string info)
        {
            System.Console.WriteLine(info );
            System.Console.WriteLine( "called sucessfully!");
        }
         public void DisplayName()
        {
            System .Console.WriteLine(Name);
        }
        public string getName()
        {
             return Name;
        }

    }
}
说明:
使用反射动态调用类成员,需要Type类的一个方法:InvokeMember。对该方法的声明如下:

public object InvokeMember(
   string name,
   BindingFlags invokeAttr,
   Binder binder,
   object target,
   object[] args
);

参数

name
String,它包含要调用的构造函数、方法、属性或字段成员的名称。
- 或 -
空字符串 (""),表示调用默认成员。

invokeAttr
一个位屏蔽,由一个或多个指定搜索执行方式的 BindingFlags 组成。访问可以是 BindingFlags 之一,如 Public、NonPublic、Private、InvokeMethod 和 GetField 等。不需要指定查找类型。如果省略查找类型,则将应用 BindingFlags.Public | BindingFlags.Instance。

binder
一个 Binder 对象,该对象定义一组属性并启用绑定,而绑定可能涉及选择重载方法、强制参数类型和通过反射调用成员。
- 或 -
若为空引用(Visual Basic 中为 Nothing),则使用 DefaultBinder。

target
在其上调用指定成员的 Object。

args
包含传递给要调用的成员的参数的数组。

返回值
表示被调用成员的返回值的 Object。

下列 BindingFlags 筛选标志可用于定义包含在搜索中的成员:

为了获取返回值,必须指定 BindingFlags.Instance 或 BindingFlags.Static。
指定 BindingFlags.Public 可在搜索中包含公共成员。
指定 BindingFlags.NonPublic 可在搜索中包含非公共成员(即私有成员和受保护的成员)。
指定 BindingFlags.FlattenHierarchy 可包含层次结构上的静态成员。

下列 BindingFlags 修饰符标志可用于更改搜索的执行方式:
BindingFlags.IgnoreCase,表示忽略 name 的大小写。
BindingFlags.DeclaredOnly,仅搜索 Type 上声明的成员,而不搜索被简单继承的成员。

可以使用下列 BindingFlags 调用标志表示要对成员采取的操作:
CreateInstance,表示调用构造函数。忽略 name。对其他调用标志无效。
InvokeMethod,表示调用方法,而不调用构造函数或类型初始值设定项。
对 SetField 或 SetProperty 无效。
GetField,表示获取字段值。对 SetField 无效。
SetField,表示设置字段值。对 GetField 无效。
GetProperty,表示获取属性。对 SetProperty 无效。
SetProperty 表示设置属性。对 GetProperty 无效。

posted @ 2010-01-04 15:16 侯唯 阅读(191) 评论(0) 编辑

2009年11月4日 #

using System;
using System.Collections.Generic;
using System.Text;
using System.IO;
using Microsoft.Win32;
using System.Diagnostics;

namespace Uni.UniCustoms
{
    public class clsWinrar
    {
        /// <summary>
        /// 是否安装了Winrar
        /// </summary>
        /// <returns></returns>
        static public bool Exists()
        {
            RegistryKey the_Reg = Registry.LocalMachine.OpenSubKey(@"SOFTWARE\Microsoft\Windows\CurrentVersion\App Paths\WinRAR.exe");
            return !string.IsNullOrEmpty(the_Reg.GetValue("").ToString());
        }

        /// <summary>
        /// 打包成Rar
        /// </summary>
        /// <param name="patch"></param>
        /// <param name="rarPatch"></param>
        /// <param name="rarName"></param>
        public void CompressRAR(string patch, string rarPatch, string rarName)
        {
            string the_rar;
            RegistryKey the_Reg;
            object the_Obj;
            string the_Info;
            ProcessStartInfo the_StartInfo;
            Process the_Process;
            try
            {
                the_Reg = Registry.LocalMachine.OpenSubKey(@"SOFTWARE\Microsoft\Windows\CurrentVersion\App Paths\WinRAR.exe");
                the_Obj = the_Reg.GetValue("");
                the_rar = the_Obj.ToString();
                the_Reg.Close();
                the_rar = the_rar.Substring(1, the_rar.Length - 7);
                Directory.CreateDirectory(patch);
                //命令参数
                //the_Info = " a    " + rarName + " " + @"C:Test?70821.txt"; //文件压缩
                the_Info = " a    " + rarName + " " + patch + " -r"; ;
                the_StartInfo = new ProcessStartInfo();
                the_StartInfo.FileName = the_rar;
                the_StartInfo.Arguments = the_Info;
                the_StartInfo.WindowStyle = ProcessWindowStyle.Hidden;
                //打包文件存放目录
                the_StartInfo.WorkingDirectory = rarPatch;
                the_Process = new Process();
                the_Process.StartInfo = the_StartInfo;
                the_Process.Start();
                the_Process.WaitForExit();
                the_Process.Close();
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
        /// <summary>
        /// 解压
        /// </summary>
        /// <param name="unRarPatch"></param>
        /// <param name="rarPatch"></param>
        /// <param name="rarName"></param>
        /// <returns></returns>
        public string unCompressRAR(string unRarPatch, string rarPatch, string rarName)
        {
            string the_rar;
            RegistryKey the_Reg;
            object the_Obj;
            string the_Info;


            try
            {
                the_Reg = Registry.LocalMachine.OpenSubKey(@"SOFTWARE\Microsoft\Windows\CurrentVersion\App Paths\WinRAR.exe");
                the_Obj = the_Reg.GetValue("");
                the_rar = the_Obj.ToString();
                the_Reg.Close();
                //the_rar = the_rar.Substring(1, the_rar.Length - 7);

                if (Directory.Exists(unRarPatch) == false)
                {
                    Directory.CreateDirectory(unRarPatch);
                }
                the_Info = "x " + rarName + " " + unRarPatch + " -y";

                ProcessStartInfo the_StartInfo = new ProcessStartInfo();
                the_StartInfo.FileName = the_rar;
                the_StartInfo.Arguments = the_Info;
                the_StartInfo.WindowStyle = ProcessWindowStyle.Hidden;
                the_StartInfo.WorkingDirectory = rarPatch;//获取压缩包路径

                Process the_Process = new Process();
                the_Process.StartInfo = the_StartInfo;
                the_Process.Start();
                the_Process.WaitForExit();
                the_Process.Close();
            }
            catch (Exception ex)
            {
                throw ex;
            }
            return unRarPatch;
        }
    }
}

RAR参数:

一、压缩命令
1、将temp.txt压缩为temp.rarrar a temp.rar temp.txt
2、将当前目录下所有文件压缩到temp.rarrar a temp.rar *.*
3、将当前目录下所有文件及其所有子目录压缩到temp.rarrar a temp.rar *.* -r
4、将当前目录下所有文件及其所有子目录压缩到temp.rar,并加上密码123rar a temp.rar *.* -r -p123

二、解压命令
1、将temp.rar解压到c:\temp目录rar e temp.rar c:\temprar e *.rar c:\temp(支持批量操作)
2、将temp.rar解压到c:\temp目录,并且解压后的目录结构和temp.rar中的目录结构一


压缩目录test及其子目录的文件内容
Wzzip test.zip test -r -P
WINRAR A test.rar test -r

删除压缩包中的*.txt文件
Wzzip test.zip *.txt -d
WinRAR d test.rar *.txt


刷新压缩包中的文件,即添加已经存在于压缩包中但更新的文件
Wzzip test.zip test -f
Winrar f test.rar test


更新压缩包中的文件,即添加已经存在于压缩包中但更新的文件以及新文件
Wzzip test.zip test -u
Winrar u test.rar test


移动文件到压缩包,即添加文件到压缩包后再删除被压缩的文件
Wzzip test.zip -r -P -m
Winrar m test.rar test -r


添加全部 *.exe 文件到压缩文件,但排除有 a或b
开头名称的文件
Wzzip test *.exe -xf*.* -xb*.*
WinRAR a test *.exe -xf*.* -xb*.*


加密码进行压缩
Wzzip test.zip test
-s123。注意密码是大小写敏感的。在图形界面下打开带密码的压缩文件,会看到+号标记(附图1)。
WINRAR A test.rar test -p123
-r。注意密码是大小写敏感的。在图形界面下打开带密码的压缩文件,会看到*号标记(附图2)。


按名字排序、以简要方式列表显示压缩包文件
Wzzip test.zip -vbn
Rar l test.rar


锁定压缩包,即防止未来对压缩包的任何修改
无对应命令
Winrar k test.rar


创建360kb大小的分卷压缩包
无对应命令
Winrar a -v360 test


带子目录信息解压缩文件
Wzunzip test -d
Winrar x test -r


不带子目录信息解压缩文件
Wzunzip test
Winrar e test


解压缩文件到指定目录,如果目录不存在,自动创建
Wzunzip test newfolder
Winrar x test newfolder


解压缩文件并确认覆盖文件
Wzunzip test -y
Winrar x test -y


解压缩特定文件
Wzunzip test *.txt
Winrar x test *.txt


解压缩现有文件的更新文件
Wzunzip test -f
Winrar x test -f


解压缩现有文件的更新文件及新文件
Wzunzip test -n
Winrar x test -u


批量解压缩文件
Wzunzip *.zip
WinRAR e *.rar


本文来自CSDN博客,转载请标明出处:http://blog.csdn.net/spring21st/archive/2009/10/12/4659430.aspx

posted @ 2009-11-04 20:51 侯唯 阅读(279) 评论(0) 编辑

2009年3月11日 #

posted @ 2009-03-11 23:55 侯唯 阅读(967) 评论(3) 编辑

2008年12月2日 #

摘要: 一个出库单里,处理两个订单的出库业务(如下图),其中的物料需要按批次进出库。该物料的明细批次,只有三条记录:当在生产单编号为 2008120201中,已使用了这三批次的数量(如下图),如何才能在填写生产单编号为2008092201的明细数量时,只允许20081130003批能出库122斤,其余的均提示:库存数量不足。以上数据都还未保存到数据库。阅读全文
posted @ 2008-12-02 18:45 侯唯 阅读(241) 评论(0) 编辑

2008年6月9日 #

提交当前行的修改
using DevExpress.XtraGrid;
using DevExpress.XtraGrid.Views.Base;
using System.Data.Common;
//...
public void UpdateDatasource(GridControl grid) {
    //Save the latest changes to the bound DataTable
    ColumnView view = (ColumnView)grid.FocusedView;
    view.CloseEditor();
    if(!view.UpdateCurrentRow()) return;
   
    //Update the database's Suppliers table to which oleDBDataAdapter1 is connected
    DoUpdate(oleDbDataAdapter1, dataSet11.Tables["Suppliers"]);
    //Update the database's Products table to which the oleDbDataAdapter2 is connected
    DoUpdate(oleDbDataAdapter2, dataSet11.Tables["Products"]);
}
 
public void DoUpdate(DbDataAdapter dataAdapter, System.Data.DataTable dataTable) {
    try {
        dataAdapter.Update(dataTable);
    } catch(Exception ex) {
        MessageBox.Show(ex.Message);
    }
}

 

从非绑定数据源得到数据
private void Form1_Load(object sender, System.EventArgs e) {
   // ...
 
   // Create an unbound column.
   GridColumn unbColumn = gridView1.Columns.Add("Total");
   unbColumn.VisibleIndex = gridView1.Columns.Count;
   unbColumn.UnboundType = DevExpress.Data.UnboundColumnType.Decimal;
   // Disable editing.
   unbColumn.OptionsColumn.AllowEdit = false;
   // Specify format settings.
   unbColumn.DisplayFormat.FormatType = DevExpress.Utils.FormatType.Numeric;
   unbColumn.DisplayFormat.FormatString = "c";
   // Customize the appearance settings.
   unbColumn.AppearanceCell.BackColor = Color.LemonChiffon;
}
 
// Returns the total amount for a specific row.
decimal getTotalValue(ColumnView view, int rowHandle) {
   decimal unitPrice = Convert.ToDecimal(view.GetRowCellValue(rowHandle, "UnitPrice"));
   decimal quantity = Convert.ToDecimal(view.GetRowCellValue(rowHandle, "Quantity"));
   decimal discount = Convert.ToDecimal(view.GetRowCellValue(rowHandle, "Discount"));
   return unitPrice * quantity * (1 - discount);
}
 
// Provides data for the Total column.
private void gridView1_CustomUnboundColumnData(object sender, DevExpress.XtraGrid.Views.Base.CustomColumnDataEventArgs e) {
   if(e.Column.FieldName == "Total" &&  e.IsGetData) e.Value = getTotalValue(sender as ColumnView, e.RowHandle);
}

 

运行时绑定到实现Ilist接口的数据源
public class Record {
   int id, age;
   string name;
   public Record(int id, string name, int age) {
      this.id = id;
      this.name = name;
      this.age = age;
   }
   public int ID { get { return id; } }
   public string Name {
      get { return name; }
      set { name = value; }
   }
   public int Age {
      get { return age; }
      set { age = value; }
   }
}

ArrayList listDataSource = new ArrayList();
listDataSource.Add(new Record(1, "Jane", 19));
listDataSource.Add(new Record(2, "Joe", 30));
listDataSource.Add(new Record(3, "Bill", 15));
listDataSource.Add(new Record(4, "Michael", 42));
gridControl1.DataSource = listDataSource;
gridControl1.MainView.PopulateColumns();

自定义列:
[C#]
DevExpress.XtraGrid.Views.Base.ColumnView view = gridControl1.MainView as DevExpress.XtraGrid.Views.Base.ColumnView;
DialogResult answer = MessageBox.Show("Do you want to create columns for all fields?", "Confirmation", MessageBoxButtons.YesNo, MessageBoxIcon.Question);
if (answer == DialogResult.Yes)
   view.PopulateColumns();
else {
   string[] fieldNames = new string[] {"ProductID", "ProductName", "QuantityPerUnit", "UnitPrice"};
   DevExpress.XtraGrid.Columns.GridColumn column;
   view.Columns.Clear();
   for (int i = 0; i < fieldNames.Length; i++) {
      column = view.Columns.Add(fieldNames[i]);
      column.VisibleIndex = i;
   }
}
GridColumn主要属性
Property
 Description
 
GridColumn.Name
 设计时定义的列名
 
GridColumn.FieldName
 绑定到的数据源中的列名
 
GridColumn.AbsoluteIndex
 在grid中的绝对位置索引
 
GridColumn.ColumnHandle
 指定关联数据源列名的标识,它不一定是唯一的,因为一个数据源列可以关联到一个Grid中的多个列.
 

 
手动创建Band
// obtaining the main view and clearing its bands collection
BandedGridView view = gridControl1.MainView as BandedGridView;
view.Bands.Clear();
// creating the bands layout
GridBand bandGeneral = view.Bands.Add("General Info");
GridBand bandTechnical = view.Bands.Add("Technical Info");
GridBand bandEngine = bandTechnical.Children.Add("Engine Info");
GridBand bandTransmission = bandTechnical.Children.Add("Transmission Info");
// assigning columns to bands
colTrademark.OwnerBand = bandGeneral;
colModel.OwnerBand = bandGeneral;
colLiter.OwnerBand = bandEngine;
colCylinders.OwnerBand = bandEngine;
colSpeedCount.OwnerBand = bandTransmission;
colTransmission.OwnerBand = bandTransmission;

 

如何定位和查找指定列显示值的行(注意是列的实显示值,而不是关联数据源列值)
using DevExpress.XtraGrid.Views.Base;
using DevExpress.XtraGrid.Columns;
// ...
 
string searchText = "Japan";
// obtaining the focused view
ColumnView view = (ColumnView)gridControl1.FocusedView;
// obtaining the column bound to the Country field
GridColumn column = view.Columns["Country"];
if(column != null) {
// locating the row
//如果用数据源中的列值,请用ColumnView.LocateByValue
    int rhFound = view.LocateByDisplayText(view.FocusedRowHandle + 1, column, searchText);
    // focusing the cell
    if(rhFound != GridControl.InvalidRowHandle) {
        view.FocusedRowHandle = rhFound;
        view.FocusedColumn = column;
    }
}

 

另一个查找示例

DevExpress.XtraGrid.Views.Base.ColumnView view = gridControl1.MainView as DevExpress.XtraGrid.Views.Base.ColumnView;
view.BeginUpdate();
try {
   int rowHandle = 0;
   DevExpress.XtraGrid.Columns.GridColumn col = view.Columns["Category"];
   while(true) {
      // locating the next row
      rowHandle = view.LocateByValue(rowHandle, col, "SPORTS");
      // exiting the loop if no row is found
      if (rowHandle == DevExpress.XtraGrid.GridControl.InvalidRowHandle)
         break;
      // perform specific operations on the row found here
      // ...
      rowHandle++;
   }
} finally { view.EndUpdate(); }

 

将特定编辑框绑定到列
默认的cell编辑框是不可以改变的,即使是在运行时,因为它们是动态创建和注销的。
要想定制,就在设计时修改ColumnEdit吧。
using DevExpress.XtraEditors.Repository;
 
//Create a repository item for a combo box editor
RepositoryItemComboBox riCombo = new RepositoryItemComboBox();
riCombo.Items.AddRange(new string[] {"London", "Berlin", "Paris"});
//Add the item to the internal repository
gridControl1.RepositoryItems.Add(riCombo);
//Now you can define the repository item as an in-place column editor
colCity.ColumnEdit =  riCombo;

另一个运行时绑定列编辑框示例
private void gridView1_CustomRowCellEdit(object sender, DevExpress.XtraGrid.Views.Grid.CustomRowCellEditEventArgs e) {
   if (e.Column.FieldName == "FieldName") return;
   DevExpress.XtraGrid.Views.Grid.GridView gv = sender as DevExpress.XtraGrid.Views.Grid.GridView;
   string fieldName = gv.GetRowCellValue(e.RowHandle, gv.Columns["FieldName"]).ToString();
   switch (fieldName) {
      case "Population":
         e.RepositoryItem = repositoryItemSpinEdit1;
         break;
      case "Country":
         e.RepositoryItem = repositoryItemComboBox1;
         break;
      case "Capital":
         e.RepositoryItem = repositoryItemCheckEdit1;
         break;
   }          
}

 

检验录入数据是否有效

using DevExpress.XtraGrid.Views.Grid;
using DevExpress.XtraGrid.Columns;
 
public bool isValidDate(int day, int month, int year) {
    return (day > 0) && (month > 0) && (month <= 12) && (year > 1980) && (year < 2100) && (day <= DateTime.DaysInMonth(year, month));
}
 
private void gridView1_ValidateRow(object sender, DevExpress.XtraGrid.Views.Base.ValidateRowEventArgs e) {
    GridView view = sender as GridView;
    GridColumn colDay = view.Columns["Day"];
    GridColumn colMonth = view.Columns["Month"];
    GridColumn colYear = view.Columns["Year"];
    int day = (int)view.GetRowCellValue(e.RowHandle, colDay);
    int month = (int)view.GetRowCellValue(e.RowHandle, colMonth);
    int year = (int)view.GetRowCellValue(e.RowHandle, colYear);
    e.Valid = isValidDate(day, month, year);
    if(!e.Valid) {
        view.SetColumnError(colDay, "Check the day");
        view.SetColumnError(colMonth, "Check the month");
        view.SetColumnError(colYear, "Check the year");
    }
}

 


 MouseMove捕捉


  private void Grid_MouseMove(object sender, System.Windows.Forms.MouseEventArgs e)
  {
  ShowHitInfo(this.gridView1.CalcHitInfo(new Point(e.X, e.Y)));
  }

  private void ShowHitInfo(DevExpress.XtraGrid.Views.Grid.ViewInfo.GridHitInfo hi)
  {
   DevExpress.XtraGrid.Views.Base.ColumnView cgv =
    (DevExpress.XtraGrid.Views.Base.ColumnView)Grid.MainView;

   string columnName = hi.Column == null ? "No column" : hi.Column.Caption;
   switch(columnName)
   {
       case "账号":
     txtUserName.Text = cgv.GetRowCellDisplayText(hi.RowHandle,hi.Column);
     break;
    case "密码":
     txtPassword.Text = cgv.GetRowCellDisplayText(hi.RowHandle,hi.Column);
     break;
    case "真实姓名":
     txtRealName.Text = cgv.GetRowCellDisplayText(hi.RowHandle,hi.Column);
     break;
    case "电子邮件":
     txtEmail.Text = cgv.GetRowCellDisplayText(hi.RowHandle,hi.Column);
     break;
    case "角色":
     cbRole.Text = cgv.GetRowCellDisplayText(hi.RowHandle,hi.Column);
     break;
    default:
     txtUserName.Text = "Null";
     txtPassword.Text = "Null";
     txtRealName.Text = "Null";
     txtEmail.Text = "Null";
     cbRole.Text = "Null";
     break;
   }

posted @ 2008-06-09 01:40 侯唯 阅读(2602) 评论(0) 编辑

2008年5月29日 #

摘要: 上个月安装了Win2008,今天准备装VS等东西,结果发现,FastReport无论如何都安装不上去了,郁闷之极~~阅读全文
posted @ 2008-05-29 01:54 侯唯 阅读(71) 评论(0) 编辑

2007年12月17日 #

摘要: privatevoiddataGridView1_CellClick(objectsender,DataGridViewCellEventArgse){if(e.ColumnIndex>1&&e.ColumnIndex<5){boolt=(bool)dataGridView1[e.ColumnIndex,e.RowIndex].Value;t=!t;dataGridVi...阅读全文
posted @ 2007-12-17 23:38 侯唯 阅读(623) 评论(2) 编辑

2007年11月15日 #

摘要: C#中没有统计子字符串出现次数的函数,那么如何在C#求出字符串中某字符的出现次数,比如求“ADSFGEHERGASDF”中“A”出现的次数。首先想到的方法当然是从头遍历字符串并统计:c1 = 0;for (int i = 0; i < str.Length; i++){ if (str[i] == 'A') { c1++; }}第二种方法也很容...阅读全文
posted @ 2007-11-15 17:33 侯唯 阅读(3931) 评论(5) 编辑

摘要: 1//声明2publicenumDefaultType:int3{4布料=1,5商标=2,6钢弓=4,7棉碗=88}9//绑定到comboBox10comboBox3.DataSource=Enum.GetValues(typeof(Dal.Class1.DefaultType));11//取得comboBox选定值12privatevoidcomboBox3_Leave(objectsender...阅读全文
posted @ 2007-11-15 10:39 侯唯 阅读(71) 评论(0) 编辑

2007年11月6日 #

摘要: using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;阅读全文
posted @ 2007-11-06 18:35 侯唯 阅读(1077) 评论(0) 编辑