1
我有一個使用BindingSource將Linq to SQL作爲數據源的datagridview。當我嘗試插入或刪除數據時,gridview不刷新。DataGridView CRUD使用LINQ to SQL操作
SampleDataContext context = new SampleDataContext();
BindingSource bindingSource = new BindingSource();
public Form1()
{
InitializeComponent();
bindingSource.DataSource = context.Persons;
PersonGridView.DataSource = bindingSource;
}
private void AddButton_Click(object sender, EventArgs e)
{
context.Persons.InsertOnSubmit(new Person { Name = , Address = });
context.SubmitChanges();
}
private void DeleteButton_Click(object sender, EventArgs e)
{
foreach (DataGridViewRow row in PersonGridView.SelectedRows)
{
var person = context.Persons.FirstOrDefault(x => x.ID == int.Parse(row.Cells[0].Value.ToString()));
context.Persons.DeleteOnSubmit(person);
}
context.SubmitChanges();
}
我在這裏錯過了什麼嗎?
最好的問候,
布賴恩