问题:存储非简单的类属性值。
我有BindingListView<Hotel>
,这是我的datagridview的DataSorce。
我想注意,酒店课程看起来像这样。
public class Hotel
{
private Address address;
private Address legalAddress;
private Contact contacts;
public Hotel()
{
address= new Address(String.Empty);
legalAddress= new Address(String.Empty);
contacts= new Contact(string.Empty,string.Empty,string.Empty,string.Empty);
geo= new Geo(string.Empty);
}
public Contact Contacts
{
get { return contacts; }
set { contacts = value; }
}
public Address Address
{
get { return address; }
set { address = value; }
}
public Address LegalAddress
{
get { return legalAddress; }
set { legalAddress = value; }
}
public string FullName { get; set; }
public string AdmArea { get; set; }
}
现在关于Address
public class Address
{
private string fullAddress;
private int index;
public Address(string str)
{
fullAddress = string.IsNullOrWhiteSpace(str) ? null : str;
}
public string FullAddress
{
get { return fullAddress; }
set
{
fullAddress= string.IsNullOrWhiteSpace(value) ? null : value;
}
}
public string Index {
get { return index.ToString(); }
}
}
因为您可以看到Adress
和Contacts
不是简单的类型
我使用反射将数据填充到datagridview。
Datagridview从这个选项中获取数据:
//
// District
//
this.District.DataPropertyName = "District";
this.District.HeaderText = "District";
this.District.Name = "District";
//
// Address
//
this.Address.DataPropertyName = "Address.FullAddress";
this.Address.HeaderText = "Address";
this.Address.Name = "Address";
//
// LegalAddress
//
this.LegalAddress.DataPropertyName = "LegalAddress.FullAddress";
this.LegalAddress.HeaderText = "LegalAddress";
this.LegalAddress.Name = "LegalAddress";
现在我希望允许用户过滤datagridview中的数据
所以我想做这样的事情:thing that I want to reach
主要思想 - 在列表框中我存储datagridview的每一列,在组合框中,如果此列中的单元格(列表框中的选择),则显示每个唯一值
正如您可以看到Id
这样的简单数据,没有问题。
但是,例如,当我单击LegalAddress时,它找不到此列的值。
此外,我希望允许用户按地址属性Index
过滤数据,但我甚至不知道如何。
因此。你能帮我找到非简单类型的数据吗? 这里编码如何将数据存储到组合框:
private void PopulateCombobox(object column)
{
comboBox1.DataSource = null;
BindingSource data = dataGridView.DataSource as BindingSource;
DataGridViewColumn col = column as DataGridViewColumn;
ArrayList list = new ArrayList(data.Count);
// Retrieve each value and add it to the ArrayList if it isn't
// already present.
foreach (Object item in data)
{
Object value = null;
ICustomTypeDescriptor ictd = item as ICustomTypeDescriptor;
if (ictd != null)
{
PropertyDescriptorCollection properties = ictd.GetProperties();
foreach (PropertyDescriptor property in properties)
{
if (String.Compare(col.DataPropertyName,
property.Name, true /*case insensitive*/,
System.Globalization.CultureInfo.InvariantCulture) == 0)
{
value = property.GetValue(item);
break;
}
}
}
else
{
PropertyInfo[] properties = item.GetType().GetProperties(
BindingFlags.Public | BindingFlags.Instance);
foreach (PropertyInfo property in properties)
{
if (String.Compare(col.DataPropertyName,
property.Name, true /*case insensitive*/,
System.Globalization.CultureInfo.InvariantCulture) == 0)
{
value = property.GetValue(item, null /*property index*/);
break;
}
}
}
// Skip empty values, but note that they are present.
if (value == null || value == DBNull.Value)
{
continue;
}
// Add values to the ArrayList if they are not already there.
if (!list.Contains(value))
{
list.Add(value);
}
}
comboBox1.DataSource = list;
}
答案 0 :(得分:2)
解决
private void PopulateCombobox(object column)
{
comboBox1.DataSource = null;
BindingSource data = dataGridView.DataSource as BindingSource;
DataGridViewColumn col = column as DataGridViewColumn;
ArrayList list = new ArrayList(data.Count);
// Retrieve each value and add it to the ArrayList if it isn't
// already present.
foreach (Object item in data)
{
Object value = null;
PropertyInfo[] properties = item.GetType().GetProperties(
BindingFlags.Public | BindingFlags.Instance);
foreach (PropertyInfo property in properties)
{
if (String.Compare(col.DataPropertyName,
property.Name, true /*case insensitive*/,
System.Globalization.CultureInfo.InvariantCulture) == 0)
{
value = property.GetValue(item, null /*property index*/);
break;
}
else if(property.PropertyType==typeof(Address)|| property.PropertyType == typeof(Contact) /*||property.PropertyType==typeof(Geo)*/)
{
string propname = col.DataPropertyName.Substring(col.DataPropertyName.IndexOf('.') + 1);
if (string.Compare(col.DataPropertyName, property.Name + '.' + propname, true,
System.Globalization.CultureInfo.InvariantCulture) == 0)
{
object obj = property.GetValue(item, null);
Type propType = obj.GetType();
PropertyInfo pinf = propType.GetProperty(propname);
value = pinf.GetValue(obj, null) == null
? string.Empty
: pinf.GetValue(obj, null).ToString();
}
}
}
// Skip empty values, but note that they are present.
if (value == null || value == DBNull.Value)
{
continue;
}
// Add values to the ArrayList if they are not already there.
if (!list.Contains(value))
{
list.Add(value);
}
}
comboBox1.DataSource = list;
}
如果我们有'。'(点)DataPropertyName,当我们要去chek时,Name的正确部分将适合我们(或满足)。