首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >DataGridView和BindingSource

DataGridView和BindingSource
EN

Stack Overflow用户
提问于 2014-07-28 11:07:02
回答 1查看 110关注 0票数 0

我将DataGridView绑定到源代码:

代码语言:javascript
复制
BindingSource bdSource = new BindingSource();
bdSource.DataSource = db.Girls.Where(g=>g.BoobsSize == "C").ToList();
dataGridView1.DataSource = bdSource;

我只想在DataGridView上显示“年龄”和“姓名”列,但将其他字段绑定到TextBoxes。

代码语言:javascript
复制
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有关?

EN

回答 1

Stack Overflow用户

发布于 2014-07-28 11:47:35

解决方案是添加:

代码语言:javascript
复制
dataGridView1.AutoGenerateColumns = false;
dataGridView1.DataSource = bdSource;
dataGridView1.Columns[0].DataPropertyName = "Age";
dataGridView1.Columns[1].DataPropertyName = "Name";

在分配DataSource之前,应先使用dataGridView1.AutoGenerateColumns = false

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/24987929

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档