【发布时间】:2018-01-25 19:39:19
【问题描述】:
我正在开发一个 Windows 窗体应用程序。我将 DataGridView 和数据绑定与 LINQ to SQL 一起使用。它正在工作。它绑定了整个数据。
我需要一些字段而不是整表数据,例如:
客户表包含五个字段(姓名、地址、手机、电子邮件、年龄)。
我使用下面的代码来获取整个数据。
private void AssignLocation_Load(object sender, EventArgs e)
{
// Get Datacontext object to connect with data source
SmartxBillingSystemDataContext dc = new SmartxBillingSystemDataContext();
// create table object
Customer customer = new Customer();
var allcustomers = from c in dc.GetTable<Customer>() select c;
// bind to datagrid
customerBindingSource.DataSource = allcustomers;
// now assign binding source to data grid view
dataGridView1.DataSource = customerBindingSource;
}
现在我只需要一些字段,例如姓名和地址。
另外,我需要在每一行中添加一个按钮,如下所示:
| A Name | An Address | Button |
| Another Name | Fake Address | Button |
我怎样才能做到这一点?请帮忙。
对于点击事件,我使用下面的代码
private void dataGridView1_CellClick(object sender, DataGridViewCellEventArgs e)
{
popUpLocationWindow();
}
private void popUpLocationWindow()
{
MessageBox.Show("I am clicked");
}
但是这段代码不起作用。
【问题讨论】:
-
@mjwills,我是开发新手,不了解提供的链接。能否请您给我一些与我的场景相关的想法,以便我消化。
-
您必须将一个一个数据成员添加到您想要的网格视图中 datafieldname 应该与对象属性相同,以便将额外字段与按钮类型绑定以获得额外的列.
标签: c# linq datagridview