Type t = this.dataGridView1.GetType();
            DataGridViewColumnCollection cs;
            PropertyInfo pi = t.GetProperty("Columns" );
            cs = (DataGridViewColumnCollection)pi.GetValue(this.dataGridView1, null);
            foreach (DataGridViewColumn c in cs)
            {
                MessageBox.Show(c.Name +"    "+c.HeaderText );            }得到比较容易一些.但如何通过反射动态的设置各个列的属性呢???

解决方案 »

  1.   

    给你参考下
            /// <summary>
            /// 设置属性集合
            /// </summary>
            /// <param name="properties"></param>
            public void SetProerties(NameValueCollection properties)
            {
                foreach (string key in properties)
                {
                    Type subType = this.GetType();
                    PropertyInfo prop = subType.GetProperty(key);
                    if (prop != null)
                        prop.SetValue(this, TypeDescriptor.GetConverter(prop.PropertyType).ConvertFrom(properties.Get(key)),null);
                }
            }
      

  2.   

    用SetValue方法     pi.SetValue("对象的实例","要设置的值",null)
      

  3.   

    你都已经获得了,设置还是问题吗直接在你的foreach里设置就好了,比如说列的宽度这个属性。foreach (DataGridViewColumn c in cs)
                {
                    MessageBox.Show(c.Name +"    "+c.HeaderText );                c.Width= 150
                }
      

  4.   

    先不论楼主的目的是什么,仅从通过反射设置值这个方面来说,1楼的示例就可以,具体的还是参考一下MSDN。这里帖一下比较复杂的,可以实际应用,大家可作个参考:/// <summary>
    /// 设置对象属性的值,如果对象的属性依然是对象,会创建对应的空对象并设置相应的值
    /// 需要注意的是,当采用orm框架时,这样使用会产生问题,创建的对象与orm并没有联系。
    /// </summary>
    /// <param name="instance"></param>
    /// <param name="propertyName"></param>
    /// <param name="value"></param>
    /// <param name="throwException"></param>
    public static void SetPropertyValue(object instance, string propertyName, object value, bool eption)
    {
        string[] st1 = propertyName.Split('.');
        int n = st1.Length;    object obj = instance;
        object val = null;
        if (n > 1)
        {
            for (int i = 0; i < n - 1; i++)
            {
                PropertyInfo pi = obj.GetType().GetProperty(st1[i]);
                if (pi == null)
                {
                    string err = String.Format("类型{0}中不存在名为{1}的属性", obj.GetType(), st1[i]);
                    if (throwException)
                        throw new Exception(err);
                    else
                    {
                        // TODO 记录日志
                        return;
                    }
                }            val = pi.GetValue(obj, null);
                if (val == null)
                {
                    val = Activator.CreateInstance(pi.PropertyType,true);
                    pi.SetValue(obj, val, null);
                }
                obj = val;
            }
        }    PropertyInfo lpi = obj.GetType().GetProperty(st1[n - 1]);
        if (lpi == null)
        {
            string err = String.Format("类型{0}中不存在名为{1}的属性", obj.GetType(), st1[n - 1]);
            if (throwException)
                throw new Exception(err);
            else
            {
                // TODO 记录日志
                return;
            }
        }    object val1;
        Type t = lpi.PropertyType;
        if (value != null)
        {
            if (t.IsGenericType)
            {
                Type[] t1 = t.GetGenericArguments();            // TODO 这是临时的判断,用于解决当value是空字符串时,转换会失败
                // 应该将转换做为一个函数。
                if (value.ToString().Length > 0)
                {
                    val1 = Convert.ChangeType(value, t1[0]);
                }
                else
                {
                    val1 = null;
                }
            }
            else
            {
                if (lpi.PropertyType.BaseType == typeof(Enum))
                {
                    val1 = Enum.ToObject(lpi.PropertyType, Int32.Parse(value.ToString()));
                }
                else if (lpi.PropertyType == typeof(int))
                {
                    // TODO 这是临时的做法,十分危险,一定要找到解决方案。
                    val1 = Convert.ChangeType(value, lpi.PropertyType);
                }
                else
                {
                    val1 = value;
                }
            }
        }
        else // value is null
        {
            val1 = null;
        }    lpi.SetValue(obj, val1, null);
    }
      

  5.   

    有没有人试过怎么设置。
    好象不是这么简单。
    Columns 属性好象是只读的。
    要设置里面的每一项。
    应该没这么简单。
      

  6.   

    using System;
    using System.Collections.Generic;
    using System.Text;
    using System.Data;
    using System.Collections;
    using System.Reflection;
    using System.ComponentModel;
    using System.Collections.Generic;System.Reflection.Assembly baseAssembly = System.Reflection.Assembly.LoadFrom(AssemblyPath);
                Type entitytype = baseAssembly.GetType(className);
                object objModel = baseAssembly.CreateInstance(className);
                //获取所有的属性Name
                PropertyDescriptorCollection attCollection = TypeDescriptor.GetProperties(objModel);            Hashtable hTable = new Hashtable();
                hTable = DataRowConvertHashtable(row);            foreach (PropertyDescriptor attDescriptor in attCollection)
                {
                    PropertyInfo propertyinfo = entitytype.GetProperty(attDescriptor.Name);
                    try
                    {
                        if (hTable[attDescriptor.Name] != null)
                        {
                            object objValue = Convert.ChangeType(hTable[attDescriptor.Name], propertyinfo.PropertyType);
                            propertyinfo.SetValue(objModel, objValue, null);
                        }
                    }
                    catch (System.Exception e)
                    {
                        try
                        {
                            propertyinfo.SetValue(objModel, null, null);
                        }
                        catch (System.Exception ee)
                        {
                            continue;
                        }
                    }
                }记得给分哦
      

  7.   

    这写是啥跟啥啊。
    没搞明白。我要的是改变列属性的。
    因为Columns属性是只读的。一时搞不清楚该怎么设。
    我在代码中已经给出了读取哪个属性了。
    注意哦
    属性是只读的不能用SETVALUE设置值Columns。