[英]DataGridView and BindingSource
我將DataGridView綁定到源:
BindingSource bdSource = new BindingSource();
bdSource.DataSource = db.Girls.Where(g=>g.BoobsSize == "C").ToList();
dataGridView1.DataSource = bdSource;
我只想在DataGridView上顯示“年齡”和“名稱”列,但將其他字段綁定到TextBoxes。
txt_Name.DataBindings.Add(new Binding("Text", bdSource, "Name"));
txt_Age.DataBindings.Add(new Binding("Text", bdSource, "Age"));
txt_Phone.DataBindings.Add(new Binding("Text", bdSource, "Phone"));
//.. other TextBoxes bound to the same BindingSource
怎么做 ? 與DataMember有關嗎?
解決方案是添加:
dataGridView1.AutoGenerateColumns = false;
dataGridView1.DataSource = bdSource;
dataGridView1.Columns[0].DataPropertyName = "Age";
dataGridView1.Columns[1].DataPropertyName = "Name";
dataGridView1.AutoGenerateColumns = false
應該在分配數據源之前。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.