[英]Linq to Entity insert data in db
How can I insert data added in GridView after clicking Save buton on BidingNavigator using Linq to Entity. 在使用Linq to Entity的BidingNavigator上单击“保存”按钮后,如何插入添加到GridView中的数据。
I am trying to add data to database but some problem occurs. 我正在尝试将数据添加到数据库,但是会出现一些问题。 I'm showing data using this code:
我正在使用此代码显示数据:
... ...
private void PostojeciPRojekti_Load(object sender, EventArgs e)
{
dbcontext = new logicrms2Entities1();
var projekti = from c in dbcontext.projekti
select c;
projektiBindingSource.DataSource = projekti.ToList();
projektiGridControl.Refresh();
}
... ...
EDIT 编辑
I have try to save data using: 我尝试使用以下方法保存数据:
private void projektiBindingNavigatorSaveItem_Click(object sender, EventArgs e)
{
dbcontext.SaveChanges();
gridView1.RefreshData();
}
First maintain a list of newly added objects in your form: 首先维护表单中新添加的对象的列表:
private List<projekti> addedList = new List<projekti>();
Then, handle RowsAdded event on your DataGridView: 然后,在您的DataGridView上处理RowFyre事件:
dataGridView1.RowsAdded += new DataGridViewRowsAddedEventHandler(dataGridView1_RowsAdded);
Next, add any added object to the list in your RowsAdded event handler : 接下来,将任何已添加的对象添加到Row Transactions事件处理程序的列表中:
private void dataGridView1_RowsAdded(object sender, DataGridViewRowsAddedEventArgs e)
{
addedList.Add(dataGridView1.Rows[e.RowIndex].DataBoundItem as projekti);
}
Finally, in your navigator's save button click event handler method you can store the objects: 最后,在导航器的保存按钮单击事件处理程序方法中,您可以存储对象:
private void saveButton_Click(object sender, EventArgs e)
{
var dbcontext = new logicrms2Entities1();
addedList.ForEach(p=> dbcontext.projeckit.Add(p));
dbcontext.SaveChanges()
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.