[收藏]DataGridView绑定内嵌有对象的对象

http://www.informit.com/articles/article.aspx?p=446453&seqNum=5

添加一个DataGridViewColumn "UserIDColumn", 但是设置其Visible属性为false, 并增加如下事件处理

private void dataGridView1_RowsAdded(object sender, DataGridViewRowsAddedEventArgs e)
        {
            for (int i = 0; i < e.RowCount; i++)
            {
                DataGridViewRow row = dataGridView1.Rows[e.RowIndex + i];

                // 获得各行的主键,到BindingSource所关联的对象列表中搜索该行绑定对象。
                if (row.Cells["UserIDColumn"].Value != null)
                {
                    string key = row.Cells["UserIDColumn"].Value.ToString();
                    List<Person> group = (List<Person>)bindingCollection.DataSource;
                    Person p = Person.FindFromPersonList(group, key);

                    if (p != null)
                    {
                        row.Cells["ContactColumn"].Value = string.Format("{0} : {1} : {2}",
                            p.Contact.AddressLine1,
                            p.Contact.AddressLine2,
                            p.Contact.PhoneNumber);
                    }
                }
            }
        }


 
posted @ 2011-08-01 05:49  现役程序员  阅读(516)  评论(0)    收藏  举报