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 ); }得到比较容易一些.但如何通过反射动态的设置各个列的属性呢???
解决方案 »
- 关于循环插入的问题
- C# 怎么调用 Shell_NotifyIcon隐藏任务栏中应用程序的图标?
- 如何在DataGrid中显示图片?
- 公司面试题,顺便散分
- 疑问:WPFToolkit中的DataGrid数据列为何会意外丢失?
- 求教 c#连接sql anywhere9数据的代码
- datagridview赋值 求助
- 如何在PageLayoutControl中插入一个EXCEL
- WPF中如何保证一个控件的width和height始终相等?
- 介绍一些 关于 C# 的 例子 的网站。
- TaskVision调用WebService的GetUserInfo出错
- 高手救急!!!:如何在界面用radioButton控制dataGridView的数据编辑功能(WINFORM)
/// <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);
}
}
{
MessageBox.Show(c.Name +" "+c.HeaderText ); c.Width= 150
}
/// 设置对象属性的值,如果对象的属性依然是对象,会创建对应的空对象并设置相应的值
/// 需要注意的是,当采用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);
}
好象不是这么简单。
Columns 属性好象是只读的。
要设置里面的每一项。
应该没这么简单。
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;
}
}
}记得给分哦
没搞明白。我要的是改变列属性的。
因为Columns属性是只读的。一时搞不清楚该怎么设。
我在代码中已经给出了读取哪个属性了。
注意哦
属性是只读的不能用SETVALUE设置值Columns。